0

I'm having a bit of trouble with instances.
Im making a game in java where the user fights monsters, each monster has its own class file containing static reference info (health, attack, defense, introText and atkText), which are subclasses to the Monster class.
I start the fight with:

Monster current = new monster();

My problem being I have absolutely no idea what the monster will be and I don't want a switch case.
Can I do something like

Monster current = new getClassFromString(monster);

that ?
I'm not that good at Java (started yesterday), so I'm sorry if what I'm asking is hard to understand.

I use the classes like a database to store the basic info of a monster type/race, or like a lua table (im more used to lua):

Dragon = {"health"=100, "atk" = 25, "def" = 10} 
Troll = {"health"=50, "atk" = 10, "def" = 0} //these exist in the files as classes
Monsters = { Dragon, Troll } //the ones before are subclasses of Monsters
//after the user selected the monster he wants to fight
current = Monsters[selected] //and now the table current contains all the info I need, without altering the stored one (in Dragon and Troll, and every other kind of monster)

Is there a way to do that in Java, but with the tables being classes?

  • It sounds like you want to use a [factory](https://en.wikipedia.org/wiki/Factory_method_pattern) as opposed to what you're thinking. The factory pattern is far more conventional than any hackery that you'd attempt with reflection. – Makoto Dec 02 '15 at 17:22
  • I'm sorry, but I don't get it. I wasted everyone's time, Im gonna do it in lua at least I know how – LiquidFenrir Dec 02 '15 at 17:43
  • To each their own, I suppose. Hopefully the link to the factory pattern can give you some insights into this. Think more in terms of patterns on the next time, so your question won't be marked as a duplicate in (what I believe to be) error. – Makoto Dec 02 '15 at 17:44

0 Answers0