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 jessee03 · Jul 10, 2012 at 06:58 PM · saving

Saving and loading seperate data

I was wondering how to go about saving to separate text files depending on the save box that the User selects. My load menu is in a separate scene from the levels so I'm unsure how to tell if I select game1 to load game1 variables when the separate scene loads. I'm planning on having up to 3 different save files.

Also what's the simpilist way to save and load basic variables such as.

 var randomData : int = 5;
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

2 Replies

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

Answer by Piflik · Jul 10, 2012 at 07:42 PM

I create simple text files to save my games (after doing some magic to the variables, so it is not too easy to manipulate them) and then store in PlayerPrefs if a certain save file is existing (and the name of the level the player is in).

 import System.IO;

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



 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 range = parseInt(lData.ReadLine());
  var melee = parseInt(lData.ReadLine());
  var shields =  parseInt(lData.ReadLine());
  var mines =  parseInt(lData.ReadLine());
  var bombs =  parseInt(lData.ReadLine());
  var turbo =  parseInt(lData.ReadLine());
  var parTest = parseInt(lData.ReadLine());
  
  lData.Close();
  
  if(decrypt(parTest) == parity(decrypt(stage), decrypt(level), decrypt(exp), decrypt(plHealth), decrypt(lives), decrypt(maxRange), decrypt(maxMelee), decrypt(range), decrypt(melee), decrypt(shields), decrypt(mines), decrypt(bombs), decrypt(turbo))) {
     GameObject.FindWithTag("Player").SendMessage("freeWP");
 
 
 PlayerStatus.level = decrypt(level);
  PlayerStatus.expToLevel = PlayerStatus.expToLevelArr[PlayerStatus.level - 1];
  PlayerStatus.exp = decrypt(exp);
  PlayerStatus.plHealthMax = 10 * PlayerStatus.multArr[PlayerStatus.level-1];
  PlayerStatus.plHealth = decrypt(plHealth);
  PlayerStatus.lives = decrypt(lives);
  PlayerStatus.maxRange = decrypt(maxRange);
  PlayerStatus.maxMelee = decrypt(maxMelee);
  PlayerStatus.range = decrypt(range);
  PlayerStatus.melee = decrypt(melee);
  PlayerStatus.pick[0] = decrypt(shields);
  PlayerStatus.pick[1] = decrypt(mines);
  PlayerStatus.pick[2] = decrypt(bombs);
  PlayerStatus.pick[3] = decrypt(turbo);
  
  MainMenu.gameOver = false;
  statics.turn = 0;
  
  Application.LoadLevel(decrypt(stage));
      }
     }

I only allow to save at the beginning of a level.

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 jessee03 · Jul 10, 2012 at 07:46 PM 0
Share

Is it just as simple to load up all the variables when the game starts?

avatar image jessee03 · Jul 10, 2012 at 08:41 PM 0
Share

Thanks :P

avatar image
2

Answer by delstrega · Jul 10, 2012 at 07:29 PM

The easiest way to save data I guess is using PlayerPrefs. The wiki has a powerful script that allows for easily saving and loading arrays:

http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs2

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

saving class array states 1 Answer

Saving a game for the player 2 Answers

Accessing Functions from other Scripts 1 Answer

saving volume between scenes 2 Answers

[Please Help!]How do I make a player save their position when I pause the game? 3 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