|
I have a Motor script that needs to call a Motion function from one of several types of Motion classes. So basically, one type of character might use the standard Motion script, while another character might use a SuperSpeed motion script that overrides the standard Motion class's main Motion function. How can I tell my Motor class which type of Motion class to use?
(comments are locked)
|
|
It sounds like you want a virtual function. and here's an example of the implementation: Outputs
In a Virtual functions shouldn't be confused with Virtual calls have a small performance decrease but it usually won't matter. Hm... It looks like I'm on the right track then, but what I really need is some way to serialize said class. Then if I had say, a Robot using RobotMotion instead of standard Motion, I could simply set his motion in the inspector.
Jun 25 '11 at 08:09 PM
Nemox
@Nemo, seeing your post below, I think I understand what you want to do. Unfortunately I can't think of a way to tell Unity which child of Motor to use with an inspected variable. Having said that, I would make a generic object moving script, and also attach the specific motor. So given you have a Motor and a RobotMotor. You would create a GameObject with 2 scripts on it: 1 to handle input and that passes that information onto the motor which controls the characters actual movement. So everyone would have this script: and each class will get a specific motor script attached to it that overrides the default Move() command. class RobotMotor extends Motor { override function Move () { //Move like a robot does. } }
Jun 26 '11 at 08:47 PM
Peter G
(comments are locked)
|
|
I would avoid where it's possible the use of inheritence in Unity because Unity uses the component pattern. Is it not possible for you to define public fields in your motion script that would change its behaviour (e.g. MAxAcceleration, MaxSpeed, ...)? There are several advantages in using public field:
I'm not sure I fully agree. Obviously any programming style can be done poorly, but I don't believe that using inheritance in Unity is usually a bad thing.
Jun 26 '11 at 04:50 PM
Peter G
Without more information from Nemo, I wouldn't go for inheritance especially if he starts with scripting. But maybe Nemo should give more details on what he is doing and which features he is looking through inherictance.
Jun 26 '11 at 06:21 PM
BigBulle
Imagine you have a script with a public GameObject. You can drag your GameObject into the spot in the inspector, and it will function based on that object. Instead of GameObjects or Components though, I want to do it with a simple base class; essentially, I would want to drag the script onto the spot, and my system would call the function from whatever script is there. I hope that's a better explanation.
Jun 26 '11 at 07:52 PM
Nemox
(comments are locked)
|
