Dynamic connect to signals

enickenick Posts: 3Member

I'm new to Godot and while building my first game, i'm coming across one problem.
As a first project, i'm building a temple-run like game, where my world is generated while the player proceeds. New parts are generated and old parts are deleted.
Some of these parts contain area nodes which are assigned to the group obstacle.

In my player scene, i want to connect to all of these area nodes body_entered signal.

What i tried is:

func _physics_process(delta):
    for obstacle in get_tree().get_nodes_in_group("obstacle"):
        obstacle.connect("body_entered", self, "_entered_obstacle")

But this results in an Error:
ERROR: connect: Signal 'body_entered' is already connected to given method '_entered_obstacle' in that object.
And the function _entered_obstacle is never triggered.

What is the best way to do this?

Comments

  • MegalomaniakMegalomaniak Posts: 2,738Admin
    edited July 29

    Why would you actively do this on every physics frame? I think doing it in _ready() of each of the nodes might be just enough since it would need do be called only once per each of these obstacle nodes. No?

    Edit: Also so long as you haven't confirmed your account your posts will keep going into the moderation queue.

  • enickenick Posts: 3Member

    Thank you for your answer!

    Because these objects are changing on nearly every frame, so when i do it in the ready function, maybe there isn‘t even one object to connect with.

    Or do you mean i connect the signal in the script of these nodes and then connect it to the player?
    So in my obstacles gdscript in _ready()?
    How would i adress a function in my player?
    I also thought this would be bad practice, since the obstacle node depends on the player, and i thought in godot you always try to seperate the nodes so easy would work individually.

  • MegalomaniakMegalomaniak Posts: 2,738Admin

    Yeah, I meant connect the signal in the obstacle objects. ready is called when the node becomes ready so it should be good for this, however it is also assuming that the player is already in the scene and ready too.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file