Godot Version
4.4.1
Question
When I save my IdleSpawner script, sometimes I get the error Bad Address Index
in the stack trace tab.
When I click on it it takes me to this line await ground_timer.timeout
func spawn_ground():
await ground_timer.timeout
I also have an error in the debugger tab related to this script:
E 0:05:15:143 load_source_code: Attempt to open script 'res://Scripts/IdleSpawner.gd' resulted in error 'Can't open'.
<C++ Error> Condition "err" is true. Returning: err
<C++ Source> modules/gdscript/gdscript.cpp:1115 @ load_source_code()
However the game runs as expected and can be saved and loaded. The IdleSpawner script is saved, can be edited and works fine. Ignoring this error is giving me no problems.
Do I have a problem? I can’t see anything wrong but I don’t understand this error enough to know why it has a problem with this line and this script and if I should be worried.
The remainder of my code regarding the timers is here in case it’s relevant:
func _ready() -> void:
root = GlobalVariables.current_scene_root
camera = GlobalVariables.camera
camera.connect("on_rotate", set_spawning_rects)
ground_area2d = $GroundSpawnArea2D
await get_tree().process_frame
sky_timer = Timer.new()
root.add_child(sky_timer)
ground_timer = Timer.new()
root.add_child(ground_timer)
sky_timer.start(randf_range(sky_timer_min, sky_timer_max))
ground_timer.start(randf_range(ground_timer_min, ground_timer_max))
spawn_sky()
spawn_ground()
func spawn_sky():
#when the timer ends
await sky_timer.timeout
#If can, spawn a new prop and get a reference to it
if check_if_can_spawn(spawned_sky_props, sky_spawn_limit):
var new_sky_prop = spawn_prop(cloud0_res, get_random_position_on_screen(sky_rect))
#put it in an array so we can cound how many props there are
spawned_sky_props.append(new_sky_prop)
#regardless, set a new timer length & try again
sky_timer.wait_time = randf_range(sky_timer_min, sky_timer_max)
# call this function again
spawn_sky()
func spawn_ground():
#when the timer ends
await ground_timer.timeout
#If can, spawn a new prop and get a reference to it
if check_if_can_spawn(spawned_ground_props, ground_spawn_limit):
var new_ground_prop = spawn_prop(branch0_res, get_random_position_on_screen(ground_rect))
#put it in an array so we can cound how many props there are
spawned_ground_props.append(new_ground_prop)
#regardless, set a new timer length & try again
ground_timer.wait_time = randf_range(ground_timer_min, ground_timer_max)
# call this function again
spawn_ground()
How do these two functions ever end?
func spawn_sky()
func spawn_ground()
They don’t, they keep going in the background every n seconds.
I don’t think the circular logic there is the issue. The error is detected only on the spawn_ground function.
I don’t think gdscript is tail recursive?
Perhaps see if you still get the problem if you do:
func spawn_ground():
while true:
await ground_timer.timeout
[...stuff...]
That is, loop explicitly rather than making a recursive tail call. I wouldn’t be surprised if your error is the stack overflowing.
3 Likes
Hi!
Thanks for you help with this.
Because the issue was intermittent I thought I’d leave it a bit of time before replying.
The addition you suggested does seem to have solved the issue, but it has caused the game to run really slowly. After about 30 seconds the frame rate drops and the game eventually crashes.
Removing the “while” condition completely solves the frame rate issue, so I’m quite sure that this is the culprit.
Is there another way to solve the recursion issue without impacting the frame rate? I’m sure I could find a new logic flow to re trigger the function, but I wonder if there’s a tidy solution working with what I have.
Thanks again!
If you just want to keep spawning something periodically, you’re probably better off creating a periodic timer and having the completion handler call the spawn function. Make the spawn function spawn one thing and then end.
That should solve your framerate issues, and will also give you a little more control over how quickly things spawn.
1 Like