Last active
April 18, 2025 14:36
-
-
Save digitalbreed/81736e63f86300b6bed9cd61a930f31c to your computer and use it in GitHub Desktop.
Simple Game Creator 2 instruction to store and restore state of game objects with "Remember" components. Use with caution, data is stored in static variable without any lifecycle management.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using GameCreator.Runtime.Common; | |
using GameCreator.Runtime.VisualScripting; | |
using UnityEngine; | |
using System.Collections.Generic; | |
[Title("Store or Restore State")] | |
[Category("Custom/State Management")] | |
[Description("Stores or restores the state of all objects with a Remember component, using an optional root object and filter component.")] | |
[Parameter("Mode", "The mode of operation, store or restore.")] | |
[Parameter("Root", "The root game object to scan from in case of storing; if null, the whole scene is searched. Only used for storage.")] | |
[Parameter("RequiredComponentType", "A required component type that the stored game objects need to have, e.g. Character; if null, no filtering is applied. Only used for storage.")] | |
[Parameter("clearAfterRestore", "Indicates whether the storage should be cleared after restore. Only used for restore.")] | |
[Image(typeof(IconDiskSolid), ColorTheme.Type.Blue)] | |
[Keywords("Save", "Load", "Memory", "Remember")] | |
[Serializable] | |
public class InstructionCommonStoreRestoreState : Instruction { | |
private enum Mode { | |
Store, | |
Restore | |
} | |
[SerializeField] private Mode mode = Mode.Store; | |
[SerializeField] private PropertyGetGameObject root = new(); | |
[SerializeField] private TypeReferenceBehaviour requiredComponentType = new(); | |
[SerializeField] private bool clearAfterRestore = true; | |
private static Dictionary<GameObject, object> storedStates = new(); | |
public override string Title => $"{mode} state"; | |
protected override async Task Run(Args args) { | |
if (mode == Mode.Store) { | |
GameObject rootObj = this.root.Get(args); | |
Remember[] rememberComponents = rootObj != null ? rootObj.GetComponentsInChildren<Remember>(true) : UnityEngine.Object.FindObjectsByType<Remember>(FindObjectsSortMode.None); | |
storedStates = new(); | |
foreach (Remember remember in rememberComponents) { | |
if (requiredComponentType.Type != null) { | |
if (remember.GetComponent(requiredComponentType.Type) == null) { | |
continue; | |
} | |
} | |
storedStates[remember.gameObject] = remember.GetSaveData(true); | |
} | |
} else if (mode == Mode.Restore) { | |
foreach (KeyValuePair<GameObject, object> kvp in storedStates) { | |
if (kvp.Key == null) { | |
continue; | |
} | |
if (kvp.Key.TryGetComponent(out Remember remember)) { | |
await remember.OnLoad(kvp.Value); | |
} | |
} | |
if (clearAfterRestore) { | |
storedStates = new(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment