Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Is this rpg scheme and data structure okay?

Discussion in 'Scripting' started by astracat111, Apr 3, 2018.

  1. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    719
    Hello again,

    Thanks to the guys who helped me out in the previous topic with constructors...I think I've got a decent namespace down for a basic rpg but was looking for some feedback.

    Here's the code. I'm wondering if it's okay to write it like this.

    The idea is that characters have these things called guardians which are like summons in final fantasy. With this game instead of monsters characters just fight other characters and are assisted by guardians which are their allied monsters, so it's kind of like pokemon in a way except the characters themselves are assisted by the pokemon instead of fighting themselves only.

    There are no items, everything is focused on abilities. I'm trying to keep it very simple for now to start. There's also just a single most important stat: mana which counts for HP and MP, with five basic stats on top of that.


    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4.  
    5. namespace RPG {
    6.  
    7.     public class Data {
    8.         public List<Ability>     Abilities         = new List<Ability>();
    9.         public List<Effect>     Effects         = new List<Effect>();
    10.         public List<Character>     Characters        = new List<Character> ();
    11.         public List<Ally>     Allies            = new List<Ally>();
    12.         public List<Opponent>     Opponents         = new List<Opponent>();
    13.         public List<Guardian>     Guardians         = new List<Guardian>();
    14.         public List<Encounter>     Encounters         = new List<Encounter>();
    15.     }
    16.  
    17.     public class Element {
    18.         public enum Type {
    19.             Fire,
    20.             Earth,
    21.             Wind,
    22.             Leaf,
    23.             Water,
    24.             BlueFire,
    25.             Ice
    26.         }
    27.         public Element.Type type;
    28.         public int Lvl = 1;
    29.     };
    30.  
    31.     public class Stat {
    32.         public enum Type {
    33.             HandtoHand,
    34.             Mana,
    35.             Projectile,
    36.             Movement,
    37.             IQ
    38.         };
    39.         public Type type;
    40.         public int Power = 0;
    41.     }
    42.  
    43.     public class Effect {
    44.         public string Name;
    45.         public Stat stat;
    46.         public int Mod;
    47.         public int NumberOfTurns;
    48.         public bool EveryTurn;
    49.     }
    50.  
    51.     public class Ability {
    52.         public string Name;
    53.         public int Cost;
    54.         public int Hit;
    55.         public int Blocked;
    56.         public int Miss;
    57.         public Element ElementRequired;
    58.         public List<Stat> StatsRequiredList = new List<Stat>(); //this is wrong
    59.         public List<Effect> Effects = new List<Effect>();
    60.     }
    61.  
    62.     public class Stats {
    63.         public Stat HandToHand() {
    64.             HandToHand ().type = Stat.Type.HandtoHand;
    65.             HandToHand ().Power = 1;
    66.         }
    67.         public Stat IQ() {
    68.             IQ ().type = Stat.Type.IQ;
    69.             IQ ().Power = 1;
    70.         }
    71.         public Stat Mana() {
    72.             Mana ().type = Stat.Type.Mana;
    73.             Mana ().Power = 1;
    74.         }
    75.         public Stat Movement() {
    76.             Movement ().type = Stat.Type.Movement;
    77.             Movement ().Power = 1;
    78.         }
    79.         public Stat Projectile() {
    80.             Projectile ().type = Stat.Type.Projectile;
    81.             Projectile ().Power = 1;
    82.         }
    83.     }
    84.  
    85.     public class Guardian {
    86.         public string Name;
    87.         public string FacesetFilePath;
    88.         public enum GuardianElement { Light, Dark };
    89.         public GuardianElement guardianElement;
    90.         public int ManaBonus;
    91.  
    92.         public Stats stats = new Stats();
    93.  
    94.         public List<Effect> GuardianEffects = new List<Effect>();
    95.     }
    96.  
    97.  
    98.     public class Character {
    99.         public string Name;
    100.         public string FacesetFilePath;
    101.         public Element element;
    102.         public Guardian guardian = null;
    103.         public int CurrentMana;
    104.         public int TotalMana;
    105.  
    106.         public Stats stats = new Stats();
    107.  
    108.         public List<Ability> Abilities = new List<Ability>();
    109.     }
    110.  
    111.  
    112.     public class Ally {
    113.         public Character character;
    114.     }
    115.  
    116.     public class Opponent {
    117.         public Character character;
    118.     }
    119.  
    120.     public class Encounter {
    121.         public List<Ally> Allies             = new List<Ally>();
    122.         public List<Opponent> Opponents     = new List<Opponent>();
    123.         public O_Lists.O_Lists_Base o_Lists_Base; //scripting language strings get tucked inside here
    124.         public string BGM;
    125.     }
    126.  
    127. }
    128.  
    So in my main game database's script I have static methods for adding and removing data. Here's a sample of creating a character. I'm not sure if it's necessary to make it static...but I've been making all of the writing, reading, and removing of data static within my main game data class/script:

    Code (CSharp):
    1.  
    2. public static Character Create_Character(string Name, string FacesetFilePath, Element element, Guardian guardian, int TotalMana, int HandToHand_Power, int IQ_Power, int Mana_Power, int Movement_Power, int Projectile_Power) {
    3.         //Create new Character
    4.         Character newCharacter                 = new RPG.Character();
    5.         newCharacter.Name                     = Name;
    6.         newCharacter.FacesetFilePath         = FacesetFilePath;
    7.         newCharacter.element                 = element;
    8.         newCharacter.guardian                 = guardian;
    9.         newCharacter.TotalMana                 = TotalMana;
    10.         newCharacter.stats.HandToHand ().Power     = HandToHand_Power;
    11.         newCharacter.stats.IQ ().Power            = IQ_Power;
    12.         newCharacter.stats.Mana().Power            = Mana_Power;
    13.         newCharacter.stats.Movement ().Power     = Movement_Power;
    14.         newCharacter.stats.Projectile ().Power     = Projectile_Power;
    15.  
    16.         //Add Character to Database Here
    17.         return newCharacter;
    18.     }
    Now all that I'm thinking I'll need to do is to create an editor script for the front end part of it where you manipulate the data visually...
     
    Last edited: Apr 3, 2018
  2. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    661
    Instead of having a method with many parameters you should provide a struct or class containing this data. I also use static methods like GenerateCharacter in singleton Factory classes. I think it is fair for those as they have to be accessed from a lot of places.

    For my game I have a StaticData class for each of my content types, like Character in your case. It would be called CharacterStaticData. You can use Scriptable Objects for instance and edit it right in the editor.

    I use xml files which I edit in a spreadsheet application. Therefore I generate an intermediate class, CharacterXMLData which I read as an array from the xml file and convert that to CharacterStaticData instances. This is necessary because I have limited possibilities in a spreadsheet and that is not suited for everything. So for instance I cannot have an array inside that xml file, excel wouldn't handle that. So I write my own parsers which do validity checks and can convert a string to an array, enum or boolean. You could already serialize them but I prefer to use strings only.

    I will switch over to a custom editor later as well but then I will make an Unity app using Canvases so I can also use it outside of Unity. The advantage is that you can use dropdown lists and custom fields or even linkt to other objects/IDs. But that can also be achieved with ScriptableObjects in Unity.
     
    Last edited: Apr 4, 2018
    astracat111 likes this.
  3. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    719
    Gotcha, this is the second time scriptable objects has been suggested. What XML editor do you use?

    Using an XML editor seems like it'd save the most time, at the moment since it's fairly simple I've been creating a custom rpg database editor window for it.
     
  4. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    661
    I use MS Excel, other spreadsheet apps seemed less intuitive.
    In order to open and write xml files it has to be an array of a class with non array properties.
    So the XMLData class can not contain members like arrays or structs but string, int and so.

    Then each row is an entry and each column is a property.
     
  5. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    719
    Gotcha, I feel like a newb I really didn't know that excel opened xml files...