Skip to content

Saveable Component

Isaac Robinson edited this page Oct 3, 2023 · 1 revision

Usage

The saveable component is a simple adapter for entities that allows the game to save all entities easily. To use this component it must be added to a Entity and then called using .save().

Adding to an entity

The SaveableComponent is a generic component - meaning that the type is defined by the type of entity config that will be returned by the .save() method (the type T must extend BaseEntityConfig). When calling the constructor a simple function must be defined as one of the parameters which returns the relevant config class that represents the current state of the entity. An example instantiation onto a player entity would look as follows:

player.addComponent(new SaveableComponent<>(p -> {
    PlayerConfig playerConfig = config;
    playerConfig.position = p.getGridPosition();
    playerConfig.health = p.getComponent(CombatStatsComponent.class).getHealth();
    playerConfig.maxHealth = p.getComponent(CombatStatsComponent.class).getMaxHealth();
    return playerConfig;
}, PlayerConfig.class));

In the above example we use the variable config, which was the original config this player was created with to reduce the amount of duplication for fields that don't change thruoghout the execution of the program - such as spritePaths, etc.

Saving an entity

To save an entity simply call player.getComponent(SaveableComponent.class).save() and it will return the respective config file which then can be written to a .json file using the FileLoader.writeClass method.

Clone this wiki locally