How do i reset a resource

Godot Version

4.2.2

Question

how do i reset a resource

I’m using resources to make save files. I want to know how I could say, have a new game button that resets everything back to it’s original state/

If you have default values you can create a new resource to override.

my_save_resource = SaveResource.new()

Im pretty sure im doing this wrong

func _on_yes_pressed(): #loads the game and clears the save file
	PlayerData == PlayerData.new()
	get_tree().change_scene_to_file("res://scenes/game/main.tscn")

When I load the scene, the resource is the same.

Can you show the resource code, that loads/saves the game. Is it a custom resource?

1 Like

A few things wrong with this.

  1. Two equal signs is a comparison, you probably have a warning along the lines of “statement has no effect”. You mean to use one equal sign, but
  2. PlayerData must be a type for .new() to work, so it cannot be assigned to itself

If you have a global/autoload for player data then you must mean to use

Global.player_data = PlayerData.new()

Yes, it is a custom resource

so in the resource script i have

extends Resource
class_name PlayerData

I also have a bunch of variables here that save

In the game and title screen i have this

var save_file_path = "user://save/"
var save_file_name = 'PlayerSave.tres'
var playerData = PlayerData.new()

for the button that creates a new save file

func _on_yes_pressed(): #loads the game and clears the save file
	#code for reseting would go here.
	get_tree().change_scene_to_file("res://scenes/game/main.tscn")

Not sure how your game saves yet. But I would do the following:

Make/Save the PlayerData resource to default.tres file. When the player initiates a new game, copy/overwrite from the default.tres file to a player.tres file and use that by .load() going forward, Your default.tres remains intact.

Now, once the player wants to reset, all you have to do is erase the player.tres and repeat the above procedure.

Here is some rough code that can help you with the basics. All it does is have a default.tres set with the @export variables, and when executes it makes a copy to the working config.tres. Sorry I have lots of error checking.

extends Resource
class_name PlayerData
@export var user_id: int = 0
@export var user_pref: String ="defaults"
@export var other_stuff: String =""
# ...
pass

Call this method below. This will open existing or create a new resource from the default.
All you would need to supply if you ever wanted to reset, is a function to delete the conf_resorce.resource_path file.

var user_path = "user://save/"
var default_config= "default_config.tres"
var player_config = "PlayerSave.tres"
var res=0

var default_conf_resource: PlayerData
var conf_resource: PlayerData

func open_conf_resource() -> int:
	res=0
	var def_conf_path = user_path.path_join(default_config)
	if ResourceLoader.exists(def_conf_path):
		default_conf_resource=load(def_conf_path)
	else:
		res=-1
		return res
	
	var conf_path = user_path.path_join(player_config)
	var conf_folder = conf_path.get_base_dir()
	
	if !ResourceLoader.exists(conf_path):
		res=DirAccess.make_dir_recursive_absolute(conf_folder)
		if res != 0:
			return res		
		res=ResourceSaver.save(default_conf_resource, conf_path)
		if res != 0:
			return res
	
	conf_resource=load(conf_path)
	return res

Note this is rough code. You should understand each line before you use it.

I ended up just deleting the file and creating a new one. and that worked

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.