I am thinking and looking for ways to save game data by c#. As a result, there seems to be a way to code and store each key and value using JSON, and a way to use resources. But I don't know how to use it after thinking about what's wise.
What I ultimately want to do is to save the game, save and load the entire player character object (class/node). I wonder which way is the best.
=> How to save/load one or each object (class/node). (c# use)
So, you want to save things in Godot.
This is my general advice over this topic is this post.
Objects' properties contains a lot of information that you may find useful when you're writing things to save other project things.
Something good about using resources (and File/ConfigFile) is that the thing that writes it to disk is aware about that information, and try to save it with the included data, something that you has to do manually with custom formats (like JSON).
Generally speaking, I don't use JSON when is not necessary (like, when I don't need to transfer data between two apps), I prefer YAML or INI style format (and is a personal thing, is ok if you use whatever you want to use).
Edit: Forgot to upload the pic that is on the post
Hi, I'm also in the middle of creating my save and load system in c# and was wondering what path I should choose Json or resources... What exactly do you want to save and load? I didn't get your question. Can you please elaborate?
So, you want to save things in Godot.
This is my general advice over this topic is this post.
Objects' properties contains a lot of information that you may find useful when you're writing things to save other project things.
Something good about using resources (and File/ConfigFile) is that the thing that writes it to disk is aware about that information, and try to save it with the included data, something that you has to do manually with custom formats (like JSON).
Generally speaking, I don't use JSON when is not necessary (like, when I don't need to transfer data between two apps), I prefer YAML or INI style format (and is a personal thing, is ok if you use whatever you want to use).
Edit: Forgot to upload the pic that is on the post
Baroja I want to save the player character object.
The object could also be a node or another class of objects.
Friedman Almost everything is an object, your player is a node, so is an object. According to the posted flow diagram, you may be benefit of using PackedScene
to store your player node (or even the current played scene?). All relies on "what you want to save?"
Heartbreaker I want how to save Node by using PackedScene and Resource methods.
Friedman 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/
Heartbreaker
I want to save to hard disk(later load) .......this two red circle
Friedman The packedscene thing includes the script
Heartbreaker Please take to c#.
Heartbreaker I also want to know how to use resources.
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;
}
}
}
Heartbreaker 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;
}
}
}
Heartbreaker Hi how can you save the inventory data inside a file and load that file on the startup?
© 2022 - 2024 — McMap. All rights reserved.