Saving item count with Playerprefs
So i'm new to programming and writing scripts, and ive decided to make an incremental game just for fun and to learn new things. and i have come to a point where i think i should implement some kind of save system, before i add a bunch more stuff.
So i started using PlayerPrefs , and it works great for saving gold and other variables, but im having problems with saving how many upgrades i have bought. All my upgrades are in one script, and if i save Count , it thinks that every upgrade should have the same count.
Heres a pastebin http://pastebin.com/JspjPdDa , ignore the Playerprefs, i was just trying out different strategies. Thanks in advance.
Getting a bit confused with this part:
All my upgrades are in one script, and if i save Count , it thinks that every upgrade should have the same count.
So you have different upgrades, which should have different counts?
Yes that's right, with my script im able to add multiple upgrades that all use the same script and have different counts, but I'm not able to save them this way.
huh I had answered yesterday. Did it get deleted?
Answer by fredz0004 · Nov 24, 2015 at 02:10 PM
No worries not sure what happened I submitted an answer yesterday. Anyways if you need different count values for different upgrades. You need to be able to differentiate your upgrades. Give your upgrades different ID values in PlayerPrefs, and associate that ID to a count value in PlayerPrefs.
Small example:
string upgradeID = 3;
int upgradeCount = 43;
PlayerPrefs.SetInt(upgradeID, upgradeCount);
This should guide you in the right path, you just need to make sure each time you generate a new upgrade, you give it a new ID somewhere in your script. When you are testing the logic, just print in a forloop all the available upgrades, and see if they have different count values.
Something like this:
string upgradeID = 3;
int upgradeCount = 43;
PlayerPrefs.SetInt(upgradeID, upgradeCount);
for (int i = 0; i < numberOfUpgrades; i++) {
print ("This is upgrade ID" + i + "its count value is " +
PlayerPrefs.GetInt(i.ToString()));
}
Let me know if it works.
I'm still confused , I'm able to have different count values already on different upgrades , But i can't save it because they all use the same Int i suppose. I realise now that im going to need different ID's thanks to you but im still not sure how to implement it.
As you have most likely noticed im using Unity's UI feature , so when i ''buy'' an upgrade it only updates the count for that specific button/upgrade. But i have no way to store that count.
I'm sorry if im co$$anonymous$$g off as really stupid , this is the first time im making anything at home and not in School. I still have a lot to learn so i appreciate all the help.
EDIT: I got it working , Thanks a lot! I think i even solved the problem with updating the prices so you cant buy it for the base price when you first start the game , altough to do that i have to update the price in Void Update all the time , so if there's any better way to do it please share it with me!