Error: Nonexistent function call in Autoload

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AlejandroG

Hi everyone,

I’m trying to make a simple tile-movement-based game as I’m still learning Godot. I’m trying to have the Player node call a function on an Autoload named GameGod whenever a keyboard key is pressed. I defined this function myself in the Autoload (it’s called player_moved()).However, when the Player node calls it, the engine tells me the function doesn’t exist. Here’s the code for the Autoload:

extends Node
var PlayerScore = 0


func _ready():
    pass

func player_moved():
    print ("Player has moved")
    $Robot1.move()

Here’s the bit of code containing the player’s Node call on the Autoload:

extends Node2D

var speed = 128
var tile_size = 32

var last_position = Vector2()
var target_position = Vector2()
var movedir = Vector2()
var is_moving_diagonally = false

onready var ray = $RayCast2D

func _ready():
    position.snapped(Vector2(tile_size,tile_size))
    last_position = position
    target_position = position

func _input(event):
    if event is InputEventKey && event.is_pressed():
	    GameGod.player_moved()
#(Code continues...)

This is the error I get:

Invalid call. Nonexistent function 'player_moved' in base 'Node (GameGod.gd)'.

I’m obviously doing something wrong, but I’m still coming to grips with the engine and can’t find the problem. I’d greatly appreciate any comments on this. Thanks!

HI,
have you reloaded the projekt? Autoload objects dont update on save … i believe

klaas | 2020-08-22 22:49

I tried reloading but didn’t work. However, I hadn’t thought about the fact that autoloads don’t update upon saving scenes, which is useful to know. Thanks!

AlejandroG | 2020-08-23 23:30

:bust_in_silhouette: Reply From: AuthorSan

This should work. I don’t see anything wrong with the code. Try removing and adding back your singleton script to the autoload.

Great! I deleted the autoload from the project settings manager and added it again with the same script - I think that’s what you meant - and now it works fine. No error thrown. So it means that every time I add a function or otherwise modify the autoload singleton script I’m meant to delete and reload the autoload? Seems awkward.

Thanks for the advice!

AlejandroG | 2020-08-23 23:28

Yes, that’s what I meant. But there is no need for deletion and reload of the autoload after updating the script. What you are encountering is probably a Godot’s internal bug. Try adding another function to test and see if the issue persists. If it does, then report the bug to the Godot’s development community and create a new project and move your files to that project.

AuthorSan | 2020-08-24 08:00

Great, will do. Thanks.

AlejandroG | 2020-08-24 22:34