Hello,
I am currently programming an idle game and have the following problem:
In my scene there will be a generator (prefab) from the beginning, which generates money and can be updated. And you can buy new generators. For this purpose, a generator prefab is instantiated and added to the list “Generators”, in which the generator from the beginning is already in. Now i want to save and load the game, when the Player leaves / close the game / app and later come back.
How do I get that exactly?
I have to collect the datas (int UpgradeLevel, int Multipliers and float LastTick (to calculate offline progression)) from each generator in the List or in the scene with a foreach loop and save the datas in a new list, which i save in a json-file. right? And later i instantiate the number of generators in the list and assign them the values from the generators from the list. right?
So i understand it so far, but I don’t get it implemented.
I already try to code the following for the first part:
using System;
using IdleEngine.Sessions;
using UnityEngine;
using IdleEngine.Generators;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace IdleEngine
{
public class IdleEngine : MonoBehaviour
{
public Session Session;
string json;
string json2;
Generatordata generator;
Generaldata general;
public List<Generatordata> generatorList;
private void Awake()
{
generatorList = new List<Generatordata>();
}
private void OnEnable()
{
Session.CalculateOfflineProgression();
}
private void OnDisable()
{
Session.LastTicks = DateTime.UtcNow.Ticks;
SaveGeneralJson();
SaveGeneratorsJson();
}
public void SaveGeneratorsJson()
{
foreach (Generator gObject in GameObject.FindObjectsOfType<Generator>())
{
generator = new Generatordata(gObject.Owned, gObject.transform.position, gObject.multipliers);
generatorList.Add(generator);
}
json = JsonUtility.ToJson(generatorList);
File.WriteAllText(Application.persistentDataPath + "\\save.txt", json);
}
public void SaveGeneralJson()
{
general = new Generaldata(Session.Money, Session.Level, Session.LastTicks);
json2 = JsonUtility.ToJson(general);
File.WriteAllText(Application.persistentDataPath + "\\savegeneral.txt", json2);
}
}
}
[Serializable]
public class Generatordata
{
public int UpgradeLevel;
public Multiplier[] Multipliers;
public Vector3 Position;
public Generatordata(int upgradeLevel, Vector3 position, Multiplier[] multipliers)
{
this.UpgradeLevel = upgradeLevel;
this.Multipliers = multipliers;
this.Position = position;
}
}
[Serializable]
public class Generaldata
{
public double Money;
public double Level;
public long LastTicks;
public Generaldata(double money, double level, long lastTicks)
{
this.Money = money;
this.Level = level;
this.LastTicks = lastTicks;
}
}
But i´m not sure, if my approach is correct and i also have problems to find the generators in the scene with the foreach-loop and the FindObjectsOfType. The Loop is not executed. I’m already overwhelmed with the first part. So can someone help me and show me how it works correctly? Maybe there is a tutorial for my case or something. That would be awesome.
Thank you very much.