Godot Version
4.3
Question
Hi guys, having trouble dealing with awaits in my godot project.
Quick summary is I have a GameEventsHandler autoload that, through a process that isn’t relevant to the question, generates an Array of objects with Callables.
These Callables return values, what I want is to go through each item in the array, await that call, then return pass the returned value into the next item in the array. Once they’re all complete I then want that final value returned.
Currently, only the first item in the array gets called at all, and it seems before that second one even gets ran its already returned the value.
# Callables generated and sorted above here
for i in CallableArray.size():
var temp_args = args_list
if return_item != null: temp_args.append(return_item)
temp_args += [CallableArray[i].is_source]
return_item = await CallableArray[i].self_call.callv(temp_args);
if return_item != null:
return return_item;
Heres the relevant code.
I think this needs to be
for i in range(CallableArray.size()):
Or you can just ditch the index and do
for callable in CallableArray:
That’s for sure nicer code, though it doesn’t solve the issue.
Essentially, say I give it the number 5, and I have two Callables, A doubles the value and B reduces it by 5.
I’d like to give A 5 and then update the return_item to 10, then give B 10 and update the return_item to 7, then return 7.
Essentially what’s happening is once A is run, it just returns that value (10 in the above case) and B isn’t ever called.
what is self call?
I just tried this and it works.
extends Node2D
func add_one(num:int) -> int:
await get_tree().create_timer(1).timeout
return 1 + num
var callable_array :Array[Callable] = [add_one,add_one]
func _ready() -> void:
var r = 0
for callable in callable_array:
r = await callable.callv([r])
print(r)
prints
1
2
is the caller that is potentially wanting this return item also using await
?
1 Like
Hi! Trying to match your working example, I realized that as I wasn’t duplicating the base args_list array, I was just successively appending to it.
I didnt’ realize if you give a callv the wrong args for the funciton, it just wont run rather than giving you an error. Having it set temp_args by args_list.duplicate() causes things to work perfectly.
Cheers for jumping my memory here.
1 Like