feature/save_and_loaaaaaad #32
@@ -2,6 +2,8 @@ namespace Babushka.scripts.CSharp.Common.Savegame;
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines the behaviour of Nodes that have fields that should save / load to disk.
|
/// Defines the behaviour of Nodes that have fields that should save / load to disk.
|
||||||
|
/// When implementing new Saveable objects, please beware: Please check if object instances need to be identified separately.
|
||||||
|
/// If so, make sure to give them a proper ID. You can use the <see cref="SaveIDProvider"/> tool for that.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface ISaveable
|
public interface ISaveable
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,9 +4,16 @@ using Godot.Collections;
|
|||||||
|
|
||||||
namespace Babushka.scripts.CSharp.Common.Savegame;
|
namespace Babushka.scripts.CSharp.Common.Savegame;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// And editor tool that lives in the scene scope and iterates over saveable nodes and assigns them unique IDs where necessary.
|
||||||
|
/// It only works if the object (prefab) in question has been added to the "Saveable"-group beforehand.
|
||||||
|
/// </summary>
|
||||||
[Tool]
|
[Tool]
|
||||||
public partial class SaveIDProviderTool : Node
|
public partial class SaveIDProviderTool : Node
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an inspector button that calls the AssignIDs method.
|
||||||
|
/// </summary>
|
||||||
[ExportToolButton("Assign IDs")] private Callable assignIDs => Callable.From(AssignIDs);
|
[ExportToolButton("Assign IDs")] private Callable assignIDs => Callable.From(AssignIDs);
|
||||||
|
|
||||||
private void AssignIDs()
|
private void AssignIDs()
|
||||||
@@ -15,7 +22,6 @@ public partial class SaveIDProviderTool : Node
|
|||||||
foreach (var node in saveables)
|
foreach (var node in saveables)
|
||||||
{
|
{
|
||||||
GD.Print($"Checking {node.Name}.");
|
GD.Print($"Checking {node.Name}.");
|
||||||
GD.Print($"Node has Meta SaveID: {node.HasMeta("SaveID")} and it's: " + node.GetMeta("SaveID").AsString());
|
|
||||||
if (!node.HasMeta("SaveID") || string.IsNullOrEmpty(node.GetMeta("SaveID").AsString()))
|
if (!node.HasMeta("SaveID") || string.IsNullOrEmpty(node.GetMeta("SaveID").AsString()))
|
||||||
{
|
{
|
||||||
string saveID = Guid.NewGuid().ToString();
|
string saveID = Guid.NewGuid().ToString();
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ namespace Babushka.scripts.CSharp.Common.Temp;
|
|||||||
public partial class MVPDuck : Node2D, ISaveable
|
public partial class MVPDuck : Node2D, ISaveable
|
||||||
{
|
{
|
||||||
[ExportGroup("Persistence")]
|
[ExportGroup("Persistence")]
|
||||||
|
// A container for the current scene. Used to automatically add the current scene context to the save ID.
|
||||||
[Export] public VariableResource _sceneKeyProvider;
|
[Export] public VariableResource _sceneKeyProvider;
|
||||||
|
|
||||||
[ExportGroup("Animation")]
|
[ExportGroup("Animation")]
|
||||||
@@ -56,6 +57,9 @@ public partial class MVPDuck : Node2D, ISaveable
|
|||||||
|
|
||||||
#region SAVE AND LOAD
|
#region SAVE AND LOAD
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
// Saves duck position.
|
||||||
|
/// </summary>
|
||||||
public void UpdateSaveData()
|
public void UpdateSaveData()
|
||||||
{
|
{
|
||||||
var payloadData = new Dictionary<string, Variant>
|
var payloadData = new Dictionary<string, Variant>
|
||||||
@@ -69,6 +73,9 @@ public partial class MVPDuck : Node2D, ISaveable
|
|||||||
SavegameService.AppendDataToSave(_sceneKeyProvider.Payload.AsString(), id, payloadData);
|
SavegameService.AppendDataToSave(_sceneKeyProvider.Payload.AsString(), id, payloadData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads duck position.
|
||||||
|
/// </summary>
|
||||||
public void LoadFromSaveData()
|
public void LoadFromSaveData()
|
||||||
{
|
{
|
||||||
var sceneName = _sceneKeyProvider.Payload.AsString();
|
var sceneName = _sceneKeyProvider.Payload.AsString();
|
||||||
|
|||||||
Reference in New Issue
Block a user