0 votes

I'm adding more features to the "Your First Game". Currently I'm trying to increase the score when a bullet collides with a creep.

My initial attempt was to emit a custom signal from within the Bullet scene and then connect that to a function inside the Main scene. Almost none of the signals are detected (it did seem to detect once in a while, is that a bug?), so I concluded that it's because the bullets don't exist initially.

I currently am exporting the Bullet PackedScene into the Player scene.

Do you need to set up a listener for each Bullet child instance (is that possible)? Or is there just a flaw in my structuring?

asked May 12 in Projects by rseonp (12 points)

1 Answer

0 votes

It probably would be easier to help you, if you provided any code.

That being said, if "the children don't exist at the start", I assume you instance them from code during runtime. That's where you should connect the signal as well:

var BULLET_SCENE = preload("res://Bullet.tscn")

func spawn_bullet():
    var new_bullet = BULLET_SCENE.instance()

    new_bullet.connect("fired", self, "_on_fired")

    # you can provide an array of extra arguments to the callback 
    new_bullet.connect("hit", self, "_on_hit", [new_bullet])

    add_child(new_bullet)

func _on_fired():
    print("BANG BANG BANG")

func _on_hit(bullet):
    print("OUCH! Something was hit by ", bullet)
answered May 12 by njamster (9,820 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.