Godot Version
4.7.2
Question
I’m coming from Unreal Engine trying to switch my current project from there. In Unreal you could grab a reference to the player pawn in every actor you wanted, which was very convenient. Is there a way to set up something similar in Godot?
Follow up: I’ve learned about signals now and I’m trying to send the reference to my player along, but it gives this error:
E 0:00:01:522 Mancante.gd:42 @ _ready(): Error calling from signal ‘DeclarePlayer’ to callable: ‘CanvasLayer(hud.gd)::_on_mancante_declare_player’: Method expected 0 argument(s), but called with 1.
<C++ Source> core/object/object.cpp:1311 @ emit_signalp()
Mancante.gd:42 @ _ready()
My code:
On player node:
signal DeclarePlayer( Player : Node2D )
func _ready() → void:
DeclarePlayer.emit(self)
On recieving node:
signal PassPlayer(Player : Node2D)
func _on_mancante_declare_player() → void:
PassPlayer.emit(Player)
On second recieving node:
func _on_hud_pass_player(Player: Node2D) → void:
PlayerRef = Player
UpdateHealthBar(PlayerRef.CurrentHealth, PlayerRef.Vitalty)
Summary
This text will be hidden
I would recommend Posting guidelines in #Help channel to post better organised info about issue, in this case code.
To your topic :
1.) you can extend your player in your script and get reference to it as class, and add the node into group
class_name Player
extends CharacterBody2D
Other script
var player: Player
func _ready():
player = get_tree().get_first_node_in_group("player") as Player
2.)use export variable to get reference to node.
@export var player: Player
Assign corresponding player node in inspector on the right side.
3.) last resort use Singleton/Autoload.