Add a score variable to a different score variable for each object depending on HP and timer

Hi everyone, I’m making a top down shooter where the Player is rewarded more points the faster they can kill the Enemy. I’m open to any way that this can be set up and it work, my failed attempt thus far has gone like this:

For each instance of Enemy, if Enemy_Health is greater than or equal to 100, the timer called “EnemyAliveScoreTimer” is reset.

The score starts at it’s highest number and decreases every 0.5 seconds until we get to 31 seconds.
At which point, the score for killing them only = 1.
I have a seperate Repeat for each instance event for each of these and for example, they go like this;

Repeat for each instance of Enemy: Condition is - The timer “EnemyAliveScoreTimer” of Enemy is greater than 1.5 seconds, Trigger once and Action is - Change the variable "EnemyAliveScore of Enemy: set to 944.

When the variable Enemy_Health is less than or equal to 0, I have a boolean variable called EnemyDead which is set to false by default but it is set to true when Enemy_Health = 0 as the Enemy is now dead.
I then say in a seperate Repeat for each instance of Enemy event, if the boolean value of EnemyDead is set to true and Trigger once then in the action for this event - Pause the timer “EnemyAliveScoreTimer” and change the Enemy variable EnemyAliveScore to Enemy variable EnemyDeadScore.

I then planned on adding the dead scores together to make a total score which would be the scene variable but as getting this far has yet to work for me, I haven’t gotten that far unfortunately as I cannot get EnemyDeadScore to change to anything from 0 and I cannot figure out why.

I’ve tried making it so that the Enemy object is not deleted when the Enemy variable Enemy_Health hits 0 but that didn’t make a difference. Neither did not using the booleans and being more direct and saying that when the variable Enemy_Health was less than or = 0 then EnemyAliveScore was set to and even added in a different trial to EnemyDeadScore but in both cases, EnemyDeadScore was still = 0. I have a couple of text objects in the scene that show each of them and outside of debugger that’s how I’ve been keeping track.

If anyone can help me solve this that would be amazing cause I’m totally puzzled why this won’t work. I’m going to try and add a few pictures to a post for the first time so I apologise if I cannot post more than 1 picture.

Have you tried using “subtract” (amount) instead of “set to” (amount) on the EnemyAliveScore variable?

If possible, can you upload another screenshot showing the rest of your code on the right?

2 Likes

A few things:

  1. Use only a single “Repeat for each enemy” event. Add no conditions or actions to it, then make all of your other events normal events that are subevents of the parent event. This will get you much better performance long term and avoid a lot of other issues.
  2. You should have just 1 subevent for your scoring decrease. It should have two conditions:
    “The timer “EnemyAliveScoreTimer” of Enemy is greater than 0.5 seconds”
    “The variable “EnemyAliveScore” of Enemy > 1”.
    It should have two actions:
    “Change the variable “EnemyAliveScore” of Enemy: Subtract 1”
    “Reset the timer “EnemyAliveScoreTimer” of Enemy”

This dramatically simplifies your code and avoids any potential recursion issues.

You shouldn’t be adding the dead scores together to make your parent score. You should instead just have a scene (or global) score variable, and your event that deals with your enemy dying you should have an event that modifies that variable to add the EnemyAliveScoreTimer object variable of the dying enemy.

1 Like

Thanks guys, good news and bad news…!

I’ve set it up vastly differently to before following the advice of Silver-Streak and it is much tidier and simpler to edit.
I’d love to set up any timers I use from now on forever in this fashion!
Initially I thought the timers and the EnemyAliveScore were being saved correctly for each Enemy but after running it through the Debugger, it appears not.

I can control how many Enemies spawn in the scene, no Enemies are already in the scene at the start, they begin spawning after a couple of seconds.
I set the limit of Enemies that would spawn to 20 from 10 and raised the default EnemyAliveScore value from 992 to 9920 as it decreases by 16 every 0.5 seconds because in my initial tests following the changes I noticed that the last 5 Enemies all had an EnemyAliveScore value of 0 and they had each deffinetly spawned less than 30 seconds from me hitting refresh in Debugger…

Thinking that was odd, I raised the EnemyAliveScore value to 9920 and when all 10 were dead, I hit refresh in Debugger to check the EnemyAliveScore value and the first 5 looked right but the last 5 were each set to 8320 despite the value Enemy_Health reaching 0 and the boolean EnemyDead going from false to true at different times for each Enemy, it shouldn’t have been possible for those last 5 to be displaying the same value…
I ran it again with 20 Enemies and 17 were correct while the last 3 were all the same number!?
So I ran it again… That time 15 correctly had different values but the last 5 were each 6032. Having no understanding why it’s doing this…

I figured, it can only do that if the timer for the Enemy exists after they have died so I thought that maybe if I delete the timer for each Enemy once their variable Enemy_Health was less than or = 0. Unfortunately it did not make a difference. Did 20 again, 15 were different, last 5 were each 6624.

So I went back to a previous save (18 from 22) to do more tests and I noticed something… 4 were correct while 6 were the same and all of the correct ones were “type”: “number” while all the ones that failed were “type”: “string”.
Decided I’d run a test in save 19… No good, I was still deleting the Enemy objects and replacing them with a DeadEnemy object at this point so there were no values to be seen in Debugger, save 20 was the same so on to 21… 3 correct, 7 saying EnemyAliveScore = 0, all numbers no strings.
Back to 22 and 5 the same, 15 correct, no strings only numbers. It seems the last 5 usually mess up but it’s not always 5 so…?

I have no idea what is going here or how to make it entirely work correctly so I’m going to abandon scoring the Player in this way and tweak things around a little differently, I was hoping Player’s would be able to eventually compete with their scores but I’d rather forego that feature for the time being so that I can continue moving things along.

I still also don’t quite understand the part about not adding the dead scores together to make the parent score so I was still stuck on making a total score, I could make it so that it counted once just one time or for just one Enemy infinitly but not for each seperate Enemy after they had died. I don’t think I need to know how to do that myself anymore but if anyone else comes by this in the future, I’ll happily try any possible solutions and post my results :slightly_smiling_face: