I’m currently struggling with a “design” problem and hope someone with the experience can help me out.
I have three classes derived from a base actor class:
static (can only rotate)
biped (can move and rotate, as one)
dyadic (can move and rotate the upper body separately)
All actors in the scene rely on one of them. Now I would like to create a controller class which enables to control each of them. So I can control all kinds of actors and furthermore change who the player is controlling whenever I want. Also there is only one player, who can control one actor at a time.
What would be the best way to approach this?
I can’t really use GetComponent as it requires me knowing of what type the actor is
I couldn’t find a way to “override” a method outside the actor class
I could use SendMessage, but I’m not sure if its cool to use it in an Update function …
I could use delegates and only make the current actor listen to the messages, not sure again if its good practice when called each frame
I could make the controller a singleton and let the “current” actor read the values from there
Get the values by reference?
Make the control values static?
I switched to C# recently and don’t know all the possibilities provided, so any thoughts on the matter would be greatly appreciated. Thanks!
You can create a base class for your actor class, which contains all the common functions for an actor.
You could then implement the common control functions like Move or something, and then override the specific behavior in a child class so that the movement is whatever its supposed to be for that character type. Do this for all other character types, then use polymorphism in your main controller script. This is nice because if you have some shared common behavior between all actors, you can implement it here once and skip it in the child classes, while at the same time, you are also free to modify the behavior for each actor, and even add new additional actor types later that will still work with your controller code.
IE: Actor has children which inherit from it (StaticActor, DipedActor, DiadicActor). Has functions: Move, Rotate, Attack, etc. Each function should be overridden by the child classes to suit the behavior you expect.
Then in your controller class simply do a GetComponent().Move() and through the magic of polymorphism it should work. If you’re unfamiliar with how this works, do some google searches on polymorphism to get a better idea of how it works
No problem! Yeah I found out through experimentation that getcomponent works with polymorphism. I guess since they probably use reflection for that, it works just fine.