How to save/load one or each object (player character Node/Class/Object)).
zegtern Essentially:
var my_node = get_node("foo") # <- First, get a reference of the node you want to save
var packed_scene = PackedScene.new() # <- Create a new PackedScene resource where your node will be stored
packed_scene.pack(my_node) # Now store your node in the resource.
# Keep in mind that it'll save your node and all subnodes that uses it as owner. Other nodes are discarded.
var error = ResourceSaver.save(packed_scene, "user://path/to/your/file.scn") # Save your resource as file
assert(error == OK) # Verify that everything went OK when saving the resource. If something went wrong, the program will halt here.
You can see more about it here: https://docs.godotengine.org/en/stable/classes/class_packedscene.html#class-packedscene-method-pack
Also, here's a good guide about saving things using File class: https://kidscancode.org/godot_recipes/3.x/basics/file_io/
- Edited
Please see the code below.
If you save a player in this code, the tempInt value is also saved, and the next time you load the saved player, the saved tempInt value is the initial 0 value, not the abnormally stored value.
//[Player.cs]
public partial class Player : Resource
{
public int tempInt { get; set; } = 0;
}
//Main.cs
public partial class Main : Node
{
public Player player;
public override void _Ready()
{
base._Ready();
player = new Player();
player.tempInt = 0;
}
public override void _Input(InputEvent inputEvent)
{
if (inputEvent is InputEventKey keyEvent && keyEvent.Pressed)
switch (keyEvent.Keycode)
{
case Key.A:
player.tempInt++;
GD.Print(player.tempInt);
break;
case Key.S:
var fooPath = System.Environment.CurrentDirectory + "\\" + "Save";
if (Directory.Exists(fooPath) == false)
{
Directory.CreateDirectory(fooPath);
}
GD.Print(player.tempInt);
ResourceSaver.Save(player, fooPath + "\\player.tres");
break;
case Key.L:
var loadPath = System.Environment.CurrentDirectory + "\\"+ "Save";
player = (Player) ResourceLoader.Load(loadPath+ "\\player.tres");
GD.Print(player.tempInt);
break;
}
}
}
AnidemDex Please see the code below.
If you save a player in this code, the tempInt value is also saved, and the next time you load the saved player, the saved tempInt value is the initial 0 value, not the abnormally stored value.
//[Player.cs]
public partial class Player : Resource
{
public int tempInt { get; set; } = 0;
}
//Main.cs
public partial class Main : Node
{
public Player player;
public override void _Ready()
{
base._Ready();
player = new Player();
player.tempInt = 0;
}
public override void _Input(InputEvent inputEvent)
{
if (inputEvent is InputEventKey keyEvent && keyEvent.Pressed)
switch (keyEvent.Keycode)
{
case Key.A:
player.tempInt++;
GD.Print(player.tempInt);
break;
case Key.S:
var fooPath = System.Environment.CurrentDirectory + "\\" + "Save";
if (Directory.Exists(fooPath) == false)
{
Directory.CreateDirectory(fooPath);
}
GD.Print(player.tempInt);
ResourceSaver.Save(player, fooPath + "\\player.tres");
break;
case Key.L:
var loadPath = System.Environment.CurrentDirectory + "\\"+ "Save";
player = (Player) ResourceLoader.Load(loadPath+ "\\player.tres");
GD.Print(player.tempInt);
break;
}
}
}
AnidemDex Hi how can you save the inventory data inside a file and load that file on the startup?
connor_jade_0199 technically, yes