Search Unity

Would this work for easy saving for a skill tree?

Discussion in 'Scripting' started by ardizzle, Aug 29, 2014.

  1. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    I have a game with 3 classes and they are going to have skill trees. I have been thinking about how I want to go about saving them and I came up with an idea to just save one big number rather than making several playerPref calls for each class. What I was thinking was on save I would call an algorithm that would look through each skill and add 4 numbers to the end of a string.

    For example if my first skill in the tree was level 3 it would add "0103" to the string. Then it would add "0205" to the string. Then all I have to do is save that string and on load change the sting to an int. Divide that by 1000 to get my last skill group and divide that by 10 to get skill id and skill level.

    Think this would work well is is there a better system for mass saving small data?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    http://docs.unity3d.com/ScriptReference/PlayerPrefs.SetInt.html

    Since playerPrefs works like a dictionary of <string,int>, you can use the string key as something like "skill01" and the int value as the skill level. There is no 'efficiency' advantage in retrieve a single string and then performing additional operations on it.
    Then you can easily iterate over all skills and retrieve all skill levels in one go, without the need to parse.
    Code (CSharp):
    1. for(int i =0; i<numberOfSkillsInTree; i++){
    2.   string key = "skill" + i;
    3.   int skillLevel = playerPrefs.GetInt(key);
    4.   //do something here with skillLevel
    5. }
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    hpjohn is right that there's no performance advantage to a single entry. However, you might find it easier to manage PlayerPrefs with just one key. You could serialize the whole thing as an XML or JSON string and save that. Something like:

    Code (csharp):
    1. [Serializable]
    2. public class SkillTree {...}
    3.  
    4. XmlSerializer xmlSerializer = new XmlSerializer(typeof(SkillTree));
    5. StringWriter writer = new StringWriter();
    6. xmlSerializer.Serialize(writer, mySkillTree);
    7. string myXML = writer.ToString();
    8. EditorPrefs.SetString("My Skill Tree Key", myXML);
     
  4. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    Ok guys thanks for the replys. :)