Item Database or Individual Scripts?

So basically, I’m making a game with C# that takes a simpler approach to items: there are tiers of items and different types of weapons. For ex: there are iron swords, axes, lances, etc. and all iron swords are the same in every way. So I was looking at ways to implement an item database and people recommended xml but it was kind of confusing and seemed overly complicated for what I am attempting to do (especially since I won’t be retroactively adding items later).

Basically, is it better to learn how to do xml item databases or would I be able to create a general script for all swords/weapons and create a specific sword script inheriting from that for all iron swords?
And building off of that:

How would I create a new iron sword to place in a character’s inventory? Would I create a new game object in Unity or could I just create a new sword inside of an individual unit’s script by referencing that sword’s constructor in the script, and then insert it into their inventory?

Thanks and even if you can’t answer all of these, any and all opinions/ideas are appreciated!

1 Like

I would implement your weapons as prefabs: Game objects with mesh or sprite renderers, rigidbodies and colliders (if necessary) and a script defining their attributes and functionality saved as a prefab and then instantiated in game when needed.

For the scripts I would create a base weapon class to contain attributes and functionality common across all the weapons, and then I would create individual classes, which inherit from this weapon class, to implement the major classes of weapons (axes, lances, swords). The differences among all the swords, for example, would just be different attribute information specified in the Sword class script.

Later, if there is a need you could separate the data describing the weapons and their behavior from the weapon prefabs and scripts (and simplify the prefab and scripts in the process). You could store the information in a text file, an XML file, a JSON file or even a database, but I wouldn’t worry about that to start.

Awesome, thanks! I’ll give it a try, I appreciate the feedback!

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:

  1. Create instance of data object (it’ll be empty)
  2. 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.
  3. 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).
  4. Save it. Write a new data file that becomes your save game file (like 4-5 lines of code above).
  5. 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.
  6. 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.

6 Likes

Wow, thanks first and foremost for such a detailed response! I will definitely give xml and databases a longer look and try to get the hang of them if you recommend them, and your examples are a really great starting off point! Also, thanks for explaining everything, this makes it so much easier to know what to look for and start learning!

No problem, and look into .json too, there are other formats for databases you can use as well.

Also, what I do here isn’t ideal:

data.Scenes[0].Objects[0].Position

You want to put public methods inside of your Data class:

public class Data {
 
    //made up a class called 'SceneObj' that's probably inside a 'Scene' class:

    public Vector3 GetSceneObjectPosition(string objectName) {
         //Find object in currentScene using 'objectName':
         foreach (SceneObj sceneObj in sceneObjs) {   // < - custom objects just made this part up
              if (sceneObj.name == objectName) {
                 return sceneObj;
             }
         }
    }

}

It’s best practice probably to be able to simply just call a method then like this:

data.GetSceneObjectPosition("KingGoblinGuy");

From my knowledge that’s the best way to do it, it’s all about making the front end be able to access data easily using public methods.

I’m no expert and there are even better ways of organizing this and doing this. Ask one of the experts please!