Problem with signal looping

Godot Version

4.2.1

Question

How to make the 1st script work like the 2nd. It’s hard to explain, but the first script sends a signal with the same last position for all units, rather than in order for each. How to make the signal sent in the order of the array of units
#1

	var targetPositionList = [Vector2(50, 50),Vector2(60, 60)]
	var targetPositionListIndex = 0
	for unit in units:
		if unit.selected:
			SignalManager.give_pos.emit(targetPositionList[targetPositionListIndex])
			targetPositionListIndex = (targetPositionListIndex + 1) % targetPositionList.size()
			print(targetPositionListIndex)

#2

	var targetPositionList = [Vector2(50, 50),Vector2(60, 60)]
	var targetPositionListIndex = 0
	for unit in units:
		if unit.selected:
			unit.position = targetPositionList[targetPositionListIndex]
			targetPositionListIndex = (targetPositionListIndex + 1) % targetPositionList.size()```
1 Like

It should emit in-order of the for loop. If your signal is not deferred then any connected functions will run immediately when emitted.

Make sure to format your code pastes

Is there any reason you would use the first sample, it seems like you want to use a for loop with the units directly like in #2. You cannot achieve #2 with a signal, since emitting is by definition going to all connected functions, not just one unit at a time.

what should I do then? check the id of each unit?

bro, I solved the problem, in the unit script I then simply check the id.

	var targetPositionList = get_position_list_around(local_position, 8, 4)
	var targetPositionListIndex = 0
	for unit in units:
		if unit.selected:
			var unit_id = unit.get_instance_id()
			SignalManager.give_pos.emit(targetPositionList[targetPositionListIndex], unit_id)
			targetPositionListIndex = (targetPositionListIndex + 1) % targetPositionList.size()

What does your give_pos connect to? I feel like this signal is unnecessary

it gives a position to unit’s where each unit needs to go. I’m making rts, it’s a long story