- Home /
What are good ways to store a simple set of data/variables on an object
Hi, I am currently working on a project that involves a slot inventory system and everything about how the items being picked up/used/crafted etc, are identified is from a set of variables in a script on the object representing the item or icon representing it in the inventory. This is working great but the script I am storing them in is just a normal monobehaivour script with public variables that i fill in inspector. I am assuming that because the script is literally just holding some variables and not actually doing/running anything that there may be a better way to store the variables on the item objects. Also like mentioned I need to make these variables public which by my understanding is good to avoid if possible.
Answer by Stratosome · Jul 15, 2018 at 09:46 PM
So, you have a MonoBehaviour that is just a set of variables? And do you have another MonoBehavior then that actually manages your inventory system and just gets the other scripts variables? You could always make just a basic class that doesn't extend MonoBehaviour and then make a variable for that class in your main script. A struct could also do the same (similar to a class, but isn't passed around by reference). If you need to be able to modify these variables, there are a few options. If you make a class or struct to hold these variables, you could add [System.Serializable]
above the class/struct to get it to show up in the inspector. Another cool way to go about this is to use ScriptableObjects. It's a way to kind of make a prefab that holds configuration settings for something in your scene. Or that's at least one example of how to use them. I also don't think that just having a MonoBehaviour to hold some variables for inventory settings isn't all that bad. Not sure what the BEST way to do it is, but those are some options.
Just been reading the docs on those, looks like exactly what i'm looking for. Thanks!
Awesome. Let me know if you have other questions on them. It's about the best I could do given the details of your initial question.
Answer by ShadyProductions · Jul 15, 2018 at 09:38 PM
I usually make an xml file with the item structure in it, where I setup all my items. And write some custom xml parser class to load in all my items at startup based on the xml file using Linq-to-xml, This also allows (mod support) in the future if people wanna add items of their own. Another good way is to use Scriptable Objects.
Yup, that's another way to do it. Haven't used xml stuff myself yet.
Your answer
Follow this Question
Related Questions
onGui not getting called every frame? 2 Answers
Attached scripts as List? 1 Answer
Script as a variable being assigned later in script??? 3 Answers
Change variable in Mono from other script 1 Answer
What is an instance of a GameObject? 1 Answer