EDIT: Asked the wrong question, the actual question is how to use get_node on a child that is created via scripting and not in the scene prior. I think it needs to be done in my main scene and in the _ready function but I can't figure out where to go from there.

Hi all! I'm sorry if this has been asked before I've been trying to find an answer for a bit now. I'm very new to Godot and GDScript (It's maybe been a week). I've read over the Godot documentation on signals and I'm not sure if I'm just missing something or what.

I'm working on a logic puzzle game (kind of like the app Logic Riddle except with a fantasy theme). I'm having some issues figuring out how to use signals in a node that is created using scripts.

What I'm trying to do is access the pressed signal on a button after the button has been created. Right now I have a Control node with a child GridContainer and the buttons are created via script and put as children of the GridContainer when the ready function is called. I can't figure out how (or even if its possible) to use the signals in those created children to do something else (what I want to do is display the texture on another node when the button is pressed). If that is possible would it go on the main scene or on the control node?

I'm not sure how much additional information might be needed to help me, so please let me know if I need to provide for information

  • xyz replied to this.
  • Kuukrow Well the variable arrow in your example contains a reference to the node. It's exactly the same thing you'd get from get_node(). So you can store that reference in another object's property and thus make it "known" to the script attached to that other object. Or you can make arrow itself a property of the script that created the node, and let the other script read the value of that property.

    Also the node path is kinda implicit. If you name the node by setting its name property and add it as a child of some other node whose path you know, the new node's path will be [parent_path]/node_name

    The node path becomes valid as soon as you add the node to the scene tree. So if the code that creates the node runs before the code that tries to get the node via its path, everything will be fine.

    Kuukrow changed the title to How to use get_node on a child that is created through scripting .

    xyz Thank you for your response! I think I maybe asked the wrong question, I've retitled the post to better reflect the question. I did read that, but I don't understand how I'm supposed to get the node that doesn't exist yet in the script. From trying to puzzle it out, I think it needs to be done in my main scene and in the _ready function but I'm not sure where to go beyond that because the documentation only covers it witch a node that already exists, unless I'm reading that whole thing incorrectly which is certainly possible.

    • xyz replied to this.

      Kuukrow You can create new nodes by calling new() on a node class:

      var my_new_node = Node3D.new()

      However in order for the created node to be processed and rendered, you need to add it to the scene tree by parenting it to another node that's already in the scene tree:

      some_other_node.add_child(my_new_node)

      Again, this is all well described in the documentation
      https://docs.godotengine.org/en/4.2/tutorials/scripting/nodes_and_scene_instances.html

        xyz Thank you for your response again! I'm probably still not explaining myself very well. I know how to do that, my script looks something like this in the GridContainer:
        arrow = TextureButton.new()
        add_child(arrow)
        arrow.texture_normal = arrows[val]
        arrow.texture_pressed = arrows_pressed[val]

        Thats obviously truncated, So in my main in my puzzle scene how do I reference the node that is created in this code? I cant just get_node because it wants a path that doesn't exist yet and won't exist until the script runs.

        • xyz replied to this.

          Kuukrow Well the variable arrow in your example contains a reference to the node. It's exactly the same thing you'd get from get_node(). So you can store that reference in another object's property and thus make it "known" to the script attached to that other object. Or you can make arrow itself a property of the script that created the node, and let the other script read the value of that property.

          Also the node path is kinda implicit. If you name the node by setting its name property and add it as a child of some other node whose path you know, the new node's path will be [parent_path]/node_name

          The node path becomes valid as soon as you add the node to the scene tree. So if the code that creates the node runs before the code that tries to get the node via its path, everything will be fine.