error: Invalid call. Nonexistent function 'add_coin' in base 'StaticBody2D'

reapersremorsereapersremorse Posts: 4Member

im following a tutorial made by BornCG on youtube.

My code is broken. ive set everything up the exact same way BornCG has it. yet i keep getting the same error.
Invalid call. Nonexistent function 'add_coin' in base 'StaticBody2D'.

when i saw that i tried calling the node script itself and that did not work either.

coin script: https://github.com/reapersremorse/RR_2D_Platformer_Tutorial_OpenGL2.0/blob/master/Coin.gd
steve script: https://github.com/reapersremorse/RR_2D_Platformer_Tutorial_OpenGL2.0/blob/master/reapersremorse/assets/Characters/Steve.gd

if anyone can tell me how i screwed up, please let me know.

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 2,847Admin

    Welcome to the forums @reapersremorse!

    The issue is probably that the coin is colliding with a physics body that is not the player, leading it to crash as it tries to call add_coin on a node that doesn't have the function available. Given it says it's happening to a StaticBody2D, my guess is that it's a TileMap it's colliding with.

    Regardless, you just need to check if body has the function you are looking for before calling it and it should work. So, in Coin.gd, you'll need to change the _on_Coin_body_entered function to the following:

    func _on_Coin_body_entered(body):
        if (body.has_method("add_coin"):
            $AnimationPlayer.play("Coin_Bounce")
            body.add_coin()
    

    Then it should only call the function on nodes that have the function :smile:

Leave a Comment

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