0
\$\begingroup\$

I keep getting this error now in Game Maker:

FATAL ERROR in
action number 1
of Step Event0
for object base_enemy:

Variable base_enemy.hp(100013, -2147483648) not set before reading it.
at gml_Object_base_enemy_StepNormalEvent_1 (line 2) - if(hp <= 0){

I understand that it's not finding the variable hp in the object base_enemy during Step Event, but I'm not sure why. I think that the parenting I did has something to do with it, because it found hp before further edits to the code. It's set up like this:

base_enemy (top parent);
base_grunt (first child);
obj_grunt (second child)

The hp variable is set in the Create Event of the second child of each unit. Is it possible that the Step Event is checking for hp before the variable is declared?

\$\endgroup\$
1
\$\begingroup\$

The hp variable is set in the Create Event of the second child of each unit.

That means you are declaring variable hp in the Create Event of base_grunt, according to the hierarchy you exposed.

But the error Game Maker keeps displaying refers to object base_enemy, which is the parent object of base_grunt in your inheritance hierarchy.

In fact, you are testing a variable before it is declared. Object base_enemy must have variable hp declared in its Create Event, first of all because it's testing it in its Step Event, and then because health is a common trait of enemies, and due to inheritance a parent object should be in charge of anything (variables, scripts, ...) its children can have without writing the same code over and over again.

The YoYo compiler won't tell you anything about hp missing in the Create Event, because it can't know if you are declaring such variable after the object is created as long as it is before Step Event occurs.

Solution is: put hp = 0; in the Create Event of the very top parent (base_enemy, I guess), this way any child will have its own variable hp and variable testing won't return fatal errors anymore.

Also, I suggest you to give a chance to the built-in variable health, which is included in every single instance, and it has dedicated functions and events in the Game Maker environment.

\$\endgroup\$
  • \$\begingroup\$ I figured this out a while ago, actually. I used a boolean in the create of the base_enemy called kill and set it to false. Then in the collision event of the weapon, I set kill to true when the target runs out of hp. The step event in the enemy then triggers when kill is true and dies. But your way does sound simpler, so I may want to change it at some point. Thanks. \$\endgroup\$ – Shockra Jun 2 '16 at 15:03
  • 1
    \$\begingroup\$ @Shockra if this answer solves your problem, you should mark it as such. Alternatively you could also add own answer if that solution suits your needs better. \$\endgroup\$ – wondra Jun 2 '16 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.