Random Spawn with Fixed Movement Step

Hi all, I’m a JS developer, who’s trying out GDevelop.

I would like to:

  1. Spawn an Enemy (I’ve got an Object named Enemy) every 1.5s seconds.
  2. Move the spawned enemy either in X or Y, with a fixed distance of 150 pixels, every 1.5s.

Below are some parts of the logic that I’ve tried (obviously it should not be simply +150 etc.) but have had no luck. Below I would expect 1 enemy to spawn at the top left, and simply move 150 pixels right, each 1.5 seconds.

I tried programming this, but it seems that the Javascript window is a runtime loop, so I can’t do “setInterval”.

Any suggestions?!

#lostCoderWhoTriesTheNoCodeRoute

wait, so your enemy is not changing position?
if so, do the change position event first, then reset the timer.
what you can do is do a random roll with a variable.
timer move >1.5 secs do Randominrangewithsetp(0,1,1) to variable random
subevent variable random =0 do EnemyX+150
variable random =1 do enemyY+150
reset timer

Just to be clear, changing position will make it teleport, not walk.

As it is, GDevelop doesn’t know which enemy instance you want to move.
You should use a For each loop, or a Pick all action.
Or, if you use an object timer instead of a scene timer, each instance will have one timer which will be used for instance filtering.

Thank you, I didn’t know of an Object Timer, this makes more sense!

And exactly, I desire Teleport not walk so you’re right on the money.

I will explore these suggestions, thank you!

Ah, “Sub Events”, thank you.

Enemies are changing position ie. teleporting, either up/down or left/right. So, I assume I have to make a few sub events…

In JS:

const randomAxis = Math.random()
const randomDir = Math.random()

const axis  = (randomAxis > 0.5) ? "x" : "y"
const dir = (randomDir > 0.5) ? -1 : 1
const distance = 150

if (axis == "x")
  enemy.setX(enemy.getX()+(distance*dir))
} else {
  enemy.setY(enemy.getY()+(distance*dir))
}

you can use behaviors.
if double you click on the objects on the topright you find behaviors. there you find pathfinding.
give your objects variabes DestinationX and DestinationY
then use your event to change thos variables and use pathfinding action to move to the destinations.

1 Like

I’m trying the “simple” way first, without behaviours.

No effect, what am I missing?

The timer works, I tried it without the variable condition.

events dont line up like that. they need ther own events.
right now everything is 1 single event and a condition with dir =0 and also dir = 1 can never be true.each one must be its own subevent with the corresponding actions.
also reset the timer last! last subevent reset timer.

I understand! It works! Thank you Slash!