- Edited
I'm so frustrated, i can't seem to figure out how to work with signals when it comes to scenes instanciated from code in GODOT 4. I tried all sorts of things, but cannot seem to grasp my head around it.
I can fine figure out how to connect and listen to signals when the scenes are present in the game tree. It's driving me mad, I searched a lot in forums but with no luck. So i final gathered the courage to write in this forum. So here we go:
I have a Game scene with different scenes it in.
-I have a SkeetShooter scene which throws skeets for the player to shoot.
--The SkeetShooter scene instanciates skeets from code (bacially a enemy in other terms)
--When the bullet (which are also instanciated from code) hits the skeet this function is called
func _on_body_entered(body):
print("bullet_area2d are hitting: ", body.name)
#destory bullet
queue_free()
The skeet has the following signal and emition of it when it destoyed
signal increaseScore
func _exit_tree():
print("skeet destoryed")
increaseScore.emit()
I have GUI scene which i want to listen to the increaseScore.emit() which are triggered in the skeet scene/node when it's destoyed.
This is the part i cannot figure out. How to i listen to signal emitted by skeets?
I might be worth telling that i can have as many skeets in a scene there can be 1 and there can also be 20.
extends CanvasLayer
var score = 0
func _ready():
update_score_label()
$ScoreLabel.text = str(score)
#how to i dynamically connect and listen to all skeet which are instanciated when they are generated on the fly
connect("increaseScore", on_listen_to_skeet_increasce_score_emit)
func _process(delta):
pass
func update_score_label():
score = score + 1
print("we run the update score function in the gui scene")
$ScoreLabel.text = str(score)
#i want this function to be called every time any instanciated skeet are emitting the increaseScore.emit() from the skeet script
func on_listen_to_skeet_increasce_score_emit():
update_score_label()
print("we have caught the skeet increaseScore SIGNAL!!!")
I want to add if it was a normal program written in python, C# or any other laungauge i would just add a reference in the skeet script to the "gui" object and then call the function from the skeet. But as i read it and understand it, godot aims not to create to many dependencies and therefore work with signals. If thats not the case I'm also open for how to add a reference from the skeet script to the "GUI" scene and call the function from there if thats a better solution and someone have suggestion for implementation of that.
Hope someone can help me figure this out . Since I'm not ready to loose my sanity
Update after post: Also the "code blocks" in this post dosen't seem to work for me. Im using the ``.. so im sorry for making this hard to read..It's my first post......
// Lars