I have a stack Overflow error on a spawner script.I've been trying to find the solution for a couple pf days now.If anybody could help. Thanks!

Here's my Script.

extends Node2D

func _ready():
	var rand = RandomNumberGenerator.new()
	#Generates Random Number
	var enemyscene = load("res://EnemySpawner.tscn")
	var screen_size = get_viewport().get_visible_rect().size
	#Gets the Size of the screen
	
	for i in range(0,10):
		var enemy = enemyscene.instance()
		rand.randomize()
		var x = rand.randf_range(0,screen_size.x)
		rand.randomize()
		var y = rand.randf_range(0,screen_size.y)
		enemy.position.y = y
		enemy.position.x = x
		add_child(enemy)

Your script did not cause an error when I tested it, so the problem might be in the EnemySpawner scene.

By the way, creating a new RandomNumberGenerator object is not necessary. You could simplify line 13 to var x = rand_range(0, screen_size.x), for example.

You also don't have to call randomize all the time. Once in ready (after you create the generator) should be enough. All this does is seed the RNG. After it is seeded, each additional call to the rand functions will essentially look random.

EDIT: Oh, yeah, and like @Azedaxen says, you don't even necessarily need the RNG at all.

3 years later