Timer Flag how to use correctly

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Teatimeman

Hi,
What im trying to do is to code a “generic” timout funciton for multiple timers. My current approach is to try and use the flags in the method head of the connect function, but how can i access these flags? For example when i try to pass my timeout function an argument it results in the error of not accepting arguments.

Im trying to code an enemyspawner that uses my own Resource where the spawn time/ratio is defined and at the same time i want to spawn multiple enemies.

Below you can see my code snippet for my resource:

extends Resource

class_name Spawn_info

@export var time_start:int
@export var time_end:int
@export var enemy:Resource
@export var enemy_num:int
@export var enemy_spawn_delay:float

And the code for the node/scene (so far just for printing):

extends Node2D

@export var spawns: Array[Spawn_info] = []
@onready var player = get_tree().get_first_node_in_group("player")

func _ready():
	for i in range(spawns.size()):
		var spawn_info = spawns[i]
		spawn_info.timerInstance.set_one_shot(true)
		spawn_info.timerInstance.set_wait_time(spawn_info.enemy_spawn_delay)
		spawn_info.timerInstance.connect("timeout",_on_timer_timeout, i)
		add_child(spawn_info.timerInstance)
		spawn_info.timerInstance.start()

func _on_timer_timeout(spawnIndex):
	var spawn_info = spawns[spawnIndex]

	var timeStart = spawn_info.time_start
	var timeEnd = spawn_info.time_end
	var enemyResource = spawn_info.enemy
	var enemyNum = spawn_info.enemy_num

	print("Spawn Info:", spawn_info)
	print("Time Start:", timeStart)
	print("Time End:", timeEnd)
	print("Enemy Resource:", enemyResource)
	print("Enemy Number:", enemyNum)


print("Spawn Info:", spawn_info)
print("Time Start:", timeStart)
print("Time End:", timeEnd)
print("Enemy Resource:", enemyResource)
print("Enemy Number:", enemyNum)

Any help is appreciated

:bust_in_silhouette: Reply From: Enfyna

I am on my phone so i will write short. I think you dont bind the variable in your connect function.
I would write this spawn_info.timerInstance.connect("timeout",_on_timer_timeout, i) as

spawn_info.timerInstance.timeout.connect(_on_timer_timeout.bind(i))

This will send the variable to your function.