[SOLVED] Can someone help me out with this logic?

I want to create an enemy that has two main behaviors:

  • Enemy will move towards the player.
  • Shoot bullets to player but the enemy standing still (remove force to the enemy.)
    This two behaviors will be active alternately. (First it will move then it will shoot then it will move and so on.) (loop)
    I want to have a variety to this by having different time when the behavior will change to the other.

For example:
VariableRandom = RandomInRange(1, 2)
If VariableRandom of enemy = 1, do the first behavior for 3 seconds then the second will occur next.
If VariableRandom of enemy = 2, do the first behavior for 5 seconds then the second will occur next.

The first behavior is pretty straight forward just move towards the player until the timer is greater than the seconds set on the VariableRandom.

The second behavior will create bullet, and add permanent force towards the player. This will occur every 0.5 seconds so if this behavior will last to 3 seconds, it supposed to shoot 6 times. If 5 seconds, it will shoot 10. Depends also to the VariableRandom.

I can do this and it will work if it’s only one enemy is created, but when I added more, problem occurs. This is my problem years now for it’s hard to for each object properly. Hope someone help me with this. My ideal result here is to make this work to all enemy who have this events.

Are you using object variables and object timers? Because you can’t achieve it with scene variables and timers.

If you are using object variables, could you share the events that are relevant for the actions you described?

2 Likes

Yeah. I use object timer and variables. I will post events that’s related to this later.



This is almost the same with what I want but this works only if one of this object is currently created.

In the second screen snip, I would look at putting in a Foreach EnemyFly subevent off the two parent events (that check the value of EnemyFly.Phase), and make the other events subevents of those two.

Also, the in last event of the second screen snip. switch the EnemyFly and BulletSmall around. I believe the way you have it as it stands won’t work as you think is should.

1 Like

Hello!

First I think you need make the changes like MrMen described. If the problem still happen maybe you can fix this by removing most of the “Trigger once”. I can’t test it here, but following the code I’ll try to help.

I don’t know why you’re using “Trigger Once” in “Fly’s Pre-Events” and “Fly’s Phase Setter” and some subevents when the parent events have it. It seems to me that you are putting “Trigger once” in all conditions and maybe following the logic: “if it helps ok, if it does not help will not do any harm”. But do this can be a trap. I believe you need to be very careful using “trigger once” when you’re dealing with multiple instances of the same object.

The changes I think you should try to remove these trigger once would be:

In “Fly’s Dash Movement (Phase One)” you don’t need the “Trigger oce” in second subevent. For the first subevent, I think you can put another object variable to make it move and remove the Trigger once too. Something like this:

Obs.: I used “an instant force” instead “permanent force” to make the movement because the boolean variable will be continues true.

For "Fly’s Shoot Movement (Phase Two) if I understand corretly it will always reset the timer “Shoot”, right? So, I think you can just delete “Trigger once” in subevent and parent event and try it. You can use the Debugger to see if it’s creating more than 1 instance. In my little test here it’s created just one instance.

1 Like

It works perfectly! I now understand the “Trigger Once” and “For Each Object” on how they can use properly. Thank you so much @MrMen and @Rasterisko! I post a lot of How do I topics here because I always have a problem in multiple objects events. Now I guess I understand more about it. Thanks!

1 Like

I’m glad it worked! :star_struck: I remember your other post about the difficulty of using multiple instances in Gdevelop. I think “Trigger Once” works strangely with “Repeat for each instance” because it doesn’t take into account the number of instances. The condition occurs just once and cannot occur anymore while it’s true. That’s why it works to one instance and stops work to another instances.

But sometimes we will need to limit our conditions to occur only once. And if not a good idea use “trigger once” with “repeat for each instance” we’ll need to do it in another way. The most simple way is the action makes the condition itself false, like here in your code:

Yours conditions when are true happend just 1 time, because you changed the variable in actions, turning the condition to false. I can make another example with my project where I didn’t used variables to limit, but the animation:

My enemy never comeback to animation “andando” later it changes. So, it was a good ideia use that condition to limit the event to occur just 1 time.

Maybe you already get the logic, but I’m explain just to clarify. :v:

But I’m going to be honest. Regarding “repeat for each instance” I also have some doubts. So I always do tests and more tests here. Maybe one day I can fully understand what conditions are specific and don’t need “repeat for each instance” and which ones they need.

1 Like