I'm making a RTS game. Every Unit in RTS game can do some actions, such as Patrol, Attack or Build. In unity, you can easily manually fill-in string and integer arrays for C# scripts.
Because of this, I decided that it would be easiest to have a string[] str_actions array for ever unit and when unit is first initialised, convert this array to Action[] actions.
I can probably do this:
string className = "Attack"
Assembly assembly = Assembly.Load("Actions");
Type t = assembly.GetType("Actions."+className);
Action action = (Action)Activator.CreateInstance(t);
But this doesn't handle two problems:
- Action doesn't have constructor that takes 0 arguments
- The possibility that
classNamerefers to class that is not child ofAction
How do I handle them?
abstract class Action. But serialisation uses reflection in C# - how else would you get class instances from strings?