I'm tryin to code a save system for my project, and I wanted it to be accessible from any code anywhere so I made a script "SaveGame.gd" and in project settings made it an auto load called "Saver". The code in it goes like this

class_name SaveGame
extends Resource

const save_base_path := "user://save.save"
var lastSave

func save_to_file():
	var save_dict = {
		"lastTime" : GlobalResources.lastTime
	}
	var save_game = FileAccess.open("user://save.save", FileAccess.WRITE)
	save_game.store_var(save_dict)
	print("saved")
	
func load_save():
	var save_game = FileAccess.open("user://save.save", FileAccess.READ)
	var data = save_game.get_var()
	lastSave = data.lastTime
	print("loaded")

it fetches data from another autoload to create a test savefile, then in my main scene I have a script (main.gd) that when i press a button calls those functions, the part that does that goes like this:

func _process(delta):
	if Input.is_action_pressed("ui_text_backspace"):
		Saver.save_to_file()	
	if Input.is_action_pressed("ui_up"):
		Saver.load_save()

but whenever I press the buttons that call those functions I get that dreaded Invalid call. Nonexistant function 'save_to_file' in base 'Nil'. . And so I made a test, moved the said save and load functions to main.gd and they JUST WORKED. So I am baffled, read a lot of stuff on the web already and I'm beginning to lose my mind! My project depends on my capacity to learn to create this save system, but I just cant seem to make it work as an autoload (or singleton as some say). There are no misspellings as I thorougly verified the name of the autoload that just have 5 letters and the editor even autocompletes the functions for me after i type in the "."

Thanks in advance to anyone that hears my plea, I'll give any extra info necessary

  • I just noticed you're using Godot 4. This may be different in Godot 4, but in Godot 3, for an autoload, I would expect the top of the script to be:

    extends Node

    rather than:

    class_name SaveGame
    extends Resource

    In Project Settings >> Autoload, the autoload's Path would be res://SaveGame.gd and Node Name would be Saver, based on your previous post.

First, change Input.is_action_pressed() to Input.is_action_just_pressed().

_process() can be called every few milliseconds, and you don't want the file saves and loads to be preformed repeatedly while a key is pressed.

Then report if the problem is still present.

Also, please place ~~~ before and after the code so that it will be displayed properly here.

    Try putting a breakpoint at this line:
    Saver.save_to_file()

    When the breakpoint is hit, view the Remote tab in the Scene dock and verify that the node Saver is present.

      DonSpaghetti I though the "insert code" would work

      It should indeed, but whether it adds ``` (code block formatting) or ` (inline code) depends on how you've highlighed the code.

      DaveTheCoder it gave me this error
      E 0:00:01:0666 start: Script does not inherit from Node: res://save logic/SaveGame.gd.

      I just noticed you're using Godot 4. This may be different in Godot 4, but in Godot 3, for an autoload, I would expect the top of the script to be:

      extends Node

      rather than:

      class_name SaveGame
      extends Resource

      In Project Settings >> Autoload, the autoload's Path would be res://SaveGame.gd and Node Name would be Saver, based on your previous post.

        DaveTheCoder dude....... I deleted the script and created a new one, one that extends Node. AND IT WORKED, think I got the idea while watching the gdquest tutorial on how to make a save system (wich I did not quite grasp all of the concepts), that they taught how to do it using resources, and so I made it to fit the resource thing. Thanks a bunch!!! Very happy, wish you the best of weekends