How do you handle running loops and instantiated objects when saving your game?
I'm working on a UI oriented clicker type game where I run coroutines in the background to instantiate objects (upgrades, achievments, etc.) in a specific order.
Currently the game writes a binary save file with variable values such as gold count, playtime etc. But my problem is saving the current state of the spawn loops, and letting the game know which objects should already be instantiated when the game loads.
An example of one of my spawner scripts looks like this:
IEnumerator SpawnQueue()
{
for (int i = 0; i < workers.Length; i++)
{
while (true)
{
if (game.gold >= activeWorkers[i - 1].cost) // Spawn next worker when you can afford the current one
{
SpawnWorker(workers[i].name, workers[i].description, workers[i].cost, workers[i].icon, workers[i].upgrades, i);
break;
}
yield return null;
}
}
print("All workers spawned, ending loop...");
}
where the game contains one "worker" prefab which instantiates and sets its variables for each instance with each iteration in the loop.
Can i properly continue a coroutine like this and simulate previous steps in the loop when loading a game, or do I need to rethink how to instantiate objects?
Your answer
Follow this Question
Related Questions
How do I load the game? 0 Answers
How to SAVE to user defined file Name, and then Load any other saved game by user Selection 1 Answer
Coroutine not working on android build 1 Answer
How to wait for a function to complete before continuing? 0 Answers
Detect when a key is pressed or not pressed using Corutine? 0 Answers