I’ve been thinking of using a dictionary to store each characters’ stats, but I’m not sure if it’s a good idea, and I’m kinda stumped in that regard
A good starting point is to make a Stats class and give one to each of your characters.
Is there any reason you want to use a Dictionary?
Gotta give us some information about your idea and how you imagine it working if you want to discuss various approaches. It’s hard to speculate otherwise
The dictionary was so that I could save everyone’s data in one spot, and I thought assigning every character a key would make things easier. As for how I imagine it working, every character would have the same variable for their stats, and when I load a save file the game would read the dictionary and assign everyone their saved stats
Just do it as mopthrow said , create class Stats and assign it to every player you dont need dictionary for it.Then if you want have it in one sctipt you can create another class PartyStats for example and that will be constructed from List for example ,but you have tons of ways hot to do it.
using System.Collections.Generic;
using UnityEngine;
//This you attach on any gameobject when game start
public class GameManager : MonoBehaviour
{
//You create object where all player stats will be stored later
private void Start()
{
List<Stats> partyStats = new List<Stats>();
Data.partyStats = new PartyStats(partyStats);
}
}
//This script you add on PlayerGO in Unity
public class Player : MonoBehaviour
{
//When player spawn object of his stats will be created with 1str/2agi/3int and will be added to list of all stats
public Stats stats;
private void Start()
{
stats = new Stats(1, 2, 3);
Data.partyStats.partyStats.Add(stats);
}
}
//Hold all player stats what you add during game
public static class Data
{
public static PartyStats partyStats;
}
//Stats of each player
public class Stats
{
public int str;
public int agi;
public int intel;
public Stats(int str, int agi, int intel)
{
this.str = str;
this.agi = agi;
this.intel = intel;
}
}
//List of stats of all players in game
public class PartyStats
{
public List<Stats> partyStats;
public PartyStats(List<Stats> partyStats)
{
this.partyStats = partyStats;
}
}
thanks! Though I’m not entirely sure how I’m supposed to be able to save and load the data from there into the characters (I do know how to save variables to a file, I’m just confused as to how to do it here)
Saving thes values to file is a different story,this will make all your values to one object then you can save it how you like.
Json, database, textfile anything. I perrsonally like this :
fyi in every topic where i link this video someone will come saying that it is not secure etc, well anything is “hackable” true is told in video - it is more secure then just standard json and xml because it is not so easy editable
Oh I’ve seen that, though what I don’t get is what exactly I should save here to be able to save the whole party data (or well, being able to save everything in one file), since in the video he only saves the player.
How to reason about what you need to save:
So I made a few modifications and ended up like this, though I’m not entirely sure how to load
public class GameManager : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
List<Stats> partyStats = new List<Stats>();
Data.partystats = new PartyStats(partyStats);
}
}
public static class Data
{
public static PartyStats partystats;
}
public class PartyStats
{
public List<Stats> partyStats;
public PartyStats(List<Stats> partyStats)
{
this.partyStats = partyStats;
}
}
public class Stats
{
public int cod;
public string nom;
public int wp;
public float spe;
public Stats(Character C)
{
this.cod = C.cod;
this.nom = C.nom;
this.wp= C.wp;
this.spe=C.spe;
}
}
public class Character : MonoBehaviour
{
// Start is called before the first frame update
public Stats Stat;
public int cod;
public string nom;
public int wp;
public float spe;
void Start()
{
Stat = new Stats(this);
Data.partystats.partyStats.Add(Stat);
}
}
public class SAVE : MonoBehaviour
{
public static void Save()
{
string json = JsonUtility.ToJson(Data.partystats.partyStats);
File.WriteAllText(Application.dataPath + "/save.txt", json);
Debug.Log("Saved");
Debug.Log(json);
}
// Update is called once per frame
public static void Load()
{
string json = File.ReadAllText(Application.dataPath + "/save.txt");
Data.partystats.partyStats = JsonUtility.FromJson<List<Stats>>(json);
Debug.Log("loaded from: " + json);
}
}
ok so I think I managed to fix the load function, though I’m not sure if having to assign it to every party member separately is too efficient, but I couldn’t find a way to load the data to every party member simultaneously
public static void Load(Character C)
{
List<Stats> Aux = new List<Stats>();
string json = File.ReadAllText(Application.dataPath + "/save.txt");
Aux = JsonUtility.FromJson<List<Stats>>(json);
for (int i = 0; i < Aux.Count; i++)
{
for (int j = 0; j < Aux.Count; j++)
{
if (Aux[j].cod == C.cod)
{
C.nom = Aux[j].nom;
C.wp = Aux[j].wp;
C.spe = Aux[j].spe;
}
}
}
Debug.Log("loaded from: " + json);
}
Here is how it works both examples give yo usame values
using UnityEngine;
//This values you want save.
public class DataYouWantSave : MonoBehaviour
{
public int a = 1;
public int b = 2;
public int c = 3;
//You pass whole script as parameter.
void Save1()
{
SaveManager.SaveGame(this);
}
//You pass your 3 variables
void Save()
{
SaveManager.SaveGame(a,b,c);
}
}
public static class SaveManager
{
public static void SaveGame(DataYouWantSave data)
{
///Some save method what use this parameter.
int a = data.a;
}
public static void SaveGame(int value1, int value2, int value3)
{
int a = value1;
///Some save method what use these parameters
}
}
one question tho, would the script I used above work well? (and is there a way to load every party member’s data at once rather than one at a time?)
there is a way i described already you just load all PartyStats together and then you assign each stats for player
(sorry, haven’t been trying much this past month)
How do I load them all and assign them though? Through a for? Since the party size will increase as the game progresses