0 votes

Is it possible for me to make Area2D gets duplicated and spawns like 20 pixels forward the currently area2d and repeat that progress and so it spawns like 10 pixels higher and lower so its random?

in Engine by (17 points)

1 Answer

+1 vote
Best answer

Sure, that can be done.

I might recommend something like:

  • Create a standalone scene containing the Area2D and anything else you want to be a part of your spawned instances.
  • In a script on some controlling scene, preload your Area2D scene using something like var area2d = preload("res://Scenes/Area2D.tscn")

    • Create a loop that will spawn as many instances as you want. You could use something like this to drive the loop for i in range(10): (that'll execute the code in the loop 10 times)
  • In the loop, create a new instance of your preloaded scene using var newArea = area2d.instance()
  • Also in the loop, position the newly instanced scene at some random location based on logic of your choice. With your new, random x and y position selected, you can place the new item there using newArea.postion = Vector2(newX, newY)

That's really about it. Just season to taste...

by (12,282 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.