Okay, so, I know the learning curve can be a little bit of a tough part for you starting out with xml, but I have to seriously recommend just having one database for your entire game, a single game object.
Create a c# script called Data.cs (I called my MG_Data.cs for 'main game data) and put something like this in it:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using System;
using System.Xml;
using System.Xml.Serialization;
public class MG_GameData
{
public List<SceneObject> SceneList = new List<SceneObject>();
public RPG_Data rpg_Data = new RPG_Data ();
public List<Ecmd> EcmdList = new List<Ecmd>();
}
I’m not gonna write what I have for my Ecmd or SceneObject objects, but inside of my rpg data object I then have this:
public class RPG_Data
{
public RPG_Character[] RPG_PartyArray = new RPG_Character[3];
public List<RPG_Character> RPG_CharactersList = new List<RPG_Character>();
public List<RPG_Ability> RPG_AbilitiesList = new List<RPG_Ability>();
public List<RPG_Enemy>RPG_EnemiesList = new List<RPG_Enemy>();
public List<RPG_EnemyGroups>RPG_EnemyGroupsList = new List<RPG_EnemyGroups>();
public List<RPG_Animation>RPG_AnimationList = new List<RPG_Animation>();
public List<RPG_Item>RPG_ItemsList = new List<RPG_Item>();
}
It’s based on rpg maker basically.
Then you can have a class for each one:
public class RPG_Item
{
public string Name;
public string menuGraphic;
public bool Useable_OnField;
public bool Useable_InBattle;
public int Effect_Base;
public enum StatusEffect { Dodge, Mana, DodgeEachTurn, ManaEachTurn }
public StatusEffect statusEffect;
public int Effect_EachTurn_NumberOfTurns;
public enum Range { Close, Far, Any }
public Range range;
public enum UsedOn { OneAlly, AllAllies, Enemy, AllEnemies, Everyone }
public UsedOn usedOn;
}
That’s what I’m doing at least.
Now, there are other ways you can do it, but if you’re trying to make an rpg game with some length to it, in my opinion it’s better to do it this way.
Now, when your game starts have a script that’s the very first script in your execution order and put this line of code in it (I just had my mg_data.cs file execute first so the same file had this:
public class MG_Data : MonoBehaviour {
public static MG_GameData mg_GameData = new MG_GameData();
public void Awake() {
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MG_GameData));
FileStream fileStream = new FileStream (Application.dataPath + "/Data/MG_Data.data", FileMode.Open);
MG_Data.mg_GameData = xmlSerializer.Deserialize (fileStream) as MG_GameData;
fileStream.Close ();
}
}
Listen to me when I say this: You shouldn’t give up on xml for a few reasons. Xml is very very very easy. Above is like 5 lines of code that loads a file into your main game data object, and then when you save the ONLY object you’ve gotta save is that one object mg_GameData or whatever you call it.
You can get an xml editor then, and mess around with the file you save (above for me it was my MG_Data.data file).
If you have trouble sometimes, make sure you’re debugging and using breakpoints. Take a good amount of tutorials and practice File.IO commands.
They just seem hard at first, but in reality if you want to make rpgs it’s critically important that you get the hang of reading and writing to some kind of data format first and foremost, so take the time and actually learn how to do it, you won’t regret it (if you’re truly serious about creating an rpg).
To work with xml or json or even writing to text files, you need to create instances of objects/classes.
You can’t do that with any class that inherits from monobehaviour. So you couldn’t do:
public class Data : Monobehaviour {
}
You’ll have to make a class that’s detached from monobehaviour like this:
public class Data {
}
Only then can you run within a monobehaviour script (the in-game scripts) the new keyword on it like this:
Data data = new Data();
Now you can use dots to get it’s data from it anywhere in your game as long as you reference it in a script:
//first object in first scene
data.Scenes[0].Objects[0].Position
(I know that’s not ideal, but for the sake of example)
Now you can simply just write your file like this:
XmlSerializer xmlSerializer = new XmlSerializer (typeof(MG_Data));
FileStream filestream = new FileStream (dataPath, FileMode.Create);
xmlSerializer.Serialize (filestream, mg_Data);
filestream.Close ();
It’s extremely simple. Xml is readable from an external editor, so it’s very easy to export an empty main game object and then edit it within an editor so you don’t even have to create your own custom xml editor within unity’s inspectors if you don’t want to (saves you tons of time).
I was trying to get the crappy regular filestream working and encryption files using Unity, but a friend of me said “people are gonna crack your save files anyway, and it’s easier to have it be a readable file”, so I tried xml and seriously once you get it working it’s actually very easy.
I hear json is also good, but the point is, take it slow one step at a time and actually learn this topic, even spend a month or so if you have to. Don’t give up on xml, just use your breakpoints in your ide and debug that way and you’ll see what lines are causing your trouble.
For the current rpg making template for unity I’ve been working on, as I’ve worked at game programming and scripting on and off for 10 or so years, I knew I needed to have data save/load at the top of my list of priorities, even if it was one of the most difficult topics starting in c#.
The steps to working with it are easy:
- Create instance of data object (it’ll be empty)
- When your very first scene gets loaded (put inside of a newscene.cs script or something if(SceneManager.GetCurrentSceneID (or whatever it is) == 0) { write data file into empty data object we just made.
- As your game progresses, the mg_data file is gonna change (world data like non-static object positions it holds, if you want to do that or whatever else).
- Save it. Write a new data file that becomes your save game file (like 4-5 lines of code above).
- If user loads game, you make it so that they select from a list of the xml files (you can name them like .sav if you want) you have.
- If user chooses to load, empty the data object, load data from your save file into your empty data object.
Sounds complex, actually really simple, and it’s well worth it.