Saving Data in Unity: Custom Binary Files

แชร์
ฝัง

ความคิดเห็น • 289

  • @Chubzdoomer
    @Chubzdoomer 7 ปีที่แล้ว +83

    One of the clearest and most straight-forward Unity tutorials I've ever seen on this subject. Thank you.

  • @minecraftoutrage
    @minecraftoutrage 7 ปีที่แล้ว +24

    You know when you spend countless hours trying to research a new type of code and then find a video that explains it easily? This is that video.

  • @Nergosu36
    @Nergosu36 6 ปีที่แล้ว +8

    I watched 5 tutorials about saving, different ways... And only your works, thank you, wish you the best.

  • @_resul6386
    @_resul6386 5 ปีที่แล้ว

    You are a life saver sir, I agree with the other commenters, this is the most straight-forward Unity tutorial I've ever seen on this subject

  • @Amcor09
    @Amcor09 7 ปีที่แล้ว

    You make it look so easy. I've spent all day trying to work out how to save & load to file and this answered all of my questions. Subject aside, I didn't even know you could call entire classes, and that having a function in it of the same name seems to run upon the class being called. Maybe I should watch more tutorials, I've spent too long on my own limited style. No wonder my local games dev studio turned me down for a job (cry).

  • @James-mu8rp
    @James-mu8rp 5 ปีที่แล้ว

    The best video series (including the other 2) pertaining to saving. It is clear, to the point, shown in practice, and thorough!
    Gentleman and a scholar

  • @mecamargob
    @mecamargob 8 ปีที่แล้ว

    I want to say thank you, your channel is the only one I have seen with this kind of tutorials, please keep going.

    • @BoardToBitsGames
      @BoardToBitsGames  8 ปีที่แล้ว +1

      +mecamargob you're very welcome! Glad you've found them useful. Thanks for watching.

  • @toreole5831
    @toreole5831 8 ปีที่แล้ว +10

    this was exactly what i was looking for :D I tried saving my objects by writing all of their attributes one by one with a BinaryWriter, this is just so much simpler xD

  • @Gallamanji
    @Gallamanji 4 ปีที่แล้ว +2

    Thank you so much for this amazingly clear tutorial. I was wondering what I would do if, rather than saving a list of one "PlayerData", I wanted to save a list of multiple "PlayerDatas" For example, I am making a quest log, each quest in my game is a class with a set of properties, and I was thinking to use this to create a database of all the active quests and their properties and progress.

  • @RealCaptainAwesome
    @RealCaptainAwesome 4 ปีที่แล้ว

    Apart from a few optimizations in the code, this is a great base for a saving system. Thank you!

  • @tobycook9039
    @tobycook9039 7 ปีที่แล้ว +25

    I wish unity could have a built in save feature so you do
    void Save( *Save Location of choice* )
    {
    *float*
    *position of an object*
    etc
    }

    • @luiscerrada4012
      @luiscerrada4012 6 ปีที่แล้ว +4

      PlayersPrefs ? docs.unity3d.com/ScriptReference/PlayerPrefs.html

    • @Dacommenta
      @Dacommenta 6 ปีที่แล้ว +4

      Definitely Not Player Prefs. Binary is for a secure save. PlayerPrefs (prefences) should only be used to save player preferences such as controls and settings.

    • @EpochEmerge
      @EpochEmerge 6 ปีที่แล้ว +3

      It`s good for beginners, but as you dive into this process you`ll see why Unity devs did not implemented a way "out of box" to store data. It takes some time to understand things, but in the end it`s not that hard at all

    • @alan138
      @alan138 5 ปีที่แล้ว +2

      There is not a "built in" save my game method becouse is not of primordial importance thats all, and at the end you can do it yourself with binary format. The thing here is to carefully structure your game classes and scripts to be able to work well with a save&load "system". The big problem here is that in the tutorial for example this one, you are saving a simple int or a float, etc. The hedaches come when you are trying to load an AI state, so for example you are making an action game such as a Hack&Slash or a shooter, where you are probably a bit like surrounded by "enemys or allies", so how you manage to make sure that if an enemy have died and you save your progress, it doesn't come up when you load the game? Or if the AI was on a certain state like chasing you or patroling, and you want to save that state aswell as the position and rotation, and then load it and make sure that everything work fine. Well to do this in an optimized maner, you will have to write a code to store all of those "types" of runtime objects like rigidbodys that could have move frome their position, or an AI stuff. That is how i did it in the past and it work well for the most part.
      Let me explain this a bit more. What i usually do and work fine mostly, is create a script class that manage and store diferent gameobjects that i want to access when saving or loading. For this i use arrays or collections depending on wich one suit better. Then i simply loop through the array or collection doing diferent checks and comparisons. For example if an enemy died i use a bool like "bool isAlive = false" or true, and depending on that bool i already know if an enemy should be continue loading or it should just hide or destroy depending on the implementation of the game code. So deeply, if you use a pool system like i did last time y was testing this stuff, i stored inside the AIManager class diferent variables such as healt or if its alive with a bool, and inside the AI class itself i manage the state of the AI with an enumerator wich is perfect to switch quicly between AI states such as attacking or patroling or whatever. So With that being said, you need a way of accessing those variables and classes, so the easyest way i found was storing them all in a container wich is nothing else than a script wich will contain references of all the important gameobjects and components as in this case for example the AI component or the AIManager component. So your "profesional" save&load system should reference or access only that container wich already contain all the necesary references. So you access them and then with a loop you scan throw all the data and start storing or replacing depending of you are saving or loading. If you save then you only need to access those bools and float or ints within the loop and store them in the binary file or whatever format you use. Then once everything is saved you load them doing the oposite, reading from the file and applying those variables to the components and gameobjects already stored in the scene container wich reference all the things you need.
      This sounds a bit complicated at first, but in reality to do this you only need to understand how arrays and collections work and how to read those datas by code. One example will be like "public AI[] enemys;" you set something like that in the container class script, then to read it you could iterate with a foreach() loop. I hope i explained something usefull, if not them i will try better to any question.

  • @77netwar
    @77netwar 7 ปีที่แล้ว

    Great video. Facu Farias 's comments were also very insightful when it comes to making a save system that can handle multiple data types. It seems he forgot to mention his edits to the Player Script (i.e the Load function/button). My Player script ended up as the following (objective was to save/load ints and floats):
    using System.Collections;
    using UnityEngine;
    public class Player : MonoBehaviour
    {
    public int attack = 1;
    public int health = 1;
    public int countHealth = 0;
    public float costHealth;
    public float baseCostHealth;
    public void Save()
    {
    SaveLoadManager.SavePlayer(this);
    }
    public void Load()
    {
    int[] loadedStats = SaveLoadManager.LoadInts();
    attack = loadedStats[0];
    health = loadedStats[1];
    countHealth = loadedStats[2];
    float[] loadedStats2 = SaveLoadManager.LoadFloats();
    costHealth = loadedStats2[0];
    baseCostHealth = loadedStats2[1];
    }
    }
    In the SaveLoadManager (again, read Facu Farias 's comments for the basic structure):
    [Serializable]
    public class PlayerData
    {
    public int[] stats;
    public float[] stats2;
    public PlayerData(Player player)
    {
    stats = new int[3];
    stats[0] = player.attack;
    stats[1] = player.health;
    stats[2] = player.countHealth;

    stats2 = new float[2];
    stats2[0] = player.costHealth;
    stats2[1] = player.baseCostHealth;
    }

  • @FunnyVideos-ni4iu
    @FunnyVideos-ni4iu 8 ปีที่แล้ว +2

    Hey Ben, thanks a ton for this! very nicely explained and best tutorial out there for saving/loading data in custom files imo, you rock bro!!

  • @gobber121
    @gobber121 7 ปีที่แล้ว

    Love your work dude, thank you for covering the same thing (Saving a game) in multiple ways. It's a great thing to do and I hope other tutorials follow your suit to show there is always more than one way of doing anything. Subbed can not wait to see more for your work.

  • @reindeboer7374
    @reindeboer7374 5 ปีที่แล้ว +1

    Thank you so much, this is working as butter and really efficient.
    Best of all, from now on I know what everything means.
    I really appreciate your explanation!

  • @treyshaw3460
    @treyshaw3460 6 ปีที่แล้ว

    Fantastic explanation. I've been struggling to get this work with just the documentation but this made it simple and easy! Thank you!!

  • @Jerry-rv4he
    @Jerry-rv4he 7 ปีที่แล้ว +1

    Amazing. This left me speechless. THIS... is game programming at its finest. Thanks a ton for this tutorial. Your one video explained what many tutorials couldn't do easily.

  • @Supergoed1
    @Supergoed1 7 ปีที่แล้ว +8

    I am getting this error: Assets/SaveLoadManager.cs(21,22): error CS0161: `SaveLoadManager.LoadPlayer()': not all code paths return a value

    • @lostconflict9369
      @lostconflict9369 7 ปีที่แล้ว +8

      Supertijs1 M8, I had this problem to, but with a simple search on the internet I found the solution, but all you have to do is put 'return null;' outside of the if statements. But before you do this, make sure you have the else statements,19:33.
      Hope I helped :D

    • @Supergoed1
      @Supergoed1 7 ปีที่แล้ว +1

      It worked tanks man!

    • @lostconflict9369
      @lostconflict9369 7 ปีที่แล้ว

      Supertijs1 Glad I could help!

    • @morphmedia2377
      @morphmedia2377 6 ปีที่แล้ว +1

      Thanks for the fix! I was lost for a second.

    • @asepdarmawan8793
      @asepdarmawan8793 6 ปีที่แล้ว

      ty bro

  • @janmatyoo3506
    @janmatyoo3506 7 ปีที่แล้ว

    Very awesome tutorial! I just made some tweaks and now I have a working save and load! Thank you man :)

  • @joshlovesfood
    @joshlovesfood 4 ปีที่แล้ว

    Clear, concise, excellent. I am subscribed, thank you for sharing this information.

  • @Adidaas
    @Adidaas 4 ปีที่แล้ว

    Thanks for the quick and easy tutorial man! Very clear and concise.

  • @takalashna5123
    @takalashna5123 7 ปีที่แล้ว

    It was really a great tutorial. I was struggling with data saving.
    Cheers to you my friend! Thank you

  • @AdamX21198
    @AdamX21198 8 ปีที่แล้ว +2

    Could there be a way to save these files as cookies as a workaround for Web-Player/WebGL builds?

  • @BestJohnEver
    @BestJohnEver 6 ปีที่แล้ว

    Thanks for the class! Very easy to follow and well explained! Great job!

  • @zflwolfgaming7985
    @zflwolfgaming7985 6 ปีที่แล้ว

    explained everything been trying to fuiger this out for ages well done

  • @dancer4723
    @dancer4723 8 ปีที่แล้ว +4

    Hi loved your video but i have a question. Will this work on mobile devices as well?

    • @BoardToBitsGames
      @BoardToBitsGames  8 ปีที่แล้ว +4

      It should. The nice thing about persistentDataPath is that it finds a path to save files regardless of platform

  • @SethHamburg
    @SethHamburg 6 ปีที่แล้ว +1

    Doesn't work for me: LoadPlayer() is red underlined with remark "not all Codepathes return a value".

  • @MrNsaysHi
    @MrNsaysHi 6 ปีที่แล้ว

    Greetings, I've got a problem, that I hope you can help me with :\
    when I try to save: Object reference not set to an instance of an object.
    and when I load: serializationStream supports seeking, but its length is 0.
    Apparently, it never saves correctly. and when I load, it returns an empty array.
    I would be very grateful if you can help me out, thanx in advance.

  • @LoOnarForge
    @LoOnarForge 7 ปีที่แล้ว

    Well done tutorial, Clear and easy to understand. Thanks!

  • @stefansokolowski
    @stefansokolowski 4 ปีที่แล้ว

    how can I pass static variables into the player class so I can save those static variables? I can't seem to get this to work across different scenes

  • @nathancoupal5059
    @nathancoupal5059 4 ปีที่แล้ว

    Thank you so much. Able to get back on the horse because of this video. Means a lot :)

  • @jeremyboetticher5640
    @jeremyboetticher5640 6 ปีที่แล้ว +2

    What if I want to load more than just an int[] from said Player? If I want to load both doubles and ints from Player, what do I do to the LoadPlayer function?

    • @Amanda-zx7eh
      @Amanda-zx7eh 6 ปีที่แล้ว +2

      This is one way to do it:
      By using ref you are telling the method to alter the passed in variables not creating new ones.
      public static void LoadWave(ref int number, ref int[] enemyTypes, ref int[] numEnemies)
      {
      if(File.Exists(Application.persistentDataPath + "/waves.config"))
      {
      BinaryFormatter bf = new BinaryFormatter();
      FileStream stream = new FileStream(Application.persistentDataPath + "/waves.config", FileMode.Open);
      WaveData data = bf.Deserialize(stream) as WaveData;
      number = data.number;
      enemyTypes = data.enemyTypes;
      numEnemies = data.numEnemies;
      stream.Close();
      }
      }
      and then its as simple as:
      public void Load(){
      SaveLoadManager.LoadWave(ref number, ref enemyTypes, ref numEnemies);
      }

    • @jeremyboetticher5640
      @jeremyboetticher5640 6 ปีที่แล้ว

      Amanda Priest I figured it out. But thanks for the reply.

  • @PcKaffe
    @PcKaffe 3 ปีที่แล้ว

    Great video. Gonna play with this tomorrow. But Im wondering. I want to save a array of scriptable objects. Would that be possible to save like this?

  • @corriedotdev
    @corriedotdev 7 ปีที่แล้ว

    So for the initial file creation you need to call save. Im getting a null reference exception on the game stats object being passed into it. In your example its the player object being passed into it? Helping me a tonne here dude, no idea why its not knowing the player object. Im calling the load(); at launch so everything is populated.
    EDIT: SO I have a check that calls the load, you know where you put the else if sav doesnt exist? returns an array of size 1, if this is the case the code will generate a save. Is this the best way to make a first time save? Seems like returning an array of a particular size isnt right

  • @tonyrigatoni766
    @tonyrigatoni766 6 ปีที่แล้ว

    I like this tutorial a lot! I dont understand why you serialized an array of ints instead of just serializing each int separately. Now, if you wanted to add or remove an attribute from your player class, you would have array sizes to modify in three different places...

  • @elijahjns81
    @elijahjns81 6 ปีที่แล้ว

    NICE! Thanks for making this video. Subscribed and my thumb is oriented upward.

  • @CrimsonYetiCartoons
    @CrimsonYetiCartoons 6 ปีที่แล้ว

    Any way we could get this project file? Would like to see the player scripts in detail...

  • @Matthew.1994
    @Matthew.1994 7 ปีที่แล้ว +1

    how can i create a save array of objects that i picked up and when i load the game again, the objects will get deacivated (with setactive false)?

    • @spud4908
      @spud4908 6 ปีที่แล้ว +1

      You could give the game objects you picked up a tag, then store that tag as a string and when you load it back up find all the gameobjects that has that tag. Sorry this response is late but hope it helps :)

  • @Saryk360
    @Saryk360 6 ปีที่แล้ว

    Hey !
    Awesome video, can you tell me if it is possible to save imbricated objects this way ?
    An example out of the top of my head would be you have a system with planets, and planets have buildings : I can save that hierarchy in XML, but is binary serialization a functional solution ?

  • @Lajlaj
    @Lajlaj 3 ปีที่แล้ว

    Works so far! But I have a question, what if I want to delete the save file on a button, and replace it with a new file with default values, such as starting player back to the beginning of the game, 3 hearts etc etc?

  • @MasterofGalaxies4628
    @MasterofGalaxies4628 4 ปีที่แล้ว

    So here's my question: Do you have to encase all data you want to save in a single serializable class, or can you write multiple different classes to the same file, and if so, how do you pull that data back into the game both as a whole and as individual classes?

    • @mandisaw
      @mandisaw 3 ปีที่แล้ว

      You can have inner classes within that base PlayerData class, so long as they too are serializable. You could also maintain multiple types of save files for different types of data, and distinguish them with file extensions, the same way text, Word, and XML are all "text" but use different read-write handlers.

  • @rickbeniers667
    @rickbeniers667 5 ปีที่แล้ว

    I am getting an error at the end of the loading function, my Arraylist is named savedData but when I type return Data.savedData it gives a conversion error.
    Cannot implicitly convert type 'System.Collections.ArrayList' to 'PlayerData'
    this is causing my Arraylist only to retrieve the latest inserted data from the binary file and not the whole ArrayList.
    does anybody have a fix?

  • @darkman237
    @darkman237 8 ปีที่แล้ว

    How would this work with lists? Load in a list, add a new one to it and save the new data? Would you use append?

  • @Firestar-rm8df
    @Firestar-rm8df 6 ปีที่แล้ว

    I actually prefer this to xml. Is there a good way to add mod support for this system? And mod support for unity games in general?

  • @runqiangong2679
    @runqiangong2679 7 ปีที่แล้ว

    Is there a way to transfer data between different instances of the game and have it saved locally on the computers of the players?

  • @cigarettesafter9435
    @cigarettesafter9435 3 ปีที่แล้ว

    I get this error: NullReferenceException: Object reference not set to an instance of an object
    How do I fix it please? I've been struggling with it..

  • @giraffge
    @giraffge 4 ปีที่แล้ว

    yo! This just saved my life. Thank you brother.

  • @lablabs3726
    @lablabs3726 5 ปีที่แล้ว +1

    what about position of player or if change scenes?

  • @Samotnick1
    @Samotnick1 6 ปีที่แล้ว

    is it possible to serialize list of GameObject's that way?? I'm fighting with this for some time now and i'm loosing :(

  • @DayMoniakkEdits
    @DayMoniakkEdits 6 ปีที่แล้ว

    I made 3 arrays of int in the SaveLoadManager, this works well, I added one more but for some reasons it don't save or don't load, I don't know why because I have the exact same script as yours.
    C# is the most mysterious langage I've ever seen

  • @amritpatel3324
    @amritpatel3324 4 ปีที่แล้ว

    Thanks for the tutorial but I have a question->what should I do if I need to save data for different levels

  • @AriesT1
    @AriesT1 7 ปีที่แล้ว

    This is a very good tutorial, thank you so much for that. However, what is there to do if you want to store values of different types (int and float) or want to create multiple save files like one for global values, one for local level-based values or similar?

  • @auke7658
    @auke7658 7 ปีที่แล้ว

    If i wanted to request some of this data now, but just the parts that my hero can use (like if my hero 1 learned a spell my hero 2 couldn't get that data)?

  • @omiorahman6283
    @omiorahman6283 4 ปีที่แล้ว

    If I am using third person shooterwith enter exit car and cg pictures cutcenes and movies using video player multiple scenes
    Will this work and how much I need to save .

  • @VGamingJunkieVT
    @VGamingJunkieVT 6 ปีที่แล้ว

    Awesome tutorial! One question, though, how would I go about saving things like items and enemies that would be in far greater numbers and may or may not even still exist in the game world?

  • @77netwar
    @77netwar 7 ปีที่แล้ว

    I have a question, though: Say, I have a main menu and I want to have a 'Load Game' or 'Continue' Button there that switches me to the other scene (i.e. the actual game) when clicked and having the correct values (i.e. the values that were saved in a previous game) displayed on screen? Does this have to do with persistence of data across scenes? I know how to create a button that switches the scene (button in the first scene, i.e. scene 0):
    public void PlayGame()
    {
    SceneManager.LoadScene(1);
    }
    but not how it can simultaneously load the correct data when it switched to scene 1.

    • @BoardToBitsGames
      @BoardToBitsGames  7 ปีที่แล้ว

      +77netwar you're on the right track, you'd either need some kind of object that won't be destroyed on scene load to store the level info you want, or to use a static class or variable, which will remain across scenes

    • @77netwar
      @77netwar 7 ปีที่แล้ว

      Good to hear I am on the right track. :) Thank you!

  • @linkid2601
    @linkid2601 5 ปีที่แล้ว

    Excellent as usual!

  • @lostconflict9369
    @lostconflict9369 7 ปีที่แล้ว

    All these videos are great, really well explained. I was wondering if you could make one about json, I've heard about it, and apparently it's better than XML. But most videos out there are ether old, or badly explained. So, I was wondering if you could make a video on Json, since, once again. You videos are impeccably explained, as they get straight to the point. Pros, cons. The layout of each of these videos are just amazing! But back to the point, if you could make a video on Json, that would be amazing! Keep up the great video quality.

    • @BoardToBitsGames
      @BoardToBitsGames  7 ปีที่แล้ว

      Hi Lost, it's something I'll look into! I covered XML because I was more familiar with it, but there are definitely benefits to JSON as well. Keep an eye out in future videos!

    • @lostconflict9369
      @lostconflict9369 7 ปีที่แล้ว

      Board To Bits Games I sure will!

  • @Panice111
    @Panice111 6 ปีที่แล้ว

    It is a good idea to code a selfmade formatter onto a more cryptic type then binary? It could be even safer, but idk much about that.

  • @BOOLitGameStudio
    @BOOLitGameStudio 8 ปีที่แล้ว +4

    Great tutorial, very clean and simple. However how can I delete the save file?

    • @twisterlord665
      @twisterlord665 8 ปีที่แล้ว +4

      If you are on windows,
      1. Search for a program called "Run" and then type %appdata% including percent signs.
      2. Press enter and click out of the roaming folder and into the LocalLaw folder.
      3. Go into the folder with your company name (This is set in your player settings, if you don't know what this is then google it)
      4. Go into your game name's folder. (Again, if you don't know what this is then google it)
      5. Your file should be here. Just click it and press "Delete" on your keyboard.
      I don't know where it is for mac or linux, so i'm sorry 'bout that.
      Hope this helps.

    • @jaythewolf7216
      @jaythewolf7216 7 ปีที่แล้ว

      thank you. i needed that.

    • @dunyaguzeldir4753
      @dunyaguzeldir4753 7 ปีที่แล้ว

      Hello,when i closebmy game and open it again i cant open the older files, anyone can help?

  • @elcarmi
    @elcarmi 7 ปีที่แล้ว

    @Board To Bits Games Loved the video. Very straight forward. I have a question though.. How would you apply this to PlayersPrefs?

    • @BoardToBitsGames
      @BoardToBitsGames  7 ปีที่แล้ว +1

      +elcarmi I actually have a video on PlayerPrefs. You can save some data there, but it's much more limited and less secure.

  • @dontikus4951
    @dontikus4951 6 ปีที่แล้ว

    Made a sudoku and used this files so that I could load the levels from there.
    And it works perfectly from unity editor. But when I build the APK Android won't load the files that I pre-made.
    Why?

  • @PCBOXER1
    @PCBOXER1 7 ปีที่แล้ว

    Help : Serialization exception : serializationstream supports seeking but its lenght is 0

  • @sandeeproy3126
    @sandeeproy3126 3 ปีที่แล้ว

    I want to use this system to store the data on the amazon s3 bucket , for all my players game state , but the really problem that is bothering me , what if I want to add new features to my game , how shall the file schema change can be implemented upon the existing binary files , plz someone reply

  • @Venraneld
    @Venraneld 8 ปีที่แล้ว

    This is a great video series. Really breaks things down nicely.
    I do have a question though. Do you know how to use the binary method with Javascript? I've watched many tutorials on saving recently and binary seems to be the most secure method as you mentioned, but every single tutorial I find is in C# and my whole project is in JS. I've tried to translate it myself, but I keep getting several errors that I don't know how to fix.

    • @BoardToBitsGames
      @BoardToBitsGames  8 ปีที่แล้ว

      Unfortunately I do all my coding in C#, and don't know much about JS beyond some basic stuff. If I find anything in my research, though, I'll send it your way!

    • @Venraneld
      @Venraneld 8 ปีที่แล้ว

      Thanks. I appreciate it.

  • @theairaccumulator7144
    @theairaccumulator7144 7 ปีที่แล้ว +1

    I can save 3 difrent variables and then when I load them convert them in an vector3?

    • @abuzerkucuk1771
      @abuzerkucuk1771 3 ปีที่แล้ว

      th-cam.com/video/n1HNOGTXvhk/w-d-xo.html

  • @djantony8501
    @djantony8501 6 ปีที่แล้ว

    Very useful. What if i want to save/load player data for different players ?

  • @petipois28
    @petipois28 6 ปีที่แล้ว

    I want to Load ints AND a list of strings but I can't seem to work it out - Could you show a void LoadPlayer() instead of returning just ints?

    • @petipois28
      @petipois28 6 ปีที่แล้ว

      I created 2 separate scripts - one like the video that returns a int array and one for returning a list :D

  • @dawid6211
    @dawid6211 7 ปีที่แล้ว

    Why do you not write "using" befor open the Stream? Code look more clean in that way, and u don't have to remember of closing the stream. Anyway, very good tutorial. But what when I want to save on example 100 enemy position and it's stats? Make save in this way can take very much time. There is no faster way to make save?

  • @locknessko
    @locknessko 6 ปีที่แล้ว

    YOU ARE THE BEST THANK YOU THIS IS THE BEST TUTORIAL EVER!

  • @TheCrustiCroc
    @TheCrustiCroc 7 ปีที่แล้ว

    Nice tutorial, very instructive but I still have one question. If I have multiple objects that I have to save, like position of my Player, some enemies, booleans for accepted quests and so on. What would you recommend? Should I create multiple binary files for different objects or is it possible to put everything into one single binary file? Could you please give us some more examples on saving? This is a very complex topic :/

    • @BoardToBitsGames
      @BoardToBitsGames  7 ปีที่แล้ว

      You could (and likely should) handle it all in one file. Presumably, you'd have a Game Manager or some other script keeping track of these various stats, lists of enemies, etc., which you could iterate through and save to binary.
      That said, keeping track of all that falls more under the idea of project management or data management within your game design, which I hope to cover soon on the channel. Thanks for watching!

    • @TheCrustiCroc
      @TheCrustiCroc 7 ปีที่แล้ว

      Alright, thanks for the quick reply! Can't wait to learn more efficient ways and tricks. Keep up the good work!

  • @systemx17
    @systemx17 6 ปีที่แล้ว

    If I wanted to store block data for a minecraft like game such as blockID, blockType, X, Y, Z - how would I serialize multiple items into the one map file?

    • @systemx17
      @systemx17 6 ปีที่แล้ว

      Worked this out, just created a LIst of my data and used the whole thing instead of trying to Serialized each block. Great tutorial. It explain things enough that I could work that out on my own after a very short time. You "saved" my game! (pun intended)

  • @001zeal
    @001zeal 7 ปีที่แล้ว

    Hey , if i save the file in the assets folder , will it share the data between multiple phones if its an android game?

    • @VGamingJunkieVT
      @VGamingJunkieVT 6 ปีที่แล้ว

      You'd have to have a cloud system for storing the saves online if you'd want to do that.

  • @KNN-s2v
    @KNN-s2v 7 ปีที่แล้ว

    I'm a newbie to Unity and C#, I want to ask whether this method is applicable if I develop a game in mobile phone? I mean saving and loading data from mobile phone.

    • @BoardToBitsGames
      @BoardToBitsGames  7 ปีที่แล้ว

      It should work for mobile devices. The benefit of using the Application dataPath approach to saving files is that Unity finds the right path to save to, regardless of the device to which you're building.

  • @tnkspecjvive
    @tnkspecjvive 3 ปีที่แล้ว

    Excellent excellent explanation 👌🏾

  • @MajorSmurf
    @MajorSmurf 6 ปีที่แล้ว +1

    A very nice tutorial which i have learnt from and adapted to save an objects location. How would i go about saving multiple objects and their co-ords? i dont know if anyone will respond but any help would be nice :3.

  • @Extrum
    @Extrum 5 ปีที่แล้ว

    Suggestion: declare PlayerData inside SaveLoadManager thus making it a local/inner class so that it won't be seen by intellisense therefore making everything a bit cleaner

  • @darkman237
    @darkman237 8 ปีที่แล้ว

    Thank you! That was EXREEEEEEMELY helpful!

  • @Amadeone
    @Amadeone 7 ปีที่แล้ว

    Great video, but I have a question. Can you load a game after several days and maybe even a year?

    • @BoardToBitsGames
      @BoardToBitsGames  7 ปีที่แล้ว

      Amadeone :D as long as you don't delete the file off your computer, sure

    • @Amadeone
      @Amadeone 7 ปีที่แล้ว

      thanks!

  • @HikikomoriDev
    @HikikomoriDev 8 ปีที่แล้ว

    9:15 ...Is there any way to make a separate application that can read those files outside of Unity?

    • @BoardToBitsGames
      @BoardToBitsGames  8 ปีที่แล้ว +2

      Arakmatzu you could either make a separate Unity program designed just to read those save files or you could program something outside of Unity, but that's beyond my knowledge as a programmer unfortunately.

    • @HikikomoriDev
      @HikikomoriDev 8 ปีที่แล้ว

      Board To Bits Games Thanks.

  • @lemetamax
    @lemetamax 6 ปีที่แล้ว

    How do I add a score, to a previously existing score for example, I have 8 as my score, and saved in the Binaryformatter, then I want when I click the add button, it adds 8 again to the Binaryformatter data, so that when I load the Binaryformatter data, it displays 16 and not 8. Thank you.

    • @avananana
      @avananana 6 ปีที่แล้ว

      A bit late but maybe I can give you an idea if you haven't figured it out yet.
      What I'd do is load the previous score into your game, then you can play and gain additional score. Once you save your game again, make sure to save the current score and not a set value. It sounds simpler than it is but it shouldn't be too hard if you've got a pretty good idea of how saving data to a file works.

  • @Tikodev
    @Tikodev 6 ปีที่แล้ว

    How could I save a list of scriptable objects?

  • @alexaquaza2208
    @alexaquaza2208 7 ปีที่แล้ว

    Can this technique be used for android games?

  • @creeperbot6597
    @creeperbot6597 6 ปีที่แล้ว

    Is there a way to make it save multiple instances (For e.g. I have 5 instances and I want to save the X, Y and Z position of them all). There isn't a specific No. on how many instances I have.

    • @BoardToBitsGames
      @BoardToBitsGames  6 ปีที่แล้ว

      +creeperbot65 you can serialize a list of instances, as long as the class or struct is serializable

    • @creeperbot6597
      @creeperbot6597 6 ปีที่แล้ว

      I had a look at Hexa Game Creator's files (By Matt's Creations) and I had a look into the game files. I found .txt files with simple values set out like this:
      (File name is XPos)
      3936
      6527
      123
      2345
      66754
      56
      and I realised it's just like an array and each instance that loads in gets the corresponding array number
      for example, Instance No.5 will load 66754 for it's X Position.
      Is there no easier way to save an array to a file?
      Thanks!

  • @rickbeniers667
    @rickbeniers667 5 ปีที่แล้ว

    how do I do this with an Arraylist or List?

  • @deerby1500
    @deerby1500 7 ปีที่แล้ว

    Is there a way to automatically load this when the scene starts or load it from a title scene?

    • @The28studio
      @The28studio 7 ปีที่แล้ว

      use docs.unity3d.com/ScriptReference/MonoBehaviour.OnEnable.html

  • @xxmaniakaxx9781
    @xxmaniakaxx9781 6 ปีที่แล้ว

    Hello, how can we do this making this auto, i mean game auto saving every for example 5-10 seconds. I mean the part to pull this out of buttons ?

    • @BoardToBitsGames
      @BoardToBitsGames  6 ปีที่แล้ว

      +xXManiakaXx you could start a coroutine when your game starts that would loop every 5-10 seconds (e.g., yield return new WaitForSeconds(5f) ) and call the save function each loop.

  • @Adomas_B
    @Adomas_B 3 ปีที่แล้ว

    Is there a way to reverse this method?

  • @purpleice2343
    @purpleice2343 8 ปีที่แล้ว

    "Which fieldtypes can we serialize?
    References to objects that derive from UnityEngine.Object"
    ... So can I store, for example, queue of structs inside of that file? Could I save already random generated level data right into the file instead of building it in scene and then reading from that file whenever I want? Would I need to do anything else but save the class made purely for saving just to save that dictionary and revert it or does it need some workaround? It might sound like a stupid question but I have no idea what I am doing.
    If I could get it to work I then could generate some things in a hacky way, convert it to the building queue like with everything else I do and save it into this kind of file, then delete the code once and forever (just kidding, probably better just remove it from project until something goes wrong.)

    • @BoardToBitsGames
      @BoardToBitsGames  8 ปีที่แล้ว

      To the best of my knowledge, you can save queues and lists, dictionaries may need to be broken into two separate lists before saving (and rejoined when loaded). You could also definitely generate a level and save it into a file instead of rendering it in your scene. And you should be able to save any class you make as long as you make it serializable.

    • @purpleice2343
      @purpleice2343 8 ปีที่แล้ว

      Board To Bits Games Okay, got it, thank you for these explanations.

  • @johannesvorkinn
    @johannesvorkinn 8 ปีที่แล้ว

    I can't access my variables in the PlayerData class... I made an int static and it worked, however, thats inconvinent. Is there a fix?

    • @BoardToBitsGames
      @BoardToBitsGames  8 ปีที่แล้ว

      It sounds like you're trying to access the variable directly from the PlayerData type (e.g., PlayerData.stat) You need to have a reference to an instance of PlayerData, and then you can access the public variable from that instance without making it static, for example:
      PlayerData pd = new PlayerData();
      Debug.Log(pd.stat);
      This will return whatever value is in the instance's stat variable. Assuming your load method returns a PlayerData instance, you could use that method instead of new PlayerData() in the example above to get the saved data.

    • @johannesvorkinn
      @johannesvorkinn 8 ปีที่แล้ว

      Holy crap! you're fast at responding and you were right! I forgot that step. I'd sub again if I could :D

  • @NDSno1
    @NDSno1 8 ปีที่แล้ว

    Great video. I was struggling with how to save player data. Right now the thing I'm struggling is that my Player class attributes are just mess right now. I'm doing a racing game where I use XML to store list of cars with default values. After that player use money to buy cars and tune the cars. So I'm storing money amount, car owned and modified attributes of the owned car into the binary file. However, this will pose a problem later on as the car list gets bigger, and the attributes will just pile up (number of cars and modified values gets bigger). Do you have any suggestion? Thank you so much.

    • @BoardToBitsGames
      @BoardToBitsGames  8 ปีที่แล้ว +1

      The good news is that you can serialize lists (as long as the contents are serializable), so even if you have a growing list of something like cars, you should be able to simply add them to a list and then serialize them in a save file.
      Like most of programming, it's just a matter of making sure each part works (adding car to list, adding modification to car) and the program ultimately handles doing it all to multiple cars. Best suggestion I can give you is to keep going as you are, but take it step by step to make sure each step is working properly.

    • @NDSno1
      @NDSno1 8 ปีที่แล้ว

      Thanks so much for your reply. Right now I'm combining XML and Binary. XML file is used to store the car database, so later on when other people on my team decided to update the game with more cars, we can just add into the XML.
      Binary is used for user data, which will be storing list of UserCar class with prefabName string, so the corresponding prefab can be loaded. I have another list of Upgrade class that contain user's tuning data. When user buy car, a new UserCar will be added to the UserCar list, and also and Upgrade will be added to Upgrade list. The index will be corresponding to each other, so there is less chance of messing up upgrade data.
      However, my problem is, I have UserCar and Upgrade in separate classes (scripts), and I don't have a Player class or object like what you demonstrated in the video. And according to the video, you can only store data of 1 class only, not 2 classes in 1 file, but I would like to store list of UserCar and Upgrade in 1 file for consistency. Can you help me figure out what to do? Thank you very much

  • @salihbalkis72
    @salihbalkis72 4 ปีที่แล้ว

    Thank you. You're the best.

  • @brooklain
    @brooklain 8 ปีที่แล้ว

    Greetings, I love your channel! I haven't worked with many arrays so I am missing something about adjusting it's size. I added a few INTs to the array and adjusted the array size within the SaveLoadManager.cs stats = new int [8]; but I'm getting the error
    IndexOutOfRangeException: Array index is out of range. It gives this error at the start of my 5th new INT on the Player script. Any ideas?
    Thanks in advance!

    • @BoardToBitsGames
      @BoardToBitsGames  8 ปีที่แล้ว

      jim jones did you change the size in the constructor method? That might be overriding it back to an int[4]

    • @brooklain
      @brooklain 8 ปีที่แล้ว

      Im sorry, not a great scipter yet, trying to learn. Do I have to change the number in more places than the one Constructor: stats = new int[8] under the PlayerData?

    • @brooklain
      @brooklain 8 ปีที่แล้ว

      Very strange, after more testing i found that it still throws up the IndexOutOfRangeException but it all works perfectly in spite of the error. Not sure at all why but at least it's not a blocker for me. It saves progress great! thanks!

  • @UpheavaI
    @UpheavaI 8 ปีที่แล้ว

    awesome! just what i needed

  • @GabrielCarvv
    @GabrielCarvv 5 ปีที่แล้ว

    Amazing tutorial.

  • @Fiilis1
    @Fiilis1 6 ปีที่แล้ว

    Just wondering how does the player name be saved.

  • @dorin-b88
    @dorin-b88 6 ปีที่แล้ว

    Hi Board To Bits Games, nice tutorial...But I have a question if I want to save in the same file float, ints, Vector2/3 etc. how can we load them, we need to create separate Loaders for each? E.g:
    [Serializable]
    public class PlayerData
    {
    public int[] statInt;
    public float[] statFloat;
    public PlayerData(Player pl)
    {
    statInt = new int[2];
    statFloat = new float[2];
    statInt[0] = pl.level;
    statInt[1] = pl.health;
    statFloat[0] = pl.attack;
    statFloat[1] = pl.defence;
    }

    • @BoardToBitsGames
      @BoardToBitsGames  6 ปีที่แล้ว

      +Buraca Dorin you can just pass the individual variables back, similar to the constructor. You might use method called LoadPlayer(Player p, PlayerData data).
      Then you’d just go through each variable:
      p.level = data.statInt[0], and so forth

    • @dorin-b88
      @dorin-b88 6 ปีที่แล้ว

      Hi @Board To Bits Games, thanks for your quick reply, I managed to deserialize and load data like ints floats etc...But I not managed to extract data to use it for example like yours in UI E.g, because you create an int array LoadPlayer not a method, in this particular case I'm lost, for all the other stuff I managed to modify and adapt for my needs. Thanks!

  • @QmzApps
    @QmzApps 8 ปีที่แล้ว

    Hello, I really like how clean and helpful your videos are! I have a question regarding my project, sorry if it sounds complicated. So I'm working on a game where once the user presses a button, he gets a random card from a List of sprites, and then I remove that specific card from the List and Add it to a new one.
    Now that all works fine and all, but my question is, how should I go about saving the "unlocked" cards and load them into an inventory? (By inventory I mean I load the object into a Scrollview with a grid layout, nothing special)
    Thanks in advance!

    • @BoardToBitsGames
      @BoardToBitsGames  8 ปีที่แล้ว

      +Qmz Benchmarks I would say have an array of locked cards, with a matching array of bool's to track which cards are unlocked. Then at load time, scan the list of books and load in the appropriate cards.

    • @QmzApps
      @QmzApps 8 ปีที่แล้ว

      So could I do it with simply 0s and 1s, 1 means unlocked?

    • @BoardToBitsGames
      @BoardToBitsGames  8 ปีที่แล้ว +1

      Qmz Benchmarks you could, though a bool is a little more efficient than an int, but either would certainly work

  • @tobycook9039
    @tobycook9039 7 ปีที่แล้ว

    Im getting these errors plz help!!
    Assets/Scripts/SaveGame.cs(47,24): error CS0029: Cannot implicitly convert type `GameManager' to `int'
    Assets/Scripts/GameManager.cs(17,38): error CS0117: `SaveGame' does not contain a definition for `LoadPLayer

    • @lostconflict9369
      @lostconflict9369 6 ปีที่แล้ว

      Toby Plays Smash i you haven't fixed this, or have given up on it. Anytime you get simple errors like this, actually read what the error is saying to you, they offer a lot of information. Even telling you where the error is, and practically how to fix it. So just try.

    • @jeremyboetticher5640
      @jeremyboetticher5640 6 ปีที่แล้ว

      What's a LoadPLayer? I've only heard of LoadPlayer.

  • @morphmedia2377
    @morphmedia2377 6 ปีที่แล้ว

    You're the man!