Search Unity

script sorting help!

Discussion in 'Scripting' started by mattcscz, Nov 8, 2010.

  1. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    hi, i've created a script here tying to use player prefs so you can create a character and it will save, however im having a little problem. so far i've gotten player prefs to save what class and race you are in a array however im having trouble with CurSlotAmount CharAmount, everytime it laods the level it resets back to 0 and i really cant figure out why XD im sure its a simple mistake somwhere!


    Race Class.js
    Code (csharp):
    1.  
    2. private var RaceHuman = "Human";
    3. private var RaceHumanDescription = "";
    4. private var HumanBoxLoc : Vector2 = Vector2(1240,500);
    5.  
    6. private var RaceNarlon = "Narlon";
    7. private var RaceNarlonDescription = "";
    8. private var NarlonBoxLoc : Vector2 = Vector2(1240,460);
    9.  
    10. private var infoBox : Vector2 = Vector2 (240,460);
    11. private var infoBoxX = 230;
    12. private var infoBoxY = 120;
    13.  
    14. private var RaceBoxW = 90;
    15. private var RaceBoxH = 30;
    16.  
    17. private var ClickedShamen = false;
    18. private var ClickedWarrior = false;
    19.  
    20. static var CharAmountStore = 0;
    21. var CharAmount = 0;
    22.  
    23. var NarlonClicked : boolean = false;
    24. var humanClicked : boolean = false;
    25.  
    26. private var C1 : Vector2 = Vector2 (400,400);
    27. private var C2 = 60;
    28. private var C3 = 20;
    29.  
    30. private var H1 : Vector2 = Vector2 (800,70);
    31. private var H2 = 140;
    32. private var H3 = 30;
    33.  
    34. function Start () {
    35. PlayerPrefs.GetInt("CharAmount");
    36. PlayerPrefs.GetInt("CharacterTemplate.StatCurSlot");
    37. print(PlayerPrefs.GetInt("CharacterTemplate.StatCurSlot"));
    38. print(PlayerPrefs.GetInt("CharAmount"));
    39. }
    40.  
    41. function OnGUI () {
    42.     if(GUI.Button(Rect(Screen.width - H1.x, Screen.height - H1.y, H2,H3), "Create")) {
    43.     CharAmount += 1;
    44.     PlayerPrefs.SetInt("CharAmount", CharAmount);
    45.     CharacterTemplate.StatCurSlot = CharAmount;
    46.     PlayerPrefs.SetInt("CharacterTemplate.StatCurSlot", CharacterTemplate.StatCurSlot);
    47.     CharacterTemplate.StatCurSlot = CharAmount;
    48.     LoadNextLevel ();
    49.  
    50.  
    51.     }
    52.     if(GUI.Button(Rect(Screen.width - HumanBoxLoc.x, Screen.height - HumanBoxLoc.y, RaceBoxW, RaceBoxH), RaceHuman)) {
    53.                                 humanClicked = true;
    54.                                 NarlonClicked = false;
    55.                                 BaseCharacter.Race = "Human";
    56.                                
    57.                                
    58.                             }  
    59.    
    60.     if(GUI.Button(Rect(Screen.width - NarlonBoxLoc.x, Screen.height - NarlonBoxLoc.y, RaceBoxW, RaceBoxH),RaceNarlon)) {
    61.                                 humanClicked = false;
    62.                                 NarlonClicked = true;
    63.                                 BaseCharacter.Race = "Narlon";
    64.                            
    65.                             }
    66.        
    67.     if(humanClicked) {
    68.             GUI.Box(Rect(Screen.width - infoBox.x, Screen.height - infoBox.y, infoBoxX, infoBoxY), RaceHumanDescription);
    69.             BaseCharacter.Race = "Human";
    70.            
    71.                 if(GUI.Button(Rect(Screen.width - C1.x, Screen.height - C1.y, C2, C3), "Shamen")) {
    72.            
    73.             ClickedShamen = true;
    74.             ClickedWarrior = false;
    75.             BaseCharacter.Class = "Shamen";
    76.            
    77.             }
    78.            
    79.         }
    80.     if(NarlonClicked) {
    81.             GUI.Box(Rect(Screen.width - infoBox.x, Screen.height - infoBox.y, infoBoxX, infoBoxY), RaceNarlonDescription);
    82.             BaseCharacter.Race = "Narlon";
    83.                 if(GUI.Button(Rect(Screen.width - C1.x, Screen.height - C1.y, C2, C3), "Warrior")) {
    84.         ClickedShamen = false;
    85.         ClickedWarrior = true;
    86.         BaseCharacter.Class = "Warrior";
    87.            
    88.            
    89.             }
    90.     }
    91.  
    92.    
    93. }
    94.  
    95. function LoadNextLevel () {
    96. yield WaitForSeconds (1);
    97.     Application.LoadLevel ("CharacterSelect");
    98.    
    99. }
    BaseCharacter.js

    Code (csharp):
    1.  
    2. // 0 = null, 1 = race, 2 = class, 3 =  Level, 4 = Admin
    3.  
    4.  
    5. static var Race = "";
    6. static var Class = "";
    7. var CharacterBase = new Array ("null","Race","Class");
    8. var RaceNP = Race;
    9. var ClassNP = Class;
    10.  
    11. static var Level = 1;
    12. var LevelNP = Level;
    13.  
    14. var CurRace =  CharacterBase[1];
    15. var CurClass = CharacterBase[2];
    16.  
    17.  
    18. // Races
    19. private var Human =  "Human";
    20. private var Narlon = "Narlon";
    21.  
    22. // clases
    23. private var Warrior = "Warrior";
    24. private var Shamen = "Shamen";
    25.  
    26. function Start () {
    27. PlayerPrefs.GetString("Race");
    28. PlayerPrefs.GetString("Class");
    29. CharacterBase[3] = Level;
    30. LevelNP = Level;
    31.  
    32. }
    33.  
    34. function Update () {
    35. var RaceNP = Race;
    36. var ClassNP = Class;
    37.             if(Input.GetKeyDown("l")) {
    38.             print(PlayerPrefs.GetString("RaceNP"));
    39.             print(PlayerPrefs.GetString("ClassNP"));
    40.             print(CharacterBase);
    41.                                     }
    42.    
    43.    
    44.     if(RaceNP == Human) {
    45.     CharacterBase[1] =  Human;
    46.     PlayerPrefs.SetString("RaceNP", RaceNP);
    47.     }
    48.     if(RaceNP == Narlon) {
    49.     CharacterBase[1]  = Narlon;
    50.     PlayerPrefs.SetString("RaceNP", RaceNP);
    51.     }
    52.     if(ClassNP == Warrior) {
    53.     CharacterBase[2] =  Warrior;
    54.     PlayerPrefs.SetString("ClassNP", ClassNP);
    55.     }
    56.     if(Class == Shamen) {
    57.     CharacterBase[2] = Shamen;
    58.     PlayerPrefs.SetString("ClassNP", ClassNP);
    59.     }
    60. RaceNP = Race;
    61.  
    62. }
    63.  
    64.  
    65.  
    CharacterTemplate.js

    Code (csharp):
    1. var CB : Vector2 = Vector2 (320,600);
    2. var CB1 = 300;
    3. var CB2 = 570;
    4.  
    5. var S1Char = "";
    6. var S2Char = "";
    7. var S3Char = "";
    8.  
    9.  
    10. static var StatCurSlot = 0;
    11.  
    12. static var CharacterCount = new Array ();
    13.  
    14. var Slot1 = false;
    15. var Slot2 = false;
    16. var Slot3 = false;
    17.  
    18. var S1 : Vector2 = Vector2(310,550);
    19. var S1x = 280;
    20. var S1y= 40;
    21.  
    22. var S2 : Vector2 = Vector2(310,550);
    23. var S2x = 280;
    24. var S2y= 40;
    25.  
    26. var S3 : Vector2 = Vector2(310,550);
    27. var S3x = 280;
    28. var S3y= 40;
    29.  
    30. function Start () {
    31. CharacterCount.length = 12;
    32. PlayerPrefs.GetInt ("StatCurSlot");
    33. print (PlayerPrefs.GetInt("StatCurSlot"));
    34. StatChecker = StatCurSlot;
    35. }
    36.  
    37. function Update () {
    38.  
    39. }
    40.  
    41.  
    42.  
    43. function OnGUI () {
    44. GUI.Box(Rect(Screen.width - CB.x, Screen.height - CB.y, CB1,CB2), "Characters");
    45. if(StatCurSlot == 1) {
    46.         if(GUI.Button(Rect(Screen.width - S1.x,Screen.height - S1.y, S1x,S1y), S1Char)) {
    47.        
    48.         }
    49.     }
    50. }
    51.  
    52.  
    ClickCreate.js

    Code (csharp):
    1.  
    2. var C1 : Vector2 = Vector2 (310,70);
    3. var C2 = 280;
    4. var C3 = 30;
    5.  
    6.  
    7. function Update () {
    8. }
    9.  
    10. function OnGUI () {
    11. if(CharacterTemplate.StatCurSlot <= 12) {
    12.     if(GUI.Button(Rect(Screen.width -  C1.x, Screen.height - C1.y, C2,C3), "Create Character")) {
    13.     Application.LoadLevel ("character_Creation");
    14.     }
    15.     }
    16. }
    Would really apprciate help! thanks guys!

    edit: if i add a update to race class with CharAmount ++; it seems to add up and save the value. However if its added by the create button it seems to reset back to 0-1 which i really dont understand why :S
     
    Last edited: Nov 8, 2010
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    It's generally not safe to increment variables in the OnGUI function. This is because it actually gets called several times per frame so any state changes you make may be repeated. It is better to set a flag that can be picked up elsewhere. For example, you could try something like this:-
    Code (csharp):
    1. var dataChanged: boolean;
    2.  
    3. function OnGUI() {
    4.   if (GUI.Button(...) {
    5.     dataChanged = true;
    6.   }
    7. }
    8.  
    9.  
    10. function Update() {
    11.   if (dataChanged) {
    12.     // Save data here.
    13.     dataChanged = false;
    14.   }
    15. }