Page 1 of 1

Same actor with individual health/life. How to do this?

PostPosted: Thu Aug 18, 2011 9:40 pm
by mcavalcanti
Hi, I created two of the same soldier. Like this:

CreateActor("soldier", "soldier", "(none)", "(none)", 498, 253, false);
CreateActor("soldier", "soldier", "(none)", "(none)", 261, 331, false);

Each one must have his own life value. I suppose I need to use actor variable, but I don't know how. I set the elife (enemy life) variable as actor variable, but it is affecting both soldiers. How to do? Thanks.

Re: Same actor with individual health/life. How to do this?

PostPosted: Thu Aug 18, 2011 9:55 pm
by skydereign
Well, if it is an actor variable, it shouldn't be changing both. What might be the problem is how you are displaying the variable. For instance if you had 50 cloned actors, that had the actor variable hp. This code would only change the first clone's hp variable.
Code: Select all
clone.hp-=10;


So when you display a variable via a health bar or textNumber, you have to be more specific.
Code: Select all
Actor * c2 = getclone("clone.2");
textNumber = c2->hp;


Now it is much easier if you use the especial actor types (creator, parent, collide). So if you have an hp bar for each soldier you create, put the create actor for the hp bar in the soldiers create actor. That way when you need the hp displayed, you can access the correct clone by using this.
Code: Select all
textNumber = creator.hp;


Now if you want to lower a soldiers hp during a collision (assuming the event belongs to the other actor), you can do this.
Code: Select all
collide.hp-=10;


So to sum that up, it is probably due to you not using actor specific references to the variable.

Re: Same actor with individual health/life. How to do this?

PostPosted: Thu Aug 18, 2011 10:17 pm
by schnellboot
oh sky was faster
anyway I made a demo for you

Re: Same actor with individual health/life. How to do this?

PostPosted: Thu Aug 18, 2011 11:00 pm
by mcavalcanti
Oh, I see. I understood both examples. Thanks. But, please, how can I destroy the textNumber of the soldier who died?

Re: Same actor with individual health/life. How to do this?

PostPosted: Thu Aug 18, 2011 11:31 pm
by skydereign
You can use this code to check if the creator is still alive or not. If it isn't, that statement will be true and it will destroy itself. You could also store the index of the created hp display actor, but this one is less involved. You'll probably put it in draw actor with your textNumber call.
Code: Select all
if(getclone(creator.clonename)->cloneindex==-1)
{
    DestroyActor("Event Actor");
}

Re: Same actor with individual health/life. How to do this?

PostPosted: Fri Aug 19, 2011 12:15 am
by mcavalcanti
Hi, skydereign. I'm using the getclone2 function. The schnellboot example was clearer to me. I'm not understanding this last statment you posted. :-(

Re: Same actor with individual health/life. How to do this?

PostPosted: Fri Aug 19, 2011 1:48 am
by mcavalcanti
The enemy's life actor is working correctly with the getclone2 function. Actually i'm having problem with creating and destroying this life actor. When i destroy one, i destroy all.

Re: Same actor with individual health/life. How to do this?

PostPosted: Fri Aug 19, 2011 7:40 am
by skydereign
Well what schnellboot did will work, but isn't as reliable as what I suggested. And in this case using getclone2 like he did is actually more work (2 lines of code, custom function, and Actor*). I changed the draw actor code of the health display to have this code.
Code: Select all
textNumber=creator.elife;


That does exactly the same thing but doesn't rely on the actors having the same cloneindex. Anyway, I also added the code I said to copy paste into the draw event to it. So the full draw event is as follows.
soldierHp -> Draw Actor -> Script Editor
Code: Select all
textNumber=creator.elife;
if(getclone(creator.clonename)->cloneindex==-1)
{
    DestroyActor("Event Actor");
}


And that should work. If for some reason that still wasn't clear, all you need to do is replace soldierHp's draw event with the code above.

Re: Same actor with individual health/life. How to do this?

PostPosted: Fri Aug 19, 2011 8:13 am
by schnellboot
I didnt know there is "creator"
thanls sky

Re: Same actor with individual health/life. How to do this?

PostPosted: Fri Aug 19, 2011 2:10 pm
by mcavalcanti
Hey, it's working! :D
Thanks for explaining again. I was doing something wrong, but now it's ok. Thank you both! :)