So here's the situation : I'm creating a multiplayer game, and I want my friends, ..., to be able to create bots for it. So what I'm going to do is create an abstract class Bot{}
, and define functions for the bots, then they'll have to implement them : pretty straight forward yet.
Where I'm stuck, is how do I add them afterwards ? Can you use reflection or something like that to find all inheriters of Bot
? Or do I need to search through .jar files ?
Until now, here's how I did it (simplified, classes are not in the same file, etc) :
abstract class Bot{
public int play();
//...
}
class Bot1 extends Bot{
@override
public int play(){/*do stuff*/}
//...
}
and then when I need to have one of each bot :
ArrayList<Bot> bots = new ArrayList<>();
bots.add(new Bot1());
//...
My problem is that if I were to use this, as the Bot1
instanciation is hard-coded, I need to allow people to modify my code to add their own bots - which I don't want.
My question is, how can I achieve this without it being hard-coded ? I know it's possible, as Minecraft does it (mods and such), but I have no clue how to do it. So far, my researches didn't got me any results (probably because I have no idea how to explain this in a simple sentence...), so any clues of tips are welcome, as well as places to learn how to do it.
Also, I have a good understanding of OOP and such, but I'm totally new to reflection and getClass()
things...