Page 1 of 2

How to make an enemy take several hits

PostPosted: Tue Jan 22, 2013 11:17 am
by lverona
I am doing a top shooting game. It is easy to shoot an enemy and make it explode. What I wonder is how can you program an enemy to take several bullets before dying? Note that in my game there are no levels and enemies are generated randomly, so I have to go for a very generic solution.

Re: How to make an enemy take several hits

PostPosted: Tue Jan 22, 2013 6:09 pm
by skydereign
HP is a very generic solution. Just have an actor variable called hp which gets reduced by one per hit, and if it reaches zero, then trigger the explosion death sequence. That itself can be handled by an if statement in the collision event.

Re: How to make an enemy take several hits

PostPosted: Wed Jan 23, 2013 9:06 am
by lverona
But how to make a variable unique to an actor? I mean, there may be several copies of same actor created.

Re: How to make an enemy take several hits

PostPosted: Wed Jan 23, 2013 9:08 am
by skydereign
You create an actor variable. You can't declare actor variables via global code, like you can other variables. So you have to use the built in gui.
In the script editor
Click [Variables]
Click [Add]
Set name to the name of the variable you want.
Click [Global] and change it to [Actor variable]
Click [Add]
Click [Close]
Now every clone in your game will have their own copy of the variable (like how all actors have their own copy of x and y).

Re: How to make an enemy take several hits

PostPosted: Wed Jan 23, 2013 9:26 am
by lverona
Thank you man, did not know this existed. Super great, will use this :)

Btw, as a part-time solution I made a probability hp, meaning you shoot and chances are you will kill it from 3-7 shots. Actually, feels not bad as well. I might as well keep this randomized hp for some enemies :)

Re: How to make an enemy take several hits

PostPosted: Wed Jan 23, 2013 10:23 am
by lverona
Added an actor variable but each time I try to use it, the script editor says - unexpected variable delcaration.

Re: How to make an enemy take several hits

PostPosted: Wed Jan 23, 2013 12:45 pm
by lverona
I did put the hp variable at the beginning of the script and it solved an error, however this is weird. What if I need the variable to be not in the beginning of the script? How to declare it without changing its value?

Re: How to make an enemy take several hits

PostPosted: Wed Jan 23, 2013 7:12 pm
by skydereign
lverona wrote:Added an actor variable but each time I try to use it, the script editor says - unexpected variable delcaration.

You sure there isn't a typo somewhere?

lverona wrote:I did put the hp variable at the beginning of the script and it solved an error, however this is weird. What if I need the variable to be not in the beginning of the script? How to declare it without changing its value?

You should not put a local copy of the hp variable in any scripts. Otherwise gE won't use the actor variable you created.

Re: How to make an enemy take several hits

PostPosted: Wed Jan 23, 2013 8:44 pm
by lverona
You should not put a local copy of the hp variable in any scripts. Otherwise gE won't use the actor variable you created.


Can you please explain how should I use this variable? So, I created an actor variable hp. What then?

Re: How to make an enemy take several hits

PostPosted: Wed Jan 23, 2013 9:02 pm
by skydereign
Then use it like any other variable.
enemy -> Collision with bullet -> Script Editor
Code: Select all
hp--; // lower the actor's hp
if(hp<=0) // actor is dead
{
     DestroyActor("Event Actor");
}

If you want the bullet to be the event actor, can use collide.hp to access the enemy the bullet is colliding with's hp. And, instead of "Event Actor" you specify "Collide Actor".

-Edit
And in the enemy's create actor, you would need to set hp to whatever number of hits you want them to survive.

Re: How to make an enemy take several hits

PostPosted: Wed Jan 23, 2013 9:41 pm
by lverona
Yes, I initially did everything like you explained and it does work, maybe I did not explain well the problem I had.

Look, in your example you have hp--; in the start of the script. However, if you put it into the middle, you'll get a "delaration unexpected". What should I do then? What if I need hp in the middle somewhere?

Re: How to make an enemy take several hits

PostPosted: Thu Jan 24, 2013 1:02 am
by skydereign
Can you post the code? It shouldn't matter where you reference the variable. Though, deceleration unexpected I believe is the error when you have some code, and then try to declare a variable, after you started running normal code.

Code: Select all
int var; // you can declare variables at the beginning of a code block
hp--; // but as soon as you stop declaring variables, you can no longer declare any

So the following gives an error.
Code: Select all
hp--;

int var;

If you are doing that, then make sure your declarations come at the beginning of the event, or brackets.

Re: How to make an enemy take several hits

PostPosted: Sat May 16, 2015 1:24 pm
by mindcontrol
hi, i tried to do this:
On enemy:

-Draw actor -> script editor -> hp == 5;

-Collision with any side of player -> script editor -> hp --;
if(hp<=0) { DestroyActor ("enemy"); }

So i wanted wich the enemy only lose 1 hp for each time he collide, but he die on the first collision, what am I doing wrong?

Re: How to make an enemy take several hits

PostPosted: Sat May 16, 2015 9:34 pm
by koala
mindcontrol wrote:hi, i tried to do this:
On enemy:

-Draw actor -> script editor -> hp == 5;

-Collision with any side of player -> script editor -> hp --;
if(hp<=0) { DestroyActor ("enemy"); }

So i wanted wich the enemy only lose 1 hp for each time he collide, but he die on the first collision, what am I doing wrong?
First, the problem is you wrote
Code: Select all
hp == 5;
instead of
Code: Select all
hp = 5;
First compares hp and 5 and if those two are same, the result of this entire expression is 1, else it is 0. But that result is not stored (saved) anywhere and you can't use it in any way, it doesn't affect anything inside your game. Even if it is saved, that's not what you need. So you should write the second statement, which will assign a value 5 to hp. It does what you wanted to do, value of hp is 5.
However, there's one more error. On enemy you should write script in Create Actor, not in Draw Actor. Draw Actor script executes on every frame. If your game has 30 fps, then this script will execute 30 times in a second, or in other words, 30 times in a second 5 will be assigned to hp. It actually doesn't matter much how often it happens. The problem is that you "reset" hp every frame. Write that script in Create Actor. That script executes only when the actor is created.

Re: How to make an enemy take several hits

PostPosted: Sun May 17, 2015 7:08 am
by mindcontrol
It work! thanks