Hello all,

I'm creating a VR game similar to space invaders where the player is entirely stationary and aliens spawn in and fly towards them to attack. The way I wanted to have them spawn in was to create a bunch of 3d Position nodes arranged in a half circle in code, put them into an array, then randomly choose a point in that array to have an alien spawn from.

Does anyone have an algorithm that could be used to generate a half circle? I've made spheres before in opengl but I'm new to GDScript so I'm having trouble translating that knowledge over. Sorry if this is too niche a question.

Thank you!


  • Code:

    Click to reveal Click to hide
    func _ready():
    	var radius : float = 7.0
    	var nodesDesired : int = 10
    	var a : float = (2.0 * PI) / float(nodesDesired)
    	a *= 0.5 # 0.5 for half circle
    	for i in nodesDesired:
    		createPrintedBox(str(i), cos(i * a) * radius, 1.0, sin(i * a) * radius) #String of i, x, y, and z

You don't even need to do a fancy algorithm for this, simply look up the code for adding children, in Godot 4 it's

var enemyInvaders = []

for enemyInvader in enemyInvaders:

var customChild = customChildScene.instantiate()
add(customChild)

For positions you could still use the node method and just create an array you would then do a randi range with a second array you declare that has all of your nodes and have the children from the for loop spawn at those positions if you want it randomly, or you could even do it in order, just as a note this is all pseudo code I whipped up on a dime so be sure to look at the documentation and research how to use for loops and arrays in GDScript generally so you can use it in context.

Just you want to be even fancier since this is a game similar to space invaders that does patterns, you could learn about co-ordinates and randomise that instead along a pattern which would take a bit more maths but would give you a much more randomised result, don't forget to look up random number generators in Godot for a truly random output. For loops and arrays by the way are extremely useful in games especially as you start doing more complex mechanics.


    Code:

    Click to reveal Click to hide
    func _ready():
    	var radius : float = 7.0
    	var nodesDesired : int = 10
    	var a : float = (2.0 * PI) / float(nodesDesired)
    	a *= 0.5 # 0.5 for half circle
    	for i in nodesDesired:
    		createPrintedBox(str(i), cos(i * a) * radius, 1.0, sin(i * a) * radius) #String of i, x, y, and z

      Lethn Thank you for your response! What you described with the pseudocode was exactly what I was thinking of doing! Instead of a child scene, however, I would just do:

      var spawn_point := Position3D.new()

      Then add it to the array. But this helps a lot, thank you very much!

      Erich_L This is absolutely perfect, thank you very much for your response and the picture you attached. I appreciate it a lot!