I have an enemy of which I plan to have multiple instances of on screen at the same time. What I want to happen is that when the player is within 45px of one of these enemy instances, a random number will be generated which will determine what move the enemy does. Let’s say if it picks 1, the enemy will charge at the player. If it picks 2, the enemy will shoot a projectile at the player.
Since I want each instance of the enemy object to be handled individually, I have these events inside a For Each object loop. I know that you can’t use a Trigger Once inside a For Each and get the expected result…it will trigger one time and one time only, not taking each instance individually.
So I’m using some boolean variables to simulate a Trigger Once inside the For Each. The problem I’m having is that when I check if the player is within 45px of the enemy, once that’s true, it will remain true. Therefore, the actions I have for the boolean to select a random number are firing off every frame once the player is within 45px of the enemy, rather than picking that random number once since the condition of distance between the two objects remains true.
I’ve had success with starting timers and many other one-time events inside a For Each without using Trigger Once, but this one has me stumped. What other conditions could you check for which would allow this random number to be generated one time for each instance of the enemy object which is within 45px of the player object?
In other words, what I want is this…when the player is 45px or less from the enemy, it picks a random number between 1 and 3 ONE TIME, then executes the code for the move corresponding to the selected number. With a Trigger Once this would be simple…but inside a For Each, I can’t think of any other kinds of conditions to check for which would allow this number selection to happen once for each instance, since the condition (distance between two objects) remains true and therefore the boolean just keeps getting toggled true/false over and over again.
Any thoughts on the best way to accomplish something like this? Thanks.