Inheritance

From Game Editor

Revision as of 17:41, 21 January 2011 by Dst (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Inheritance reduces the amount of scripting you need for multiple actors. To enable inheritance, select an actor from the 'inherit events from' list toward the bottom of the actor control panel.

An example of an actor who can benefit from inheritance is your standard enemy. First you create an actor named 'enemy'. You won't use this actor in the actual game.

So your first enemy is a bat. Create an enemy named bat, and set bat to inherit events from enemy.

The next enemy is a zombie. Add an actor named zombie and set it also to inherit events from enemy.

Now add an event: Enemy>collision>any side>playerbullet> add your collision script here.

Now start the game and shoot the bat - it will do whatever enemy is programmed to do. Shoot the zombie, same effect.

But zombie and bat move differently, how can we do this when using inheritance? Any time an actor has an event in its panel, it will override (skip) the inherited events. So, if we give bat and zombie different draw actor events, they can act however we want them to.

Ideally, you'd want to end up with the enemy actor having all events except draw actor; and all the subsequent enemies you create should only have a draw actor event. Instead of specifying animations, etc. from events like collision, you change a state variable during collision, and simply check that variable in their draw actor script, and have each type respond appropriately.

In order to set variables in the createactor script which allow for differences, we will assign variables to the actor when we create it.

In this example, the global 'g-versions' of the variables are set when the actor is spawned. Example:

 Actor * this; //to store a temporary actor
 this=CreateActor("zombie", "zombie00", "no parent", "no path", 0, 0, false);
 ChangeAnimationDirection(this->clonename, STOPPED);
 this->speed=5;
 this->damage=1;
 this->animpos=0;
 this->type=2;

Zombie will now spawn with all the correct attributes, and we can change those gtypes any time we want to produce any type of enemy.

Remember to use this-> to alter an Actors variable from another script; use this->clonename to use this in a function.

So too we can use inheritance on different types of bullets. For this example i will use the enemy shots; eshot will be used for ebullet and emissile.

 Actor * this;
 this=CreateActor("bullet", "bullet00", "none", "none", 0, 0, false);
 this->angle=angle;
 this->directional_velocity=10;

Once again, emissile and ebullet inherit events and eshot isn't used in the actual game. Except emissile may have a homing script in it's draw actor; while ebullet may not need any draw actor at all.


Using inheritance will save you lots of time and make editing actors much much simpler.