Search Unity

Character selection help

Discussion in 'Scripting' started by MrZeker, May 12, 2019.

  1. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Hello, im trying to make a system for character selection. What i already have is a button to choose and change characters, but what i need right now is a way to load specific vallues.
    Each character has his own model, stats and skills.
    Is there any way that i can set in a single script to store that information so it can be loaded? like for example use a ID
    so if the ID of the character selected is "JHON", it will automatically set the values of the stats to those in the JHON file (15 str, 12 dex, 11 con, 20 int, etc)

    i tought of making ALOT of public variables, one for each stat, and just have a script for that, but i wonder if there isnt a better way to do it. Also im not sure how good idea is that since character will change their stats over time (level up),
    Any idea (as well with what code/function to use) is welcome!
     
  2. Game development means that you're making these decisions. There aren't any "simple scripts" which does what you want to do, since it's your game, your job is to put it together.
    Now, with that said, you can choose:

    - you can store your characters base stats in ScriptableObjects
    - you can store your data in text files (JSON, XML, etc)
    - in a database (NoSql or similar)

    Also keep in mind that the base stats and the actual (current) stats are different and you should not mix them together. You will need game saving algorithm which writes data on disk or some medium and load them from there, but you don't want to change the initial character selection affected by it, so it's separate.

    Your character selection system should read the proper data (obviously you need a list of available characters as well).
    There are some assets on the Asset Store which implement some of these things, but this would mean you lock yourself in someone else's solution and it may have long term implications. If that's okay for you, then go ahead and search for them on the AS and buy one of those.
     
    xVergilx, Joe-Censored and MrZeker like this.
  3. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    do you want to change the values immediately or in other scene?
     
    MrZeker likes this.
  4. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    I need to change them for the next scene, but i managed to solve it by using Public Static Int. so far it is working, i hope it doesnt consume too much resources to use static variables.

    What i meant was, that i have the idea of what i want to do, i wanted to know possiblities, because i dont know the specific ways to implement it. But i think im gonna need for a load/save system to store the stats.

    Thanks for pointing this out!
     
  5. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    well what i do is on my base character , the player, do array of Stat.cs and Stat.cs has int BaseValue,
    and int ModifiedValue and then third value which is CurrentValue - that just returns baseValue + modifiedValue.
    so therefore from other script you can access it by saying player.stats[0].modified value += 5,
    that would not mess with your base value + ui can then show current value which is base + mod
    and i save stats to json. in menu or where you set it. and simply load it when needed.
    no statics nada,been there,not a good idea. and having like you said "a lot of public variables"
    makes chills up my spine,been there too... hah

    So in few words, have a class with int BaseValue, ModifiedValue(thats the one to change temporary with potion),current value(this returns BaseValue + ModifiedValue), Name and then you can set it up in your BasePlayer.cs into array of those stat.cs and set name for each.

    if any clarification needed, just say.
    do you have trouble with saving/loading?
     
    Last edited: May 15, 2019
    MrZeker likes this.
  6. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    oh wow, that sounds cool. I havent learned yet how to use arrays, im very new. I managed to do a Single player arcade (RPG style) where you fight vs an IA. i wanna now add more characters, and in the future a Story mode where you can progress and stuff (thats where the save and load function would come for storing stats while the character levels).I wanna try to learn that not too late, so i dont have to rewrite stuff.

    What i did is use Static variables, so i can access them when the characetr is picked.
    esentially each character has his own stats, (which are 5 public nono static ints), and when i choose SELECT, i have a public Static int that is called PlayerStat(1/2/3/4/5) and i replace those values with the values from the character selected. So far it works, but so far i havent toght of keeping track of the modified stats.
    i know that what i did is not the best, but is the only thing i managed to do by myself, i didnt wanna just use someone else's assets and code i didnt understand.

    im gonna try what you are saying tomorrow after work, maybe it will work better. Thanks!
     
    Reedex likes this.
  7. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    yes, i get it, having someone's else pieces of code that you don't understand is really not the best idea.
    anyway, what i said is good, and you wont regret 100 perc. :) hah.
    because if you manage to make it work, you can make class resist or sideskills(attack,defense) also.
    For example, Resist.cs
    would have the Same int baseValue,modValue and curValue and you can also set it to array on playerBase.cs
    or even Vital.cs, like hp, mp. very handy way indeed.
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Why not just make a prefab for each character with all of the details filled out?
     
    Pixitales and Joe-Censored like this.
  9. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    I totally agree, i have 50+ scripts and its a mess.
     
  10. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    i havent tought of that, but, in my main scrip the object is asigned. is there any way to replace one entire object with another prefab?

    i wann try that in these days, at least only for stats and skills, but i got into a weird error with blender that delayed me quite a bit.
     
  11. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    although the prefab idea is not bad also, maybe you should combine these two ideas?! :- ) lemme know how its working
     
  12. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    i tried using the prefab mode, but i made some scripts (playerscript, and enemyscript) to be in the objects they affect, so if i instantiate and create the prefab on start ugly things happens. i tried to solve it in various way and spent like three ho urs until i finally decided to drop it for now.
    ill stick to what i have done, using static ints to assert the numbers and translate on the characterselector script. However, you told me that i can use a array to store all the stats in a single line right?
    how can i do that? i mean i have an idea of how to make an array, but not how to read it, so the script knows which value is for what.
    can you help me there?
     
    Reedex likes this.
  13. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    I'm gonna make some pretend classes hopefully they somewhat match your name so its obvious what goes where.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public enum e_MainStatsNames
    6. {
    7.     Strenght,Intelect,Speed,Luck
    8. }
    9.  
    10. public class MainStatistic : MonoBehaviour    // base class for strenght ,intellect etc
    11. {
    12.     public int Basevalue;         // base value of this stat.
    13.     public int BuffValue;        // buff value, that is potions(temporary) tweak this one.
    14.     public string Name;            // name for our stat.
    15.  
    16.     public int AdjustedBaseValue {                    // acces this for ui displaying,
    17.             get { return _basevalue + BuffValue; } // so both values are displayed as one final
    18.     }                                               // Or for any calculation you need it to be.
    19. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class Player : MonoBehaviour {
    6.  
    7.     private MainStatistic[] CharacterMainStats; // intentionally private. for accesing you use GetStat()
    8.  
    9.     void Start()
    10.     {
    11.         // we set lenght of an array to be the same as the MainStats enum at the top,
    12.         // so if you want to add new statistic you just write it up there to enum.
    13.         CharacterMainStats = new MainStatistic[Enum.GetValues(typeof(e_MainStatsNames)).Length];
    14.         SetupMainStats();
    15.     }
    16.  
    17.     void SetupMainStats()
    18.     {
    19.         // We Create So many Statistics as needed
    20.         for (int i = 0; i < CharacterMainStats.Length; i++) {
    21.             CharacterMainStats [i] = new MainStatistic ();
    22.             CharacterMainStats [i].Name = ((e_MainStatsNames)i).ToString();
    23.         }
    24.     }
    25.  
    26.     public MainStatistic GetMainStat(int index)
    27.     {
    28.         return CharacterMainStats [index];
    29.     }
    30.  
    31. }

    Code (CSharp):
    1. public class PoisonousTrigger : MonoBehaviour {
    2.  
    3.     public int Damage;
    4.     public Player player;
    5.  
    6.     void OnTriggerEnter(Collider other)
    7.     {
    8.         if (other.CompareTag ("Player"))
    9.         {
    10.             // we crossed poison trigger, our first stat loses -1;
    11.             player.GetMainStat (0).Basevalue -= 1; // first stat being strenght for example.
    12.         }
    13.     }
    14. }
    you can then make Vital.cs besides MainStatistic.cs
    and just duplicate the logic in Player.cs for those.
    and acces them the same way.
     
    Last edited: May 24, 2019
  14. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Woah, dude you are awesome! Im gonna take a good look at this, there are some things that i dont know yet, but by analizing it i might be able to put it into what i need. Thanks man, really. this is awesome!