Need help with variables

Problem 1:
On collision, one bullet is supposed to subtract 50 health (variable) from an enemy, but the thing is, the program thinks that they collide like…a thousand times perhaps? Anyway, how could I make it so that 1 bullet subtracts only 50, instead of counting that they collided more than once?

Problem 2:
I made it so that when an enemy’s health equals 0, the enemy is deleted (only the one with 0 health). However, they’re all deleted instead of just that one with 0 health. How can I make it so that only one is deleted? This way the program wipes out all of them.

Thanks in advantage.

Problem 1:
Use the Trigger Once event if you want to trigger the action only a single time on each collision.
Use a timer if you want to trigger the action every X second as long the collision is happening.
Use an object variable as a counter or a Repeat event as sub-event if you want to trigger the action at certain number of times as long the collision is happening.

Problem 2:
Make sure you are using object variable to store the health of each enemy individually and after that you can use the For each object event to make sure the action is triggered only on the ones that met the condition.
Normally GD doing a good job with picking the ones that met certain conditions but in some cases you need to use this special event to check conditions on each and every instances one by one to make sure.

Also I would recommend to check if enemy health is <= 0 instead of = 0 because in some cases the enemy health might go below 0 if the damage was too big. Of course it is depends on your game design, but normally you do certain amount of damage say -3. If the enemy got only 2 health the result going to be -1 and so the condition “if health = 0” going to be false because it is not = 0.

For problem 2 it would be helpful to see the conditions you’re using to see if there’s an obvious reason why GDevelop’s picking all the enemy objects. Looping through all of them as ddabrahim suggests will work, but you might be able to solve the problem with a sub-event of the collision detection event, that has a condition of enemy health =0 (or maybe <50 would do if the bullets are the only thing that can reduce enemy health).

Bullet is in collision with enemy | Do -50 to enemy.Variable(health) Variable health of enemy <50 | Delete object enemy

By having the health check as a sub-event it forces GDevelop to only select the enemy object that has just been hit by a bullet.

For the collision, I would like to make it so that when the player collides with the enemy, only one collision is counted, and if they’re still colliding, then collisions are counted every 2 seconds as long as they’re still colliding. Could you please help me with making this script? I know exactly what the script should look like, just not sure how to make it and where to find these options.

And for the enemy health, you’re right, I’m just aware that they all share health (the same variable), so if one dies, they all do. How could I make this variable individual for each enemy that spawns?

I already set the health variable to be equal or less than 0 in case of those issues.

Thanks a lot, I appreciate it.

You are going to need a timer for each enemy. You can do that by using object variables. Check out this example how to create a timer for each instance using object variables:
gametemplates.itch.io/template- … le-sprites

You need something similar except, you would start increasing the value of the variable when the collision start and as long the collision is happening. When, there is no collision it would stop… I don’t have time to give you a working solution, but basically this what you want to do:


EDIT:// replace timer of Enemy >= 30 with a lower number like 3 to reduce enemy healh more frequently.

I don’t have time at the moment to explain it but I hope it helps somewhat. Basically it is the way as MattLB described, instead of using For Each, simply check collision and after do the rest in sub events, in the sub events it should pick the instance that is involved in the collision as MattLB described. I haven’t though of this I think it more simple. The key is to use object variables and sub events.

You need to use object variables, in the objects category you can find the events to work with variables these ones are for object variables as they are located inside the objects category, you can also add object variables inside the object properties window. My example and screenshot above show you how to work with object variables. But maybe you should read the tutorials available on the wiki first:

GD4:
wiki.compilgames.net/doku.php
GD5:
wiki.compilgames.net/doku.php/gdevelop5/start