SOLVED How do I program HP and damage?

K

Kaplutnoir

Guest
so I've been trying to implement health to enemies, but somehow I must be overlooking something.

global.Enemyhealth is obviously a stupid idea to use for individual enemies, so I tried doing this:

in obj_Enemy:

Create:


var HP = 10;

Step:

if( HP <= 0 ){

instance_destroy();

}

Collission (with ea obj_Bullet):

HP -= 1;

But every time I start up the game, it gets aborted because "variable HP not set before reading it."

I've tried many combos up to now, even putting everytihing in step, but to no avail. This seems like a very obvious code, but somehow it eludes me. I also can't find any good examples on how to implement health and damage.
While browsing Google, I find that a lot of people give the same solution more or less than I already typed above this. But I know that regular variables can only be used in the same block of code (step/create/collision/...). I'm still very green programming wise so please be gentle :) I'm also a bit stupid so I need a very clear explanation if anyone'd be willing to help ;D
 

chamaeleon

Member
You're creating a local variable that is only available within the scope of the event or function. Removing var will make it an instance variable that will remain available as long as the instance exist.
 
Top