5
\$\begingroup\$

I am making a visual novel game and I was wondering is there a better way of setting character attributes and initializing than the way that I have done down below. As you can see I did an abstract class so that I can call each character without rewriting a bunch of code. I also have an array that initializes everything. What I was wondering, is there a better way of initializing them instead of using an array, it is just tiring to type in

Init.C[1].getAge(); 

every time that you want to get or set a variable.

What i want specifically is a way to initialize all these Character Objects without cluttering up my code and if possible automaticly initialize all of them, as you can see i tried to do in my init peice of code.

If anyone has any suggestions on how to initialize all these Character Objects I would be grateful, Also Any other comments or suggestions are welcome.

package visualnovel;

import Characters.*;

public class VisualNovel {



public static void main(String[] args) {
    Init Init = new Init();
    Init.Init();
    Init.C[1].getAge();
}
}

Here is my Init peice of code

package visualnovel;

import Characters.*;

public class Init {
Characters C[] = new Characters[3];

public void Init(){
    Characters();
}

public void Characters() {
    C[0] = new Main();
    C[1] = new Secondary();

    for (int i = 0; i < 2; i++) {
        C[i].Init();
    }
}
}

Here is the Character Abstract Class

package Characters;

public abstract class Characters {


private String FirstName;
private String LastName;
private String EyeColor;
private String SkinColor;
private String Sex;
private String Age;
private String HairColor;
private static int NumofCharacters;

public Characters(){
    FirstName = null;
    LastName = null;
    EyeColor = null;
    SkinColor = null;
    Sex = null;
    Age = null;
    HairColor = null;
}

 // mutators

public void setFirstName(String F){
    FirstName = F;
}
public void setLastName(String L){
    LastName = L;
}
public void setEyeColor(String E){
    EyeColor = E;
}
public void setSkinColor(String S){
    SkinColor = S;
}
public void setSex(String S){
    Sex = S;
}
public void setAge(String A){
    Age = A;
}
public void setHairColor(String H){
    HairColor = H;
}
public void setDetails(String FirstName, String LastName, String Sex,
String EyeColor, String HairColor, String SkinColor, String Age){
        setFirstName(FirstName);
        setLastName(LastName);
        setSex(Sex);
        setEyeColor(EyeColor);
        setSkinColor(SkinColor);
        setAge(Age);
        setHairColor(HairColor);
        NumofCharacters++;
}

  // Accessors    
public String getDetails(){
    String D;
        D = LastName + ", " + FirstName;
        D += "\nSex: " + Sex;
        D += "\nAge: " + Age;
        D += "\nEyes: " + EyeColor;
        D += "\nSkin: " + SkinColor;
        D += "\nHair: " + HairColor;
        return D;
}

public String getFirstName(){
    return FirstName;   
}
public String getLastName(){
    return LastName;   
}
public String getEyeColor(){
    return EyeColor;   
}
public String getSkinColor(){
    return SkinColor;   
}
public String getSex(){
    return Sex;
}
public String getAge(){
    return Age;
}
public String getHairColor(){
    return HairColor;
}
public int getNumberOfCharacters(){
    return NumofCharacters;
}


public String toString(){
    String S;
    S = LastName + ", " + FirstName;
    S += "\nSex: " + Sex;
    S += "\nAge: " + Age;
    S += "\nEyes: " + EyeColor;
    S += "\nSkin: " + SkinColor;
    S += "\nHair: " + HairColor;
    return S;
}


@Override
public abstract void Init();
}

Charcter Example Code

package Characters;


public class Main extends Characters{

public void Init() {

setDetails("Allyson","Carter","Female","Green","Blonde","White","27");
}

}
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Have you considered a data-driven approach? This would involve making a file format and having a file that contains the information for each character. You could then add or remove characters at will by simply updating the data file. You could make a routine which looks for a particular file or takes a path to a file and simply reads the data from it and creates characters as it reads them from the file. The file could be in any format that you find easy to work with - XML, JSON, INI, whatever.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.