Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Way to save one specific object?

Discussion in 'Scripting' started by Vanetavi, Feb 25, 2017.

  1. Vanetavi

    Vanetavi

    Joined:
    Jun 24, 2014
    Posts:
    18
    Quick question, I'm currently trying to do a dungeon for a metroidvania game I'm developing, and one of the things featured in it are chests. And I'm trying to set up a chest script for it. One problem though is that I'm wondering how to make it so the game saves so it knows which chests have already been opened and which ones haven't, rather than the problem I'm having with all the chests opening together when only one has been opened. Is there a specific method I should do for this or so? Thank you for reading.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    You basically have to choose a method to do so, and then load it up. There are many ways of doing this, which can include just saving out the name of the chest once it's been open. Depending on the number of chest, you can do it just in playerprefs if it's just a few and you'll be fine with that. Otherwise, you may have to use a save system that is a bit more organized. The end result is find a way to distinguish between the chest, then compare it to some sort of save that will tell you if it should or shouldn't be open.
     
    Vanetavi likes this.
  3. GameDevRick

    GameDevRick

    Joined:
    Oct 8, 2011
    Posts:
    269
    When you say save, do you mean save to file or do you mean to remember for that instance of the game?
    Are you trying to solve your problem of all chests opening when you trigger one by making a record of the
    "has been opened" state, or do you simply need to track that information? Tracking the opened state can
    be as easy as creating a boolean variable that changes when a chest is opened. But, I suspect that you may
    have another sort of problem going on if all chests are opening.

    Can you post your code for your chest script and describe how you set them up in the scene?
    Be sure to use code tags from the "Insert code" button above.
     
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Hope you don't mind me hopping in, I created a thread on this exact subject and got lots of help from programmers on the issue, it should help you out as well, it's specifically to do with XML but there's a bit of code in JSON floating around there.

    https://forum.unity3d.com/threads/please-help-me-serialize-gameobjects-dont-advertise-assets.409098/

    You may think you're asking for something simple, but you'll often get very vague answers from other people because it's such a large subject they'll want to know exactly what you intend to do. What you're asking for if you want to do it properly involves dumping the game state ( All of your current scene data ) into a file so that the game remembers that your chests have been opened and then you can save that, then load it back in like a proper save/load function.

    I'm sure there will be a way of getting the game perhaps through playerprefs to remember which chests have been opened and so on just for your particular project, but honestly. I think that just learning saving and loading will probably be better for the long run if you intend to make games that are based on saving progression because then you can just do what all the other programmers are doing and just dump everything in a save file without having to worry about individual objects as much unless you're doing something special.

    Oh and don't worry, I tested the code from HiddenMonk in particular and talked with him about it, it all definitely works.
     
    Last edited: Feb 26, 2017
  5. Vanetavi

    Vanetavi

    Joined:
    Jun 24, 2014
    Posts:
    18
    I mean to save on a file. And yeah, the problem is that I want it to work so that the game remembers which chests have already been opened and which chests haven't, instead it keeps opening all the chests that have the script attached to it even when I opened just one separate chest. I've tried doing something like 'PlayerPrefs.SetInt (('And then name of a public string here'));' But when I do that, it doesn't choose to set it up. It only works when I do just a regular string rather than a public custom string.

    I would show a script of it but I changed some things around on it and trying to try methods on it.
     
  6. Vanetavi

    Vanetavi

    Joined:
    Jun 24, 2014
    Posts:
    18
    I've tried doing something like 'PlayerPrefs.SetInt (('And then name of a public string here'));' and then 'Opened = PlayerPrefs.GetInt(('Same public string used earlier'));' But when I do that, it doesn't choose to set it up. It only works when I do just a regular string rather than a public custom string.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Code (CSharp):
    1. public string chestName; //Name of the chest
    2. public int chestOpened; //State of chest. 0 for closed, 1 for opened
    3. PlayerPrefs.SetInt(chestName, chestOpened);
    This should work fine if you are going that route.

    You could also just use PlayerPrefs.HasKey to check if there is even a key for the chest. If there is, then it's opened. Then the state wouldn't even matter at that point. There are other ways. Like saving a list out to a string and saving that string to a playerpref. The main thing is your chest should each have a unique name so they don't all get opened.