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 /
This question was closed May 19, 2017 at 06:40 AM by Anonymized for the following reason:

The question is no longer required, i apologize for wasting people's time, the full answer and explanation is under @AlwaysSunny 's comment.

avatar image
0
Question by Anonymized · May 09, 2017 at 05:48 AM · serializationsaveloadserializable

I can't understand Serialization, saving, loading, i already have some code written, but only now i looked into Serialization... Do i have to rewrite all of my code just to support saving?

First of all, i am a pretty much a newbie when it comes to Unity's coding and other stuff like that, and this is the first time me ever coming to the answers section, so sorry in advance if i didnt ask something right or wrong. I've been making a little 2D RPG game to mostly learn about unity and everything like that. Let's say, i already have wrote a lot of "code", that contains values, strings, changing text, and etc, etc.

But i wrote it all without saving in mind, so of course, later i am trying to take a look at how to save/load content, and then i see, that in every single tutorial for save/load systems, people write the values into that code itself, with the [Serializeable] within it, so that of course, confused me a lot.

And now i am wondering, do i have to rewrite everything i wrote, just so my game can support loading/saving?

Let's just take this for example... I have this:

 public class TA_Player_Health_and_stuff : MonoBehaviour {
 
     public TA_Player_Scripts playerScripts;
 
     int playerHealth = 100;
     int playerHealthLimit = 100;
     int playerHunger = 0;
     int playerThirst = 0;
 
     int playerXp = 0;
     int playerXpNextLvl = 50;
 
     int playerLevel = 1;
     int playerNextLevel = 2;
 
     int Strenght = 0;
     int Sight = 0;
     int Speed = 0;
     int Intelligence = 0;
     int Magic = 0;
     int Charisma = 0;
     int Luck = 0;
     int Defense = 0;
 }

So i have all these values, that i obviously have to save in a game file, but i still can't seem to understand, how it's all done at all, do i have to write it all (Every single value) into a different script with [serializeable] in it? It'll break a lot of stuff in my scene, and it would be rather upsetting if i'll have to rewrite everything from the ground up just to make saving and loading working...

Pardon me for bad english, and thanks for your time.

A little edit: Keep in mind, that i have a lot more code with other values as well, i would need to save them somehow too, but i just can't seem to understand it at all

Comment
Add comment · Show 2
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 AlwaysSunny · May 18, 2017 at 06:39 PM 0
Share

I'm assu$$anonymous$$g you have a good reason to save all this data for so many objects. The only reason you'd want to do that is if you're recording snapshot of a dynamic game world. A$$anonymous$$A the most complicated style of saving games. If you can get by with a lighter-weight save system, you should consider it.

You can't / shouldn't (de)serialize $$anonymous$$onoBehaviors. However, this is a great opportunity to learn a valuable design pattern. It works best when planned out in advance, but you can definitely use most of your already-written code. You'll have to copy/paste things around, then do some replace-in-files functions, but the code itself doesn't have to change much.

The idea is that you build your $$anonymous$$onoBehavior-derived creations in a way that allows you to populate new instances of them with saved data in a way that appears seamless to the user.

Actually, in this particular case, based on what I'm seeing, this object should not be a $$anonymous$$onoBehavior at all. Then you could slap a [System.Serializable] attribute above the class and bam - you can save it.

Let's clarify what you want a little more before I launch into a big post. I'll follow up w/ you when you comment. Please describe the kinds of objects you'll be saving in greater detail, and why.

avatar image Anonymized · May 19, 2017 at 12:49 AM 0
Share

@AlwaysSunny , my apologies, when I verified my account here, my post didn't seem to show up, so I simply assumed that it didn't pass the moderator check, so I decided to dig deeper and have already learned how to serialize some files, well, entities and etc.

Basically, I have created a script, that has different ints and other variables to save, and upon player saving, I simply make it so that main variables get saved into serialized variables, and upon loading from the file, the game loads them back from other serialized variables, I do realize that perhaps its not the best method, but so far it worked without any flaw to me. I apologize for wasting anyone's time who have replied there, but I assumed my post just didn't show up here, it kept redirecting me to the main page ins$$anonymous$$d, so. $$anonymous$$y bad. I appreciate the help though!

Gave a bigger insight into dynamic worlds, and yeah, as for the reason why it is this way, is because my little RPG game that I am using for studying unity on my own, is indeed in a little open world, so getting to know more is never a bad thing, again, I appreciate the help a lot, but unfortunately I already found a solution, thanks for your time again.

Also i apologize for bad formatting, currently posting this off mobile phone

4 Replies

  • Sort: 
avatar image
1

Answer by toddisarockstar · May 19, 2017 at 01:17 AM

there is no automated way to save data. You have to gather and save the information your game needs through code and write it yourself. the answer kind of depends on what platform you are building for. if you are making games for pc you can simply write to the user's hard drive. if you are making a game to be played in a web browser there are limitations by the firewall and so you would send infomation to a server for storage or possibly check into user prefabs.

here is a code example i wrote preiviosly for writing to a hard drive with PC:

http://answers.unity3d.com/questions/1316985/unity-55-save-user-created-levels-to-folder-please.html

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
1

Answer by Matthewj866 · May 19, 2017 at 01:19 AM

To put it in a nutshell - no, you do not need to re-write this entire class.

If I've understood the question correctly, along with the comment you left only 20 minutes ago as of me starting to write this, then this should be of some use to you:

https://docs.unity3d.com/Manual/JSONSerialization.html

The concept of serialisation as I'm sure you're aware is to get in-code object instances to some external source of some description with all the values saved at that point, SQL servers, text files or otherwise.

MonoBehaviour objects cannot be serialised by throwing it into the traditional .NET serialiser classes as MonoBehaviour does not support IEnumerable, so this leaves you with two options:

1 - use the aforementioned link and adjust your loading stuff accordingly.

2 - write your own serialisation.

Funnily enough for a recent project I actually ended up doing the latter, however im not sure how far your "new"ness goes, so I'd recommend the former if you're not comfortable with it.

I hope this 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 Vandarthul · May 22, 2017 at 03:59 AM

Hello, @Kirillian

If you would like to create your own save/load system, it's a pretty much yes. But it should not confuse you that much, and it shouldn't be hard in this case. You just need to take all your variables that you want to save in a base class and create / reference it when you need. In your case :

  public class PlayerData{
      int playerHealth = 100;
      int playerHealthLimit = 100;
      int playerHunger = 0;
      int playerThirst = 0;
  
      int playerXp = 0;
      int playerXpNextLvl = 50;
  
      int playerLevel = 1;
      int playerNextLevel = 2;
  
      int Strenght = 0;
      int Sight = 0;
      int Speed = 0;
      int Intelligence = 0;
      int Magic = 0;
      int Charisma = 0;
      int Luck = 0;
      int Defense = 0;
  }

Then

  public class TA_Player_Health_and_stuff : MonoBehaviour {
  
      public TA_Player_Scripts playerScripts;
      public PlayerData playerData;
 
 void DamagePlayer(int damage)
 {
 playerData.playerHealth -= damage;
 }
  }
 

Of course this is not right way to damage player but I gave you an example on how to do it. Then you mark PlayerData class as Serializable and serialize/deserialize it. This will bring a lot of problems you will not be able to serialize somethings like Color and need to write a custom Color variable/property. Other than that you can use assets like Easy Save which will save you from bunch of head-hurting stuff. Choice is yours.

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 Anonymized · May 19, 2017 at 02:05 PM

I apologize everyone, i have already answered here under @AlwaysSunny 's commentary, it includes the explanation, sorry for wasting everyone's time, i appreciate the help however!

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

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Custom Editor: Weird reset when clicking Play 1 Answer

Serialization of Game State Into Web Server 0 Answers

Best way to save and load all objects and their parameters in a scene. 3 Answers

Custom class does not get serialized 2 Answers

Error when trying to Serialize a field that is in a class 0 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