This works fine but only within a game session. I use playerprefs to store information about Health status so in case a game is opened again the health status is updated from that playerprefs.
Unfortunately the playerprefs don’t allow dictionaries to be added (because of performance reasons/C# limitations).
I think when I somehow get this last solution working, my requirements are met. But again I face an issue because bools are not supported for PlayerPrefs either.
I know that actually serializing the data (and putting is in Json or XML) is the way to go, but there are not so many collectables so performance wise it should be ok plus I’m only into C# for two weeks now.
I spend a full day in trying to grasp the serialization concept but again a dictionary seems to be not standard supported.
Another option I thought of is to assign a different script to each collectable where the pickup is handles and a static variable is filled and stored in playerprefs that can be validated and destroying the collectable if met. Actually the same way I store my health parameter now. But in this case I have to create like 25 static variables and 25 scripts assigned to the different collectables and that I find a very dirty solution (although it’s the only one I got working myself so far).
I’m a complete noob in C# and just started to learn unity for the past 3 weeks so forgive me my unlogic propositions.
If anyone can set me on the right track I’m more than happy.
You can save the booleans as an integer. It’s a little waste of space so you can often just bit-pack the booleans into integral types by bit-shifting. E.g. an integer can represent 32 booleans with the most basic form of packing.
You could actually apply this to your scene, as your coins appear to have an ID which usually doesn’t change. (Tbh, the script that you posted works, but it has got its problems though).
E.g. if you have 100 coins, you’d not save 100 integers, you’d rather save a maximum of 4 integers, each of them represents the flags for 32 coins, except the last one which represents the remaining 4 coins.
In regards to serialization, it’s not as difficult as it might appear. There are quite some nice user-friendly and efficient ways to do it. E.g., you can find generic serializer which just take an object and it’s type and would then write it to a specified file.
But since you’re new to programming, it’d be a good step to continue working on your current approach. When it’s done, you can still look into other options.
Thanks for your answer.
I’m not getting the 4 integers and 32 flags one. I try to do the seperate 100 integers first by converting the ID’s which have true into interger values so I can try to push them in a list of playerprefs, later converting that list back to the dictionary upon start.
As I don’t have flags I don’t see how I can group them in 4 sets of 32 while keeping the coins unique.
The coin prefab are placed in the level and processed with a pick script which updates the score as well as the dictionairy. The only thing unique about them is the ID which is generated in the dictionairy code of StarManta.
I’m actually programming myself quite a bit work related, but a totally different language (ABAP non-OO).
The serialization indeeds is the most appealing as it seems far more flexible than the playerprefs. But also more factors such as filepaths for OS targets etc. I’ll follow your advise and take it step by step.
Only when you start to save the collected coins, you’d iterate the collection (dictionary in your case) and map the IDs to their corresponding bits.
First of all, imagine a bit sequence of (e.g.) 4 integers. You’d have 4*32bit = 128 bits to work with.
A coinID identifies a single bit in the sequence, i.e. if you want to save coinID 16, you’d have to set the 16th bit of that sequence.
So how do you do that?
Well, you can be creative.
You could translate the entries in the dictionary to an array that will represent the flags and afterwards map them to the integers bit.
You could directly start to write the bits.
Let’s look at the latter one:
When you start to save the game state:
Create an array with the size of necessary integers, this can be calculated using integer division.
Iterate the collected coins, calculate the integer’s index by integer division and the bit index using modulo.
As of now, you have to determine whether you want to save them using big-endian or little-endian.
Little endian would save the IDs as if you had a normal sequence and started to index the bits as: 0-31, 32-63, … (or 1-based: 1-32, 33-64, …)
Big endian would mess it up a little: 31-0, 63 - 32, …
With targetInt being the integer to map on, bitIndex the index calculated using modulo like explained above, you can do the following (keep in mind that the examples assume your coinIDs are zero-based):
This is probably not as detailed as it could be, if you’re still stuck just ask.
Last but not least you’d just save the integer array with a continuous numeration, like Coins0, Coins1, …
Reading the values would be the exact other way around.