Hey, this is my first project with Godot, so this may be an easy fix, however I can't seem to find it. Essentially what I am trying to do is that when the 'killzone' has touched the player, i want it to call a function within the player node, but it keeps coming up with the error of the Node not found ( see images ). I managed to apply this logic and create a functioning coin counter for the game, so i was able to call a function within another node but for some reason isn't working here. If somebody can please elaborate so that I won't make this mistake in the future. Thanks in advance. (images attached are the functioning coin system and the nonfunctioning death system)



  • xyz replied to this.
  • Eyaan I see the brackeys tutorial is taking victims.

    if the player is some global thing, manage it's demise through an autoload.
    it's a bit advanced, but:
    in the autoload, let's call it player_manager, create a signal, call it something like kill_player

    signal kill_player

    in player ready(), connect the signal to a function that kills the player. you have one called killed_(), let's use that:

    func _ready():
         PlayerManager.kill_player.connect(killed_)

    when your area triggers, call the signal in the autoload:

    func _on_body_entered(body)
         PlayerManager.kill_player.emit()

    autoloads are unique and run outside the scene. when the game starts, it creates nodes with the autoload scripts. you can access autoloads from anywhere, as they are given a unique name (the name of the file, capitalized and without spaces)
    player_manager becomes PlayerManager

    if the player is the one colliding with the coin and dead area, you don't need anything special, the function has a body parameter and that is going to be the player:

    func _on_body_entered(body):
         body.killed_()

    body is the player. you can change the names of these variables

    func _on_body_entered(player):
         player.killed_()

    if something other than the player can collide with this thing, you have to check for method first or do some other test:

    func _on_body_entered(player):
         if player.has_method("killed_"):
              player.killed_()

    Eyaan Scene unique node names are only valid inside the scene they're defined in.

      xyz Thanks for the reply, however (this may sound a bit dumb) how is it i am able to link the UI with the 'coins' using the unique names. Also when not using the unique names, i still ran into this problem.

        Eyaan I see the brackeys tutorial is taking victims.

        if the player is some global thing, manage it's demise through an autoload.
        it's a bit advanced, but:
        in the autoload, let's call it player_manager, create a signal, call it something like kill_player

        signal kill_player

        in player ready(), connect the signal to a function that kills the player. you have one called killed_(), let's use that:

        func _ready():
             PlayerManager.kill_player.connect(killed_)

        when your area triggers, call the signal in the autoload:

        func _on_body_entered(body)
             PlayerManager.kill_player.emit()

        autoloads are unique and run outside the scene. when the game starts, it creates nodes with the autoload scripts. you can access autoloads from anywhere, as they are given a unique name (the name of the file, capitalized and without spaces)
        player_manager becomes PlayerManager

        if the player is the one colliding with the coin and dead area, you don't need anything special, the function has a body parameter and that is going to be the player:

        func _on_body_entered(body):
             body.killed_()

        body is the player. you can change the names of these variables

        func _on_body_entered(player):
             player.killed_()

        if something other than the player can collide with this thing, you have to check for method first or do some other test:

        func _on_body_entered(player):
             if player.has_method("killed_"):
                  player.killed_()

          Jesusemora
          Thanks for the reply, your solution worked! Now I'm getting the correct function running without any errors. However the whole point of this function was in order to play a death animation, which is not occurring, it still either plays the idle animation or the running animation. I think this may be due to how the code is set up. I was wondering if you could suggest a way to fix this? The way i went about this is with if and elif statements, and in my head i thought it would run through the if statement first, and if fulfilled it will not run through the others? (Don't worry i didn't forget to define the variable) Thanks in advance.


            Eyaan use states for the player.

            #0 would be idle
            #1 could be swim, climb, etc
            #2 death
            var state : int = 0

            in your main function, _process if you are using process or _physics_process, or both, check the states with match:

            func _physics_process(delta):
                 match state:
                      2:
                           #death
                           animated_sprite.play("death")
                      _: #_: is the same as 0, or any number not being checked, it's like an else
                           #idle
                           if is_on_floor():
                                if direction == 0:
                                     animated_sprite.play("idle")
                                else:
                                     animated_sprite.play("run")
                           else:
                                animated_sprite.play("jump")

            when you run killed_, it should change the number. adding 1 would just cause it to accumulate and it would no longer be 1, in which case you should check if dead > 0 or dead != 0. but with states you just assign the state:

            func killed_():
                 state = 2

            and then when trying to enter a different state, like going into climbing, you check if the state is not 2.

              Jesusemora Thanks this worked perfectly, however now i think I'm going to take a crash course in GDscript before i continue on with attempting at making games. 😅