Hi all. I'm an absolute beginner here with very basic knowledge in coding. I am creating my first game. It is like an endless runner with random spawns of obstacles moving at different speed from top (out of screen) to bottom (exit screen). There are different types of obstacles and each of them have their own speed. These same few obstacles are used in different scenes but they are positioned differently.
Everything was going well for me until I reached this weird problem... I managed to get the same few obstacle scenes to be randomly instanced but it doesn't seem to work every time. Some times it works endlessly, some things when I closed it and reopen, it doesn't work at all or it just works for a few spawn and then it just stops instancing.
Appreciate it if anyone could advise me on this? Is it because my codes aren't ideal or is it because of queue_free or the clash of the same instanced scene names, etc.? Thanks a bunch :)
Sample of my codes in my main scene's script:
const mobs_1 = preload("res://Mobs_1.tscn")
const mobs_2 = preload("res://Mobs_2.tscn")
const mobs_3 = preload("res://Mobs_3.tscn")
func spawn_mobs_1():
var new_mobs_1 = mobs_1.instance()
call_deferred("add_child", new_mobs_1)
new_mobs_1.position.x = rand_range(25, 480)
func spawn_mobs_2():
var new_mobs_2 = mobs_2.instance()
call_deferred("add_child", new_mobs_2)
new_mobs_2.position.x = rand_range(25, 260)
func spawn_mobs_3():
var new_mobs_3 = mobs_3.instance()
call_deferred("add_child", new_mobs_3)
new_mobs_3.position.x = rand_range(25, 195)
func spawn_mobs():
var randomNum = randi()%4 + 2
if randomNum == 1:
spawn_mobs_1()
if randomNum == 2:
spawn_mobs_2()
if randomNum == 3:
spawn_mobs_3()
func _on_Player_spawn_mobs_signal():
spawn_mobs()
# When Player exits Area2D of obstacle scene, this signal is called. When obstacle scene's VisibleNotifier exits scene, queue_free().