Yokilla
1
Hello !
I am making a turn based dungeon crawler.
The monsters needs to random, so my plan is to make a List of all the possible monster (The monsters will just be strings with the name of the monster ex. “Goblin”) and call a random number from 0 to the amount of monsters in the list.
Every monster will be a method that holds different variables like HP, Strength etc.
My problem is to call the specific method based on the string from the List
Is this possible? or is there an easier method of doing this?
I know i can use switch:case but i wanted something a bit more dynamic.
I hope i’ve been specific enough
I would not recommend doing methods the way you describe, rather as whydoidoit suggests. The way I usually handle it:
public class MobType //: ScriptableObject
{
public string name;
public int maxHealth;
...
}
public class Mob //: MonoBehaviour
{
public MobType type;
public int health;
...
}
Furthermore, you can make your MobType a ScriptableObject and have it live in prefabs, editable on the fly. If your mobs are prefab GameObjects, then if your Mob is a MonoBehaviour attached to those, you can link the corresponding MobType.
As for storing the list of types, you can do something like
public class MobTypes //: ScriptableObject
{
public List<MobType> mobTypes;
...
}
Depending on your game’s structure, you might want that as a ScriptableObject too that can live in prefabs or have it be a static class or a singleton.
Well to answer your question SendMessage will call a method based on the string in the list. (There are other ways which employ “reflection”).
I’m not sure what you are trying to do though, it sounds like your monsters should be instances of classes to me, not strings.