Skip to content

Characters

piitex edited this page Jan 25, 2025 · 8 revisions

Characters

Characters are the object which represents a character in your game. Each character requires an id, name, and color.

Create a Character

To create a character, you will need to create a new class which extends Character

public class MyCharacter extends Character {
    
    public MyCharacter(String id, String name, Color color) {
        super(id, name, color);
    }
}

The id is used to identify the character. The id must be unique and cannot be re-used. The name is the default display-name for the character. The color is the color of the display text when displaying the character name in a text-box.

Register a Character

After you created the character class you will have to register the character to the framework. Go to your main class, the class that extends RenJava, and find the method createBaseData(). Use the registerCharacter(Character character) function.

@Override
public void createBaseData() {
    MyCharacter character = new MyCharacter("char", "Character", Color.BLUE);
    registerCharacter(character);
}

Character Data

Most characters will need data for game progression. Let's say for this example we have a character that has stats. These stats will have to be defined, access, and saved.

public class MyCharacter extends Character {
    private int strength, defense, magical;

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

    public int getStrength() {
        return strength;
    }

    public void setStrength(int strength) {
        this.strength = strength;
    }

    public int getDefense() {
        return defense;
    }

    public void setDefense(int defense) {
        this.defense = defense;
    }

    public int getMagical() {
        return magical;
    }

    public void setMagical(int magical) {
        this.magical = magical;
    }
}

By using the functions we created for the stats we can modify the character.

public void foo() {
    MyCharacter character = RenJava.getInstance().getCharacter("char");
    character.setStrength(100);
    character.setDefense(100);
    character.setMagical(100);
}

Saving Character Data

Any data that needs to be saved must implement PersistentData. Next, tag all the fields you want to be saved with @Data. The field must not be final. The field must be generic.

public class MyCharacter extends Character implements PersistentData {
    @Data private int strength, defense; // You can tag multiple fields.
    @Data private int magical; // Tag only one field.

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

    public int getStrength() {
        return strength;
    }

    public void setStrength(int strength) {
        this.strength = strength;
    }

    public int getDefense() {
        return defense;
    }

    public void setDefense(int defense) {
        this.defense = defense;
    }

    public int getMagical() {
        return magical;
    }

    public void setMagical(int magical) {
        this.magical = magical;
    }
}