so i have a custom save system that saved items. its not too much to worry about but all you need to know is that it saves variables such as ints, floats, bools, etc.. I also want to make an item shop where you can change cosmetics such as your skin, enemy skins and stuff similar to that. What is the best way to save this? Should I create a bool for each item? should I do an id system? this is my first time and the least amount of values i have to store the better but I also dont know exactly how I will do this so im just brainstorming now.
#Telling the computer how to save variables
1 messages · Page 1 of 1 (latest)
Id systems usually provide a very solid approach for saving, especially with stuff like items. So I would probably go with that.
is there a tutorial you could link to help put me on the right track
ive just never gone with this before
I don't know of any tutorial implementing an ID system, sadly.
But the process should be pretty straightforward. Assuming that your items are stored as Scriptable Objects, you can simply generate guids for them using System.Guid.NewGuid().ToString() in OnValidate. Just be sure to check whether they already had one before.
what are guilds
globally unique identifiers, and a builtin structure in NET
GUIDs, not Guilds 
oh my bad haha
there is a chance that guids are not unique, but it is really really low, in most cases, for your app, they will be unique
also, about OnValidate, make sure to only do that once if the guid reference is still empty.
guids are also not serializable by unity so you need to find a different way to store them, for example storing the string representation
ill look into it. do you guys mind if i keep this thread open so I can ask for some help
I don't think anyone will delete forum posts, unless they violate some kind of rule. So no worries about the post getting closed.
I actually wanted to add the suggestions solution from thread 2 into my library but I don't think I've done that yet
there are some caveats, like it is fine if you have scriptable objects with guid generations if you have unique assets, but it's harder if you need to have them with prefabs, because not only the prefab would need them, but especially the persistent prefab instances in the scenes, which need separate ones
oh my, that accepted answer in the first link is... pretty bad. using the sqrMagnitude of an object's position as GUID just has too many ways of falling apart, to be usable as a GUID 
there are also assets you could use for saving, like easy save
is there a tutorial specifically on using this kind of system. im not super new to unity but havent ever worked with this beofre
yeah 
I guess for their use case it works
but still, imagine you make some kind of grid-based base building game and use this. and then you place one object at 2, 2 and one at -2, -2. have fun figuring out IDs
NB: It is better to store the GUID as a System.Guid instance, rather than a string, because it will occupy less space
System.Guid is 16 bytes, whereas string representation will be like.. 32 at least
yes, but Unity can't serialize System.Guid. Hence .ToString()
it can't you're right, but it can serialize byte array
Only time you should ToString a guid is for human representation should the user ever need to see it
Touché.
Didn't know Guid had ToByteArray tbh
indeed
and then Guid constructor has an overload which accepts a byte array
so you can easily convert between them
I see Microsoft have changed the docs URL again from docs.microsoft.com to learn.microsoft.com