How to save/load one or each object (player character Node/Class/Object)).
Asked Answered
F

13

0

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)

Friedman answered 21/10, 2022 at 12:6 Comment(0)
H
0

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

Heartbreaker answered 21/10, 2022 at 23:13 Comment(0)
B
0

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?

Baroja answered 21/10, 2022 at 18:49 Comment(0)
H
0

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

Heartbreaker answered 21/10, 2022 at 23:13 Comment(0)
F
0

Baroja I want to save the player character object.
The object could also be a node or another class of objects.

Friedman answered 21/10, 2022 at 23:19 Comment(0)
H
0

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 answered 21/10, 2022 at 23:22 Comment(0)
F
0

Heartbreaker I want how to save Node by using PackedScene and Resource methods.

Friedman answered 21/10, 2022 at 23:23 Comment(0)
H
0

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 answered 21/10, 2022 at 23:31 Comment(0)
F
0

Heartbreaker
I want to save to hard disk(later load) .......this two red circle

Friedman answered 21/10, 2022 at 23:38 Comment(0)
H
0

Friedman The packedscene thing includes the script

Heartbreaker answered 21/10, 2022 at 23:39 Comment(0)
F
0

Heartbreaker Please take to c#.

Friedman answered 21/10, 2022 at 23:40 Comment(0)
F
0

Heartbreaker I also want to know how to use resources.

Friedman answered 21/10, 2022 at 23:41 Comment(0)
F
0

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;

            }
    }

}
Friedman answered 22/10, 2022 at 0:3 Comment(0)
F
0

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;

        }
}

}

Friedman answered 22/10, 2022 at 0:16 Comment(0)
P
1

Heartbreaker Hi how can you save the inventory data inside a file and load that file on the startup?

Pharmaceutics answered 16/3 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.