Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 eeveelution8 · Aug 24, 2015 at 06:46 AM · playerprefssavedatasave datarpg

Making a less complicated save system.

because it never really was an issue previously, I would save character data using 8 or so different PlayerPrefs values. But I want to attempt to make an RPG, and the amount of data needed to save would be astronomically time wasting and complicated to save everything as a different value.

What I was thinking of doing, is saving it all as a single string of data, as an example if these were the characters stats:

Level 2

EXP 12

HP 23/30

Strength 4

Intelligence 5

Speed 3

Charisma 6

It would save a string labeled as "0212023030040503060"

Saving the string would be easy, but I do not have the know how to have the game distinguish what parts of the string it needs to fill out the stats. How can I do this, or better yet, is there an easier way to do it?

Comment
Add comment · Show 1
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 savlon · Aug 24, 2015 at 07:38 AM 0
Share

Depending on how big your game is, you could create a "CharacterStats" class that is serializable. Then save it to text file utilizing a binary formatter. This way, your player data is harder to alter if a user was to gain access to the file. Or if you decide to use the string method, then when saving, simply place a split char such as a comma or hyphen to distinguish which stat is which in the save string.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by musaranya · Aug 24, 2015 at 07:43 AM

Hi @eeveelution8,

I don't know if it's a good approach or not but I achieve this as follows:

First I declare custom classes to hold all the info I want to persist. Note that each classe must be serializable (place the appropriate decoration before the declaration):

 [System.Serializable]
 public class MyGameState
 {
     public string idState;                     
     public int language;                         
     public List<int> listOfThings;                     
     public List<GameItem> listOfItems;             
   
     public GameState()
     {
        // Initializations...
     }
  
 }
 
 [System.Serializable]
 public class MyGameItem
 {
     public int itemId;
     public string itemName;
     public int itemAmount;
     ...
 }


After that, you can load and save the state of your game as follows:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using System;
 
 
 public void saveMyGameFile()
 {
     // Create the saveState, it can be done wherever
     MyGameState myGameState = new MyGameState();
     mgs.language=2;
     
     MyGameItem myItem = new MyGameItem();
     myItem.itemName = "stuff";
     
     myGameState.listOfItems = new List<MyGameItem>();
     myGameState.listOfItems.Add(myItem);
     ...
     
     // Save it to a file
     string myFileName = "savedFile.abc";
     BinaryFormatter bf = new BinaryFormatter ();
     FileStream file = File.Create (Application.persistentDataPath + "/" + myFileName);
     bf.Serialize (file, myGameState);
     file.Close ();
      
 }
 
 public void loadMyGameFile()
 {
     string myFileName = "savedFile.abc";
     
     if (File.Exists (Application.persistentDataPath + "/" + myFileName)) {
         
         // Load the game state
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Open(Application.persistentDataPath + "/" + myFileName, FileMode.Open);
         MyGameState myGameState = new MyGameState();
         myGameState = (MyGameState)bf.Deserialize(file);
         file.Close();    
         
         // Using the data previously loaded
         int currentLang = myGameState.language;
         
     }
 
 }

Note that this is just pseudocode and here I'm not using any type of data encryption. Hope it helps!

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 Cherno · Aug 24, 2015 at 08:14 AM

If you want to go down the serializing & BinaryFormatter road, take a look at SerializeHelper. It explains the whole procecudure in small steps and is a good base for your own save system (it's also perfectly usable on it's own).

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

28 People are following this question.

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

Related Questions

options menu UI doesnt save when switching scenes 1 Answer

How to create log by PlayerPrefs? 1 Answer

Help with RPG 2 Answers

whats the best way to save a large list of variables? 1 Answer

PlayerPrefs save after quit? 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