Transferring data with signals

Godot Version

4.4.1

Question

I’m attempting to send some data via a global signal bus, but I’m unsure of how to actually send the data over? Either that or the signal isn’t correctly connecting, but I don’t think that’s the case.
How would I transfer over the data from combat_board_node, to Global, to enemy.

My current solution, that doesn’t work, is the following:
in combat node:

pos_fill += 1
check_pos_fill()
Global.enemy_move_data.emit(pos_dict[pos_fill])

In Global - signal enemy_move_data(pos)

In enemy:

func _ready():
	enemy_data.connect(_on_enemy_data)
	Global.enemy_move_data.connect(real_coord_move)

# unrelated code between these two

func real_coord_move(pos):
	if to_move == true:
		position = pos
		to_move = false
		print("move data received, at " + pos)

Any help is greatly appreciated!

what does your print statement say? What are you expecting to recieve in real_coord_move?

real_coord_move(pos) moves the enemy to the correct coordinates in the viewport. It sends the signal to all enemies so it ensures the correct enemy receives the coordinates with to_move. The print statement checks the enemy received the data and moved to the correct position.

pos in real_coord_move is a Vector2 with the coordinates its supposed to move to, which is stored in a different scene. That’s the data I’m struggling to transfer over from the other scene through the Global scene.

Does this print statement run? If so what does it say? Do you have any errors or warnings?

Do you emit your signal during the _ready function?

Sorry for the late reply! I’m often not on during the weekend.
This print statement does not run, I have plenty of warnings, but those are mostly warning me that I’m not using the global signals within the global scene class.

There are a couple signals going around, so I’ll explain when both are emitted.
The entity_moved Global signal is emitted when an enemy is trying to move. The enemy provides two pieces of data, where its moving from and where it wants to move to.
The possible positions for the enemy to move then check if they are the position the enemy wants to move to, if it is, then they check if they have a free space thats possible to move to, if they do then they emit Global.enemy_move_data which sends the position data back to the enemy, both telling them they can move there, and also the coordinates they need to move to in the viewport.
For some reason the signal to send the data back to the enemy does not work.
Here’s the related code in each of the spaces it can move to:

func update_entity_pos(old_pos, new_pos):
	print(relative_pos)
	print(new_pos)
	if old_pos == relative_pos:
		pos_fill =- 1
		check_pos_fill()
	if new_pos == relative_pos:
		pos_fill += 1
		check_pos_fill()
		print("enemy to move received")
		Global.enemy_move_data.emit(pos_dict[pos_fill])

And the respective code in the enemy:

	if to_move == true:
		position = pos
		print("move data received, at " + pos)
		to_move = false

The signal is connected in the enemy like this:

func _ready():
	enemy_data.connect(_on_enemy_data)
	Global.enemy_move_data.connect(real_coord_move)

If you are emitting and hoping it sends back to the same script you should call the function directly instead. And a global signal will emit to every enemy, not just one.

Since you are connecting on _ready it should always catch the signal, unless it’s emitted before the enemy is added to the scene.

Fixed it! Turns out a boolean that needed to be true was getting set to true after it needed to be set.
Thank you for the help either way

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.