alright this has been puzzling me ever since i started learning godot. usually i just emit a custom signal and connect it to the parent/child scene via the inspector. but in this case i want to create a signal within the enemy script that is emitted every time the enemy dies, preferably connected to the player script to get some sort of a leveling system going. but since the enemy is spawned in to the main scene at runtime, and not there in the first place (unlike the player's scene) im not sure how i can connect the two of them.

i hear you can connect signals via script with the connect() function, but im still not sure how those work both conceptually and syntactically. it's definitely not as simple as connecting the signal straight from the inspector. can someone help me out with this? also would like to hear some other ideas to handle player leveling up/gaining xp when killing enemies

    xyz in that tutorial, they still needed to connect the node via @onready = get_node() function
    can i still do that eventhough the node is on a different scene?

    • xyz replied to this.

      wokesmeed At runtime there are really no different scenes. Everything is just one big scene tree. Once you instantiate a scene its nodes just become a regular part of the main tree. So you can access every node via its proper node path and make needed connections.

      wokesmeed easy, connect the desired signal on your enemy when it is instanced to the player (or node that is handling stats) and then tie your "leveling up" logic to that signal. You could even make a custom signal on the enemy that, say, when it is killed, transfers a value to the player to "level up" (though full honesty, you may want to use a global set of variables for stats, as they won't transfer from scene to scene without some sort of "container.") That "level up" or "xp" function could be something you plug into an Area2D signal (like say, when a weapon hits an enemy). Note: This logic in this example is faulty. You don't want to connect this to tree_exiting normally because then you'd trigger your level up/xp function every time you change scenes (because the enemies leave with it).

      But if you wanted to connect the signal with a "handler" node, you could just drop a Node2d in the scene and seek out your objects. The other easy option is to simply hide your enemy nodes until you need them and simple hide them and connect your xp function to when they "disappear."
      This is a broad example, hard to consider implementation without code in front of me. But the example should teach how to connect signals in code at an instancing.

      var _parentScene = get_parent() ## get parent scene
      var _enemy ## empty container for enemy
      export var _enemySource : String ## export so you can paste enemy filepath for instance into inspector
      onready var _player = $Player ## find player in scene
      
      func _on_enemy_Spawn():
              var _newEnemy = _enemySource.instance() ## we instance the enemy
              _parentScene.add_child(_newEnemy) ## add it to parent scene so it can move on its own
              _enemy = _newEnemy ## fill our enemy container with our newly instanced target
              _enemy.connect("tree_exiting", _player, "_enemy_xp_acquire") ## and connect. 

      '_enemy.connect()' is the function, with _enemy being your connecting object and in the parantheses, three ## arguments

      • the signal you want to connect
        the target to connect to
        and the function to trigger in the target - in that exact order.

      Then you could make a function in the player for "_enemy_xp_acquire" and put your logic there. You may want to play with different signals and functions to get it right. Good luck!