How to save/load game data with Entity/Component based Engine via JSON

Hello dear JGO :slight_smile:

I am advancing with my project, which is a 2d Tile based game.
I use entities for pretty much everything, but i do not limit myself to just using entites with components over inheritance.

Furthermore, for the time beeing I do not use a fully fledged ECS, the way my system works for now is like this:

I store the entites in arrays (or wherever needed), and add components to the entity component hashMap.
The components the entity has, are described by the EnumSet of the enum type of the components.
Then i use systems which operate on the entity mask(EnumSet) and on the component values.

My question is: How could i save my entities, i.e the components instance variables, states etc. properly without having to add (at least not much) code,
when i add a new component types. Also, since it’s early development, i will change my component classes one day.

I guess i could add a method to every component, but thats something i would rather like to avoid, the less code -> the better!
Also i dont know how to structure this properly in JSON, and with structure i mean how the objects should be save in JSON in a certain hierarchy.

I tried saving some stuff yesterday and the more complex thing was to actually reload it without getting exceptions, since i would have to load everything as ‘JsonObject’ and ‘JsonArray’ etc. and then receive the correct values from it, then pass it to the class initialization.
That system seems extremely brittle to me and complete non-sense without a proper way of structuring it.

Thank you very much for your time and help in advance :slight_smile:

YOU DO NOT NEED TO READ THE CODE, IDEAS IN GENERAL ARE WELCOME, JUST FOR INFO

Entity Class example:


public class Entity {

	// IVARS
		// TAG
		private String tag = "none";
		// ENUM SET MASK
		private EnumSet<CompType>mask = null;
		// COMPONENT MAP
		private HashMap<CompType, Component>compMap = null;
	
	public Entity(){
		// INIT IVARS
			// INIT MASK WITH COM_TYPE 'NONE'
			this.mask = EnumSet.<CompType>of(CompType.NONE);
			// INIT COM_TYPE -> COMPONENT HASH MAP
			this.compMap = new HashMap<>(0);
	}
	
	public void addComponent(CompType compTypeIn, Component compInstIn){
		// ADD COMPONENT TO HASHMAP
		this.compMap.put(compTypeIn, compInstIn);
		// ADD COMP_TYPE TO MASK
		this.mask.add(compTypeIn);
	}
	
	public <T> T getComponent(CompType compTypeIn){
		return (T)(this.compMap.get(compTypeIn));
	}
	
	public void removeComponent(CompType compTypeIn){
		// REMOVE COMPONENT FROM HASHMAP
		this.compMap.remove(compTypeIn);
		// REMOVE COMP_TYPE FROM MASK
		this.mask.remove(compTypeIn);
	}
	
	// MASK GETTER
	public EnumSet<CompType> getMask(){return this.mask;}
	
	// TAG SETTER / GETTER
	public void setTag(String tagIn){this.tag = tagIn;}
	
	public String getTag(){return this.tag;}
	
}

Component Class example:


public class Transform_COMP extends Component{
	
        // IVARS
        public Vector2f pos = null;
        public Vector2f size = null;
        public float rotation = 0.0f;
	
        public Transform_COMP(float xIn, float yIn, float wIn, float hIn, float rotIn){

        // INIT IVARS
        this.pos = new Vector2f(xIn, yIn);
        this.size = new Vector2f(wIn, hIn);
        this.rotation = rotIn;	
	}
}