Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Kona · Jan 14, 2011 at 02:06 PM · playerprefsmultiplesaveload

Saving with PlayerPrefs and having multiple character slots?

I'm using PlayerPrefs to save my character, as for now it can only save one character, once you create a new character the old characters stats are replaced by the new ones.

I would like to have the characters to be saved to an empty "slot" when created (or to its own slot if we've loaded an old character) but I have no idea how to do this. I'ld appreciate if someone had the time to show me how this could be done. :)

This is from the script used for saving & loading characters:

public void SaveCharacterData() { GameObject pc = GameObject.Find("pc");

 PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();
 PlayerPrefs.SetString("Player Name", pcClass.Name);

 for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
     PlayerPrefs.SetInt(((AttributeName)cnt).ToString() + " - Base Value",  pcClass.GetPrimaryAttribute(cnt).BaseValue);
     PlayerPrefs.SetInt(((AttributeName)cnt).ToString() + " - Exp To Level",  pcClass.GetPrimaryAttribute(cnt).ExpToLevel);
 }

 for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) {
     PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Base Value",  pcClass.GetVital(cnt).BaseValue);
     PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Cur Value",  pcClass.GetVital(cnt).CurValue);
     PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Exp To Level",  pcClass.GetVital(cnt).ExpToLevel); 
 }

 for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) {
     PlayerPrefs.SetInt(((SkillName)cnt).ToString() + " - Base Value",  pcClass.GetSkill(cnt).BaseValue);
     PlayerPrefs.SetInt(((SkillName)cnt).ToString() + " - Exp To Level",  pcClass.GetSkill(cnt).ExpToLevel);         
 }

} public void LoadCharacterData() { GameObject pc = GameObject.Find("pc");

 PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();

 pcClass.Name = PlayerPrefs.GetString("Player Name", "Name Me");

 for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
     pcClass.GetPrimaryAttribute(cnt).BaseValue = PlayerPrefs.GetInt(((AttributeName)cnt).ToString() + " - Base Value",  0);
     pcClass.GetPrimaryAttribute(cnt).ExpToLevel = PlayerPrefs.GetInt(((AttributeName)cnt).ToString() + " - Exp To Level", Attribute.STARTING_EXP_COST);
 }

 for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) {
     pcClass.GetVital(cnt).BaseValue = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Base Value",  0);
     pcClass.GetVital(cnt).ExpToLevel = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Exp To Level", 0);

     //make sure you call this so that the AdjustedBaseValue will be updated before you try to call to get the curValue
     pcClass.GetVital(cnt).Update();

     //get the stored value for the curValue for each vital
     pcClass.GetVital(cnt).CurValue = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Cur Value", 1);
 }   

 for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) {
     pcClass.GetSkill(cnt).BaseValue = PlayerPrefs.GetInt(((SkillName)cnt).ToString() + " - Base Value",  0);
     pcClass.GetSkill(cnt).ExpToLevel = PlayerPrefs.GetInt(((SkillName)cnt).ToString() + " - Exp To Level",  0);

 }

 //output the curValue for each of the vitals
 for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) {
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Tetrad · Jan 14, 2011 at 05:27 PM

One of the ways I would solve this problem is by using JSON.

Store all your per-user data in a json table, and then save that string to a playerprefs key by the user name.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kona · Jan 18, 2011 at 12:57 PM 0
Share

Thanks alot for alot of good ideas :) This JSON thing looks interesting so will give it a go, thanks!

avatar image
2

Answer by PrimeDerektive · Jan 14, 2011 at 04:29 PM

I would have a static/global integer var called something like saveId, and have a GUI at the beginning of the game that lets the player select a save slot. So if the user selected save slot 1, it would set saveId to 1... then whenever you save/load any playerprefs, append saveId to the end of the PlayerPref key name (eg: PlayerPrefs.SetInt("PlayerLevel"+saveId, 1) or something).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KittyAnn · Apr 18, 2019 at 04:14 PM 0
Share

@PrimeDerektive I know this post is very old, but I would like to use your example. In your example (PlayerPrefs.SetInt("PlayerLevel"+saveId, 1) , is the number 1 the level number or is it the saveId number? (crosses fingers it's the level number ^_^ )

1) I am setting up a GUI as you suggested with Slots players can choose from. (Player chooses favorite colored slot) (Still trying to figure out what/how to make code to let PlayerPrefs know which slot is choosen.) 2) So on my login screen I would have "PlayerPrefs.GetString("PlayerName" + saveId, ""); Is that the correct way of saving the "PlayerName"? 3) I'm trying to display the PlayerName like this in a textbox; "NameBox.text = PlayerPrefs.GetString("PlayerName" + saveId, ""); <---Is that how I call it back to be displayed on the main playing game screen?

I'm trying to understand PlayerPrefs with different logged in users. Thank you for your patience in my lack of coding skills.

avatar image
1

Answer by Bravini · Jan 14, 2011 at 05:39 PM

store the players in an array, Erich5h5 made a very handy script for that http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs

the values are stored in registry if you're using windows, I'm still not sure about the performance as I only used this script once.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Anxo · Jan 14, 2011 at 02:25 PM

I think your creating a PlayerPref variable called "Player Name" instead of the player's name. So when you create another character you are replacing the value for the variable "Player Name"

Try to call back the name of the PlayerPref to see if this is the case.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kona · Jan 14, 2011 at 02:35 PM 0
Share

Yes that is true, I put the player name as a PlayerPref so I can have my NPC's take that name and use in dialogues (pardon the grammar xD) with the player.

avatar image
0

Answer by afterlif · Apr 21, 2016 at 09:16 PM

http://answers.unity3d.com/questions/201731/how-to-save-a-progress-by-user-with-diferent-users.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Issues with Save / Load system 1 Answer

Saving & Loading the scene (or at least one array) via Javascript 1 Answer

Player Prefs not Saving in Linux 1 Answer

Why does PlayerPrefs save to the Windows Registry? 0 Answers

Playerprefs doesn't save anything. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges