(Solved) How to select only one instance of an object?

i’ve read other posts but couldn’t figure this out (sorry for any misspellings)

i’m try to make so that when an character gives an attack it gives damage to every enemy inside an specific area

but i cant find a way to do it, instead of giving damage to only the enemies close by it keeps killing every enemy on the map

how can i solve this? thanks for your time

Hi lightbell18,

It would be helpful to have a screenshot of your events.

How do you check if the enemies that should take damage are in the area? You could just use a hidden sprite object (or your attack sprite-object - if there is any) and check for collision. Or you could use the ‘distance between two objects’ condition. But it really depends on the game and the attack etc.

1 Like

i have deleted the previous code so i cant send a screenshot :frowning :frowning:

but the problem was with this code:

apply 40 points of damage to “Enemies”

how can i make so that instead of all “Enemies” it refers to only the enemies that are inside that area?

I see. So can you tell something about the shape of the damage area?

In general it is as I wrote above, you could check for collision with a sprite-object that represents your damage area, like:
shape_area

or you could check for the distance between your character and the enemies (for example, if the distance is below 250 pixels while attacking, you reduce 40 hp from the enemies).

distance_area

Btw in the examples enemies is an object group including different enemy objects.

1 Like

but in this case wouldn’t just happen the same way?
if any entity of the group “enemies” is in collision
then subtract health from the group “enemies”
making so that every enemy on the game gets damage, no?

Ah, yes. You are right. In this case you can add a for each-block as a sub-event, like:

(I have the linked text and enemies condition in the for-each block to see the health values for each enemy)

thank you very much for your help :slight_smile:
could you elaborate a little more about the logic behind this code? i know i could just copy it but i really would like to understand how it works

First the event checks for collision of the enemies and the attack area sprite. The ‘Trigger once while true’-condition makes sure that the health points are reduced only once per collision and not with every frame.

The sub-event is a ‘for each’-block which reduces the 40 HP only for those enemies-objects which are in collision with the attack_area sprite. Without this, the points for all objects in the ‘enemies’ group would be reduced - that was probably the problem you had.

The ‘Take into account linked objects’-condition and the ‘change the text’-action are just additions to see the individual hp-values for each enemy-object. You don’t need them, unless you want to display the hp-value for your enemies.

2 Likes

thank you very much, i was really frustated with this problem but you gave me a way to keep moving foward with my project, i’m very happy now.

god bless you :slight_smile:

1 Like