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!
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.
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!
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?
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!
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.
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.
although the prefab idea is not bad also, maybe you should combine these two ideas?! :- ) lemme know how its working
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?
I'm gonna make some pretend classes hopefully they somewhat match your name so its obvious what goes where. Code (CSharp): using UnityEngine; using System.Collections; using System; public enum e_MainStatsNames { Strenght,Intelect,Speed,Luck } public class MainStatistic : MonoBehaviour // base class for strenght ,intellect etc { public int Basevalue; // base value of this stat. public int BuffValue; // buff value, that is potions(temporary) tweak this one. public string Name; // name for our stat. public int AdjustedBaseValue { // acces this for ui displaying, get { return _basevalue + BuffValue; } // so both values are displayed as one final } // Or for any calculation you need it to be. } Code (CSharp): using UnityEngine; using System.Collections; using System; public class Player : MonoBehaviour { private MainStatistic[] CharacterMainStats; // intentionally private. for accesing you use GetStat() void Start() { // we set lenght of an array to be the same as the MainStats enum at the top, // so if you want to add new statistic you just write it up there to enum. CharacterMainStats = new MainStatistic[Enum.GetValues(typeof(e_MainStatsNames)).Length]; SetupMainStats(); } void SetupMainStats() { // We Create So many Statistics as needed for (int i = 0; i < CharacterMainStats.Length; i++) { CharacterMainStats [i] = new MainStatistic (); CharacterMainStats [i].Name = ((e_MainStatsNames)i).ToString(); } } public MainStatistic GetMainStat(int index) { return CharacterMainStats [index]; } } Code (CSharp): public class PoisonousTrigger : MonoBehaviour { public int Damage; public Player player; void OnTriggerEnter(Collider other) { if (other.CompareTag ("Player")) { // we crossed poison trigger, our first stat loses -1; player.GetMainStat (0).Basevalue -= 1; // first stat being strenght for example. } } } 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.
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!