Page 1 of 1

A lot of enemies with their own health. How?

PostPosted: Wed Jul 30, 2014 1:10 am
by xenidius93
It's me again :). I have one good question for you guys. How to make one integer (health) for one actor (enemy), when I want to make a lot of enemies? You know, when the enemy will be created, health should be created for him too. enemy.1 = "health 1", enemy.2 = "health 2". How to do this?

Re: A lot of enemies with their own health. How?

PostPosted: Wed Jul 30, 2014 1:16 am
by lcl
When creating the variable, select 'Actor variable' instead of 'Global'.
Now every actor has their own unique health variable accessible via actor.health

Re: A lot of enemies with their own health. How?

PostPosted: Wed Jul 30, 2014 2:57 pm
by xenidius93
This works the same way as normal variable :/. When there's collision between enemy and shot, in enemy Collision > Script Editor:

DestroyActor("Collide Actor");
enemy.health = enemy.health - 1;

What's wrong with this?

Re: A lot of enemies with their own health. How?

PostPosted: Wed Jul 30, 2014 4:15 pm
by lcl
If you write enemy.health there, it'll use enemy.0's health variable. What you want to do is to just use health, because we're in the enemy actor's own event. That way it'll use the clone that triggers the event. When you're not in the current actor's code, you'll have to use the getclone function.

Re: A lot of enemies with their own health. How?

PostPosted: Wed Jul 30, 2014 4:28 pm
by xenidius93
I don't understand :(, so how it should looks like? Where I should to use getclone?

Re: A lot of enemies with their own health. How?

PostPosted: Wed Jul 30, 2014 7:07 pm
by skydereign
xenidius93 wrote:I don't understand :(, so how it should looks like? Where I should to use getclone?

getclone would be used when you want to access a specific enemy actor. In this case of collisions though gE already provides a way to do this. If the actor with that collision event is enemy, than all you need to do is use the health variable.
Code: Select all
DestroyActor("Collide Actor");
health = health - 1;

This is because the event actor is the specific clone you want to have health reduced. If you had happened to put this event in the bullet actor, it would look like this.
Code: Select all
DestroyActor("Event Actor");
collide.health = collide.health - 1;

If you type an actor variable out in a script, it means the variable of what gE calls the event actor (the actor that is running the script). The collide actor is the one that caused the collision event.

Re: A lot of enemies with their own health. How?

PostPosted: Wed Jul 30, 2014 7:35 pm
by xenidius93
So what's wrong? Why it doesn't working? This is the project, please check it out: http://sendfile.pl/256362/test_zycia_wrogow.zip

Re: A lot of enemies with their own health. How?

PostPosted: Wed Jul 30, 2014 8:03 pm
by skydereign
That is because you aren't using getclone correctly. In this case you don't need to use getclone at all, nor should you be using enemy.health. As lcl said, enemy.health will get you the health of enemy.0. In your case you want to use the following.
skydereign wrote:
Code: Select all
DestroyActor("Collide Actor");
health = health - 1;

Re: A lot of enemies with their own health. How?

PostPosted: Wed Jul 30, 2014 8:13 pm
by Hares
xenidius93 wrote:So what's wrong? Why it doesn't working? This is the project, please check it out: http://sendfile.pl/256362/test_zycia_wrogow.zip

Because in your code you still use "enemy.health" to access the health variable, instead of just "health".

So in all the code on the enemy actor, change it to "health":
-in the create actor
-in the draw actor
-in the collision

You can put the twz-fixed.ged in same folder as the original, and see for yourself ... :wink:

edit: ahh ... skydereign was faster :)

Re: A lot of enemies with their own health. How?

PostPosted: Wed Jul 30, 2014 9:07 pm
by xenidius93
Okay, thanks guys so much :). And thank you Hares for fixed project ;D.

Re: A lot of enemies with their own health. How?

PostPosted: Wed May 27, 2015 4:57 pm
by mindcontrol
Hi, i tried, but is not working, each of mine enemys have their own life, but when i shoot to one enemy, they lose 2hp, but , a random enemy lose 2 hp, so sometimes i shoot a enemy and another one dies :/ . I'm using this codes:

On enemy create actor -> hp = 5;

On enemy draw actor->
if (hp<=0) {
DestroyActor("Event Actor");
}

On enemy collision with shoot -> enemy.hp=enemy.hp-2;

Hp is a actor variable as i read.

Re: A lot of enemies with their own health. How?

PostPosted: Wed May 27, 2015 7:26 pm
by lcl
Your problem is here:
mindcontrol wrote:On enemy collision with shoot -> enemy.hp=enemy.hp-2;

Writing enemy.hp targets the enemy with the cloneindex 0.
What you should do is just this:

This will take away 2 hp from the actor that is executing the code, so it would damage the enemy that has collided with the bullet actor you're calling with the name "shoot". :)
Code: Select all
hp=hp-2;


EDIT:
Also, you'd better move the other code together with the damage code. It's always better to try to avoid putting codes in Draw Actor, as those get executed every frame when the actor exists, and most things are not needed to be executed so often. So, ideally, you'd have this in you "enemy" collision with "shoot":
Code: Select all
hp = hp - 2;

if (hp <= 0)
{
    DestroyActor("Event Actor");
}

Re: A lot of enemies with their own health. How?

PostPosted: Wed May 27, 2015 7:32 pm
by Hares
Instead of
On enemy collision with shoot
Code: Select all
enemy.hp=enemy.hp-2;


try this
Code: Select all
hp=hp-2;


The difference is that if you set "enemy.hp" you only set it for enemy.0 (the first clone).
If you just set "hp" you set it only for the current clone (the current enemy clone in the collision).

Or as lcl explained it:
lcl wrote:If you write enemy.hp there, it'll use enemy.0's hp variable. What you want to do is to just use hp, because we're in the enemy actor's own event.


edit: ahh ... this time lcl was faster :D

Re: A lot of enemies with their own health. How?

PostPosted: Thu May 28, 2015 12:34 am
by Zivouhr
Good tips, thanks to all. 8)

Re: A lot of enemies with their own health. How?

PostPosted: Thu May 28, 2015 8:01 am
by mindcontrol
Is working! thank you guys :)