Skip to content

Characters

Hackusate_PvP edited this page Aug 4, 2023 · 8 revisions

Characters are objects that resemble a character in your game. These objects can contain your own data and there is a lot of freedom you have with them. All characters have to be created before the story is created. I recommend creating the characters in the function createBaseData(), but before we do that we must create a character class. There are two types of character classes Character and MainCharacter. First let's create the main character of the story.

public class MyMainCharacter extends MainCharacter {
    // You can add your own data here, more about this will be explained later but just to showcase lets add a money value
    private int money;

    public MyMainCharacter(String id, String name, Color color) {
        super(id, name, color);
    }

    public int getMoney() {
        return money;
    }
}

Okay let's process what we just did. We created a main character which requires three things; id, name, color. The id is used as a unique identifier for the character. The name is self explanatory and the color is the color display of the character. Let's create this character in our function.

@Override
public void createBaseData() {
    new MyMainCharacter("mc", "mc", Color.RED);
}

Once our character is created it is stored in the RenJava class. I recommend creating your own getInstance for your class but you can use the RenJava class if you want.

MyMainCharacter mc = RenJava.getInstance().getCharacter("mc); // This is red because you have to cast MainCharacter to getCharacter.
// The following should be used instead
MyMainCharacter mc = (MyMainCharacter) RenJava.getInstance().getCharacter("mc);

// Now you can get data from the class
mc.getMoney();

Let's explain data a little more as there are a few types of data that you can use. Local data is data that is not saved and is created at runtime. This data is very easy to use as its just basic java variables. The other type of data is persistent data or saveable data. This type of data is saved in the save-file and when a save is loaded the data is re-written. Let's go back to the character class we created.

// In order to have this classes data saved to the save file it must implement PersistentData.
public class MyMainCharacter extends MainCharacter implements PersistentData {
    // We are going to save the money field to the save file by annotating the field with @Data. In order for this to work properly you cannot have the field be final.

    @Data
    private int money;

    public MyMainCharacter(String id, String name, Color color) {
        super(id, name, color);
    }

    public int getMoney() {
        return money;
    }
}

Clone this wiki locally