During my semi-occasional ritual of partially rescripting something and then giving up in the span of 15 minutes, a question came across my mind. Is it better to have the stats for my weapons stored as entries in a dictionary, or have them as a set of value objects put in a folder?
I feel like having them as entries in a dictionary will make it so I’m not keeping useless data like what the value object is parented to, but I also don’t know if having them in a dictionary will have them less readily available and modifiable than a value object. Because this decision will probably result in me rewriting a handful of scripts, I would like to know the pros and cons beforehand.
For reference, the first image is written inside of a ModuleScript.
P.S. Is this the right topic to post this on?
Attachments below to hopefully explain what I mean:
Generally, values are preferred when you plan to release it to the public (so people don’t get intimidated of having to edit a script), or for mere convenience. Take into account those values, as proper instances, may take up a bit more of memory than just saving to a dictionary.
Dictionary is preferred in every other case that’s not aforementioned, including it being more memory efficient and easier access from script. Dictionary may also allow for more flexibility.
DrMystery’s suggestion of having them as attributes is fine as well, however, just as with values, attributes have some limitations on what you can save and how. If it’s just numbers and booleans these restrictions shouldn’t be of concern, though.
The only data type that we use everyday an attribute can’t save is a dictionary, but it won’t matter if all the stats are saved to respective attributes
A ValueBase (The general class of all value objects: NumberValue, StringValue, etc) will take up the most memory due to being an actual Instance, however you’ll have better control over what happens when it changes. Other issues that can occur is them not loading in entirely when a LocalScript runs to check for them, so :WaitForChild() may be needed to ensure they load in on time.
An Attribute takes less memory than an actual Instance object, and are faster to reference, however codewise will be longer to write and execute. This is the most recommended option due to being the fastest to code and more memorye efficient than an Instance
A Dictionary is the most memory efficient however it does not contain any of the features listed above, so you have to code in all the required features for it to function properly. Additionally you’d have to send over information to the Client in order to ensure that they arent cheating and or doing the funny more often as this information will not replicate across the Server/Client boundary.
You’re leaving out any kind of Instance you would like to save in an attribute, for example, a “BulletModel”, there’s no way to save a Model in an attribute.
You can put literal functions as values in a dictionary (to get a dynamic value each time that entry is called depending on the conditions established), impossible in attributes.
You could attach a metatable to the dictionary for even more functionality regarding what’s taken in and out.
Among some others.
So, no, the decision is not just about being able to save arrays/dictionaries or not.