Signal not emit, why?

Godot Version

v4.3 stable steam

Question

Hi there! I’m learning Godot so sorry for any mistakes, I try emit signal with player to enemy player but player position is not displayed in debug window. Thanks for all help in advance.

Here is code of GameNode (main node):

extends Node

signal player_ready(player)

func _ready() -> void:
	var player = $Player
	emit_signal("player_ready", player)
	pass # Replace with function body.

func _process(delta: float) -> void:
	pass

And here is enemy code (node in GameNode):

extends Node2D

func _ready() -> void:
	var game_node = get_node("/root/GameNode")
	game_node.connect("player_ready", _on_player_ready)
	pass

func _process(delta: float) -> void:
	pass

func _on_player_ready(player) -> void:
	if player:
                print(player.position)
	pass

This is wrong method for Godot 4, It was for Godot 3. Try this:

player_ready.emit(player)
1 Like

Thank you, however, still I don’t get any message about player position.

I used shared variables instead

Enemy:

extends Node2D

var player

func _ready() -> void:
	add_to_group("enemies")
	pass

func _process(delta: float) -> void:
	print(player.position)
	pass

func set_player(p):
	player = p

GameNode:

extends Node

var player

func _ready() -> void:
	player = $Player
	if player:
		for enemy in get_children():
			if enemy.is_in_group("enemies"):
				enemy.set_player(player)
	pass

func _process(delta: float) -> void:
	pass

Just so you know, your code with the signal does work. Here is what the Scene Tree looks like.

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