So out of curiosity and also to drill the approach for memory, I was trying to connect the “took_damage” signal to the Game scene from the script. This is my code thus far:
|extends Node2D
|
|var lives = 3
|
|@onready var player = $Player
|
|func _ready():
| player.took_damage.connect(_on_player_took_damage)
|
|func _on_player_took_damage():
| lives -=1
This seems to work. But Kaan seems to be connecting this signal from the Player scene directly to the Game scene (rather than using the player instance). I tried to do this manually with the code:
|@onready var game = $"."
|
|func _ready():
| game.took_damage.connect(_on_player_took_damage)
|
|func _on_player_took_damage():
| etc. etc.
Now besides just making less sense to me, this also just doesn’t work! I receive the error: “Invalid get index ‘took_damage’ (on base: Node2D (game.gd)’).” which actually makes total sense - why would the Game scene be able to respond to the took_damage signal, and why would I want it to? I’m unsure why the Game node is showing up as “.” when I drag it into the script, but I assume it has something to do with it being the root of the scene?
I guess my confusion here probably stems from not quite understanding how connecting signals through the node menu actually works under the hood. My question is: when Kaan connects the “took_damage” signal, is he actually connecting directly to the game scene? If so, is there anything wrong with connecting to the player instance? Is there a correct way to connect to the Game scene with code?
Just trying to understand a bit more what’s actually happening behind the scenes here, thanks!