x


How to use different types of scripts with an override function.

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?

more ▼

asked Jun 25 '11 at 06:50 PM

Nemox gravatar image

Nemox
169 11 16 20

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

It sounds like you want a virtual function.

public class Base {
     public virtual void SomeMethod () {
          Debug.Log("Base Method Called");
     }
}
public class DerivedA {
     public override void SomeMethod () {
          Debug.Log("DerivedA Method Called");
     }
}
public class DerivedB {
}

and here's an example of the implementation:

Base b = new Base();
DerivedA dA = new DerivedA();
DerivedB dB = new DerivedB();

b.SomeMethod();
dA.SomeMethod();
dB.SomeMethod();

Outputs

"BaseMethodCalled"

"DerivedA Method Called"

"BaseMethodCalled."

In a virtual function, the child class can override the parent's implementation of the method. Otherwise the parent's method is called. This is helpful for things like this include places where you need a polymorphic behavior Another example would be something like Animal.Bark(). You want all animal to be able to bark but they all make a different sound so you would need to override the base classes behavior. Note Animal could likely be abstract, but just for examples sake.

Virtual functions shouldn't be confused with abstract functions and/or classes. Abstract classes are classes that cannot be instanced and are designed to be inherited from by other classes. Abstract methods declared in abstract classes must be overridden by the inheriting class similar to how interface methods must be given a body in the implementing class.

Virtual calls have a small performance decrease but it usually won't matter.

more ▼

answered Jun 25 '11 at 07:22 PM

Peter G gravatar image

Peter G
15k 16 44 136

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:

class InputMovementHandler extends MonoBehaviour { 
      private characterMotor : Motor; 
      function Start () { 
            characterMotor = GetComponent.(); 
             //This objects motor will be treated as a generic motor.
             //That way you don't have to change this class at all per character. 
     } 
     function Update() { 
             characterMotor.Move(/pass input data here/); 
     }
 }

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)
10|3000 characters needed characters left

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:

  • they can be changed directly in the inspector even in play mode-
  • they are serialized with the scene
  • they can be changed dynamically by other scripts during the game-
more ▼

answered Jun 26 '11 at 08:02 AM

BigBulle gravatar image

BigBulle
202 9 12 21

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)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3343
x825
x480
x40

asked: Jun 25 '11 at 06:50 PM

Seen: 1083 times

Last Updated: Jun 26 '11 at 08:50 PM