I have a game that runs on a similar system to games like terraria or minecraft, at least in the aspect of being blocky.
I have constructed it in a way where each block is an Object, and there is a specific class (extends a BlockObject archetype) for each type of block.
I have the game save the blocks in a file like following
I get the name of the instance of the Object i am saving, and store it in a string I start writing the information of the given Object in a string that has the Name of the Class(signifieing what type of block it is Ex. a block that is an object of BlockGrass.java would be stored as BlockGrass). Normally when reading information from a save file I am reading the save file, when it gets to the info of the blocks it first saves the Block type in a local string (BlockGrass in this case), also saves the positions (x cordinate, and y cordinate). Then it would run through a series of if statements like the following
if(blockType.equals("BlockGrass"))
{
blocksArray.add(new BlockGrass(...));
}
else if(blockType.equals("BlockStone"))
{
blocksArray.add(new BlockStone(...));
}
etc
now I want to make my game vary dynamic, and want it so that if someone decides to make a new Block class (Modding the game) they do not have to modifiey the MapReader class. so instead of running through a series of if statements as shown above (where everytime a new block class is created, a new if statement must be placed for that block), i want it so that it make a new object of a class that has the same name as the BlockType. to give you a better idea look at the following
BlocksArray.add(new blockType(...));
the reason what is shown above does not work is because blockType is a string that holds the name of the class i want to use. not a Class Name
How could i do something like that?, without using if-statements.
.add(new Class.forName(String).newInstance());