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
0
Question by Piflik · Apr 21, 2012 at 03:44 PM · javascriptsaveload

Save/Load - Strange behaviour

I have written a basic save/load system, and it works just fine, except whenever I restart the game, the player's level increases by one. Every other property is loaded just fine. I don't have any explanation for this...

This is my code:

 import System.IO;

 static function save(slotNmbr) {
     var sPath : String = Application.persistentDataPath + "/savegame" + slotNmbr + ".txt";
     var sData : StreamWriter = new StreamWriter(sPath);
     sData.WriteLine(Application.loadedLevel);
     sData.WriteLine(PlayerStatus.level);
     sData.WriteLine(PlayerStatus.exp);
     sData.WriteLine(PlayerStatus.plHealth);
     sData.WriteLine(PlayerStatus.lives);
     sData.WriteLine(PlayerStatus.maxRange);
     sData.WriteLine(PlayerStatus.maxMelee);
     sData.WriteLine(PlayerStatus.pick[0]);
     sData.WriteLine(PlayerStatus.pick[1]);
     sData.WriteLine(PlayerStatus.pick[2]);
     sData.WriteLine(PlayerStatus.pick[3]);
     sData.Flush();
     sData.Close();
     
     PlayerPrefs.SetInt("Slot" + slotNmbr, 1);
 }

 static function load(slotNmbr) {
     var lPath : String = Application.persistentDataPath + "/savegame" + slotNmbr + ".txt";
     var lData : StreamReader = new StreamReader(lPath);
     var stage = parseInt(lData.ReadLine());
     var level =  parseInt(lData.ReadLine());
     var exp =  parseInt(lData.ReadLine());
     var plHealth =  parseInt(lData.ReadLine());
     var lives = parseInt(lData.ReadLine());
     var maxRange =  parseInt(lData.ReadLine());
     var maxMelee =  parseInt(lData.ReadLine());
     var shields =  parseInt(lData.ReadLine());
     var mines =  parseInt(lData.ReadLine());
     var bombs =  parseInt(lData.ReadLine());
     var turbo =  parseInt(lData.ReadLine());
     
     lData.Close();
     
     print(level);
     GameObject.FindWithTag("Player").SendMessage("freeWP");
     
     PlayerStatus.level = level;
     PlayerStatus.expToLevel = PlayerStatus.expToLevelArr[PlayerStatus.level - 1];
     PlayerStatus.exp = exp;
     PlayerStatus.plHealthMax = 10 * PlayerStatus.multArr[PlayerStatus.level-1];
     PlayerStatus.plHealth = plHealth;
     PlayerStatus.lives = lives;
     PlayerStatus.maxRange = maxRange;
     PlayerStatus.maxMelee = maxMelee;
     PlayerStatus.pick[0] = shields;
     PlayerStatus.pick[1] = mines;
     PlayerStatus.pick[2] = bombs;
     PlayerStatus.pick[3] = turbo;
     
     
     
     Application.LoadLevel(stage);
 }

When I load a savegame from within the game, everything works as it should.

I have a Splash Screen Scene that automatically loads a 'hub' level. This in turn checks, if there is an autosave present and if so loads that, if not loads the first level. This hub scene has all important objects that are not destroyed on load (Player, Camera, Main Menu and the AutoLoader). When I delete the key for the autosave, the first level is loaded just fine. When I then close the game and restart (or restart it in the editor), the same level is loaded (since now there is an autosave there), but suddenly the player is at level 2. The print(level); line prints 1 to the console. When I repeat this, the pplayer is level 3, and the game prints 2.

The really strange thing is that values derived from the level (like max health and a damage multiplier) are calculated to level 2 although this should only happen an levelup, so it seems that the player somehow levels up, but that doesn't make sense...just to be sure, here is the PlayerStatus script:

     //FX
 var lvlUp : Transform;

 //weapons
 var weapons : Transform[] = new Transform[6];
 static var maxRange : int = 0;
 static var maxMelee : int = 3;
 //health
 static var lives: int = 3;
 static var plHealth : int = 10;                //player health
 static var plHealthMax : int = 10;            //maximum health

 //power-ups
 static var maxPowerUp : int = 5;            //max number per power up

 static var pick : int[] = [5,5,5,5];            //availible powerups - shield, mines, bombs, turbo

 //EXP
 static var level : int = 1;
 static var maxLevel : int = 10;
 static var multiplier : float = 1.0;
 static var multArr : float[] = new float[10];
 static var exp : int = 0;
 static var expToLevel : int = 1000;
 static var expToLevelArr : int[] = new int[10];

 //weapons
 static var i : int = 0;
 static var j : int = 3;


 function Awake() {
     multArr = [1.0, 1.1, 1.3, 1.6, 2.0, 2.5, 3.0, 3.5, 4.0, 5.0];
     expToLevelArr = [1000, 1500, 2500, 4000, 6000, 8000, 10000, 15000, 20000, 25000];
     
     switch(i) {
         case 0 : 
             gameObject.Find("Blaster").renderer.enabled = true;
             gameObject.Find("Laser").renderer.enabled = false;
             gameObject.Find("Plasma").renderer.enabled = false;
         break;
         
         case 1 : 
             gameObject.Find("Blaster").renderer.enabled = false;
             gameObject.Find("Laser").renderer.enabled = true;
             gameObject.Find("Plasma").renderer.enabled = false;
         break;
         
         case 2 : 
             gameObject.Find("Blaster").renderer.enabled = false;
             gameObject.Find("Laser").renderer.enabled = false;
             gameObject.Find("Plasma").renderer.enabled = true;
         break;
     }
     GameObject.FindGameObjectWithTag("Player").GetComponent(Player).primAtt = weapons[i];
     
     switch(j) {
         case 3 : 
             gameObject.Find("Blade").renderer.enabled = true;
             gameObject.Find("Mace").renderer.enabled = false;
             gameObject.Find("Lance").renderer.enabled = false;
         break;
         
         case 4 : 
             gameObject.Find("Blade").renderer.enabled = false;
             gameObject.Find("Mace").renderer.enabled = true;
             gameObject.Find("Lance").renderer.enabled = false;
         break;
         
         case 5 : 
             gameObject.Find("Blade").renderer.enabled = false;
             gameObject.Find("Mace").renderer.enabled = false;
             gameObject.Find("Lance").renderer.enabled = true;
         break;
     }
     GameObject.FindGameObjectWithTag("Player").GetComponent(Player).secAtt = weapons[j];
 }

 function Update() {
     if(exp >= expToLevel) {
         if(level <= maxLevel){
             Instantiate(lvlUp, transform.position, Quaternion.identity);
             level++;
             exp -= expToLevel;
             multiplier = multArr[level - 1];
             expToLevel = expToLevelArr[level - 1];
             plHealthMax = 10 * multArr[level-1];
             plHealth = plHealthMax;
             transform.GetComponent("Player").marker.gameObject.renderer.material.SetFloat("_Cutoff", 1 - (parseFloat(PlayerStatus.plHealth) / parseFloat(PlayerStatus.plHealthMax)));
         }
     }
     
     if(statics.playerActive) {
         if(Input.GetKeyDown(KeyCode.R)) {
             i++;
             if(i > maxRange)
                 i = 0;
                     
             switch(i) {
                 case 0 : 
                     gameObject.Find("Blaster").renderer.enabled = true;
                     gameObject.Find("Laser").renderer.enabled = false;
                     gameObject.Find("Plasma").renderer.enabled = false;
                 break;
                 
                 case 1 : 
                     gameObject.Find("Blaster").renderer.enabled = false;
                     gameObject.Find("Laser").renderer.enabled = true;
                     gameObject.Find("Plasma").renderer.enabled = false;
                 break;
                 
                 case 2 : 
                     gameObject.Find("Blaster").renderer.enabled = false;
                     gameObject.Find("Laser").renderer.enabled = false;
                     gameObject.Find("Plasma").renderer.enabled = true;
                 break;
             }
             
             GameObject.FindGameObjectWithTag("Player").GetComponent(Player).primAtt = weapons[i];
         }
         if(Input.GetKeyDown(KeyCode.F)) {
             j++;
             if(j > maxMelee) 
                 j = 3;
                 
             switch(j) {
                 case 3 : 
                     gameObject.Find("Blade").renderer.enabled = true;
                     gameObject.Find("Mace").renderer.enabled = false;
                     gameObject.Find("Lance").renderer.enabled = false;
                 break;
                 
                 case 4 : 
                     gameObject.Find("Blade").renderer.enabled = false;
                     gameObject.Find("Mace").renderer.enabled = true;
                     gameObject.Find("Lance").renderer.enabled = false;
                 break;
                 
                 case 5 : 
                     gameObject.Find("Blade").renderer.enabled = false;
                     gameObject.Find("Mace").renderer.enabled = false;
                     gameObject.Find("Lance").renderer.enabled = true;
                 break;
             }        
                             
             GameObject.FindGameObjectWithTag("Player").GetComponent(Player).secAtt = weapons[j];
         }
     }
 }


And my AutoLoader:

 function Awake() {
     Resources.UnloadUnusedAssets ();
     if(PlayerPrefs.HasKey("Slot0"))
         SaveLoad.load(0);
     else    
         Application.LoadLevel ("test2");
 }
Comment
Add comment · Show 6
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 AchillesWF · Apr 21, 2012 at 04:33 PM 0
Share

PlayerStatus.expToLevel = PlayerStatus.expToLevelArr[PlayerStatus.level - 1]

Why -1? I would think that would get the exp needed to level "up" to be the exp needed for the previous level, and since the player will have more exp than that, he will dutifully level up in your update.

avatar image Piflik · Apr 21, 2012 at 05:46 PM 0
Share

The array starts at 0, the level starts at one. At level one I want to access the first entry in the array and that is expToLevelArr[0].

In any case, it happens at 0 exp already.

avatar image raoz · Apr 21, 2012 at 06:33 PM 1
Share

Could you please post the whole script the level up script is in. I have an idea what might be happening, but I have to see the whole script.

avatar image Piflik · Apr 21, 2012 at 07:03 PM 0
Share

I edited the post to include the complete PlayerStatus script.

The only time anything in that script is edited by other scripts (aside from load()) is when enemies die. They increase the exp by their exp-worth.

avatar image AchillesWF · Apr 21, 2012 at 07:18 PM 2
Share

You are setting the expToLevel value in the Awake() function of the AutoLoader script, which may proceed the Awake() function of the PlayerStatus script which populates the array.

Show more comments

1 Reply

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

Answer by raoz · Apr 21, 2012 at 08:31 PM

The autoload awake might be called before the PlayerStatus Awake, thus rendering the Playerload obsolete. The reason why it doesn't give you an exeptcion is that you are initializing the exptolevel array when you are defining it and it gets initialized in constructor. Thus, if you change your PlayerStatus Update to this:

 function Update() {
     if(expToLevel == 0)
     {
         expToLevel = expToLevelArr[level - 1];
     }
     else if(exp >= expToLevel) {
        if(level <= maxLevel){
          Instantiate(lvlUp, transform.position, Quaternion.identity);
          level++;
          exp -= expToLevel;
          multiplier = multArr[level - 1];
          expToLevel = expToLevelArr[level - 1];
          plHealthMax = 10 * multArr[level-1];
          plHealth = plHealthMax;
          transform.GetComponent("Player").marker.gameObject.renderer.material.SetFloat("_Cutoff", 1 - (parseFloat(PlayerStatus.plHealth) / parseFloat(PlayerStatus.plHealthMax)));
        }
     }

Then it should work.

Comment
Add comment · Show 2 · 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 Piflik · Apr 21, 2012 at 09:24 PM 0
Share

Holy shit...never would have thought of that...thanks man. Works just perfectly.

avatar image raoz · Apr 22, 2012 at 01:11 PM 0
Share

You're welcome

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to save/load game, codes doesnt do anything. not even an error. 1 Answer

Saving lots of objects (Only answer in JavaScript) 1 Answer

simple checkpoint/save/load system in javascript 4 Answers

Not loading an object if it have be retained from a previous scene. 1 Answer

Simple save/load scripting for multi level iOS game. 1 Answer


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