[Solved] Change score when enemy dies


I finally got my score working. I now need help in understanding how to get the score to increase when an enemy dies. Does anyone know why when I use “player has just fired” the score counter works, but when I select “enemy is dead” the score counter does not work?

Many thanks.

Answered in the other post:

I am using the 8 bit shooter template and when I chose Enemy1 is dead, the score does not change. As far as I can tell the Enemy1 is losing all its health and then being deleted, so I can’t figure why the score counter isn’t working.

Here is your problem. You are deleting the Enemy1 before you check that it’s dead. So there won’t be any dead Enemy1 objects around when it comes time to increase the score.

Ok. How do I solve it? Sorry, I am still learning. I haven’t changed how enemies die from the 8 bit template.

Increase the score in the same event when the Enemy1 is deleted. If there’s a chance that multiple enemies can be hit simultaneously, then it will need extra sub events. But that can be dealt with later.

For now, just increase the score in the same event that the Enemy1 is deleted, and remove the event where you check if it’s dead.

Ok. So having it set up through an external event might not be ideal then??

You can put it in the external event if you’ll be using external event in other scenes or want to keep all scoring functionality in one script.

If so, then the alternative method is to move the enemy deletion to the event where you check if it’s dead.

It’s a “6 of one and half a dozen of the other” type situation.

Thank you MrMen. That solved my problem. It all works now. I just moved my score increase code to my scene event (instead of external event) and now all works. Thanks my friend! It still is confusing why my code didn’t work in the external event, since I did check if the enemy was dead there?

Because the Enemy1 had been deleted by that stage. The Enemy1 is dead check goes through all the Enemy1 objects in the scene , and selects only those marked as dead. Because the dead one had been removed, it wasn’t captured by the event conditions.

Ok. I understand. It looks as though the level takes priority then? It seems like it would be more complex to use the external event method then. I’m still trying to figure all these things out.

External events keep a set of events in a separate file. When you link to the external events, you’re effectively including those events at the point of the link. When the game runs, GDevelop treats it as if you’d copied and pasted the events at the link point.

And it’s not about priority - if you’d put the Enemy is dead event in the level scene events, it still would have made no difference. It’s the order things were done.

External events are great if you have repeated code over multiple scenes, so you only need to make a change in one spot. They can also help keep the code a tidier by having more compact appearance, making the easier to read and follow.

1 Like

Wow, thank you for that explanation. I’ve been wondering why my external link was not working correctly. Now I know. I put the link at the bottom of my level events, at the end of my code. Ok that makes alot of sense now.