Forum rules - please read before posting.

Remembering Custom Scene Data

Hi! I just gave this tutorial a whirl: https://www.adventurecreator.org/tutorials/saving-custom-scene-data

It isn't throwing any errors, but it's also not doing what I expected it to.  I have a vines object with a public int vinesCount that I want to be tracked in this scene.  Right now, when the character uses a knife on the vines, it changes the vinesCount. That works fine. When I leave the scene and come back, I want it to load the same vinesCount # that it left with.  Not sure if I misunderstood the tutorial, but is there a step I'm missing? Should I just save it as a global int and move on? (It isn't applicable in any other scene.. but it keeps reseting on load).  Suggestions?  The remember script I wrote using the tutorial is here: https://pastebin.com/GUvVMhD1  The vines object has RememberVines and VinesInteraction scripts attached (VinesInteraction is what alters the vinesCount).

Thanks!

Comments

  • During loading, you seem to be trying to assign the saved data back to a saved data component... so it's not going to do anything. What you want in the last line of your LoadData() method is:

    GetComponent <vineInteractions>().vinesCount = data.vinesCount;

    But if your script doesn't automatically update your animator or whatever you are using to display the vines when you feed it new numbers, then you may want to call a public method (from the same vineInteractions), right at the end of the LoadData() method. Just think of the LoadData() method as what happens when AC loads. You aren't really limited to simply feeding data, you could easily call methods from other components if you think it's fitting.
  • edited September 2017
    Ah thanks for that tip on the last line of LoadData, fixed that but still having trouble... butI think my issue is that I'm trying to use this for the wrong use case.

    When I save/load a file, it remembers the vineCount just fine.  When I leave the scene and then come back, vineCount gets reset to 0.  There's no start function resetting it.  What's the best practice to remember it across scenes?  Adding it to AC's GameState somehow, or making my own GameState file with a DontDestroyOnLoad?  Or a third solution? (Tried saving it as a local var but got a weird bug where local vars vanish on scene re-enter, shown in this gif: http://gph.is/2eI0lJh )

    thanks again! :)
  • You are getting a NullReference about setting an int local variable. First make sure you fix that issue before you try anything else (the local variable seems to disappear from the list. That's not normal behaviour, it's probably been caused by the error). Make sure you aren't calling AC methods from Awake() or OnEnable() since AC will likely not be ready at that point (if you absolutely have to, try using it in the Start() method).

    If fixing that issues still doesn't get you the results you want you could use an AC global variable, then write to it during SaveData and read from it during LoadData.
  • If all you're looking to do is track the "vinesCount" integer, then
    relying on a local variable should indeed be enough - no need for a
    custom save script.  Know, however, that a custom save script shouldn't
    need any special "resetting" etc functions - it should save/load data
    automatically when moving between scenes.  Certainly nothing like manipulating GameStates should be necessary.

    Are you manipulating the local variable through code, or is it disappearing naturally?  Try it in the latest release (even if it's in a backup/duplicate project).
  • True, you should probably use remember scripts only if you have multiple objects that need the same data saved. If you only have one Vines object in one scene, then a remember script, more likely than not, is overkill...
  • edited September 2017
    I don't have any scripts that touch vinesCount as a LocalVariable, nothing resets it, etc.  I tried updating Adventure Creator in a backup project and it broke so much more stuff I couldn't test to see if it solved the original problem.

    Added a second local var (another int) to see if it would vanish. Now it, and the original vinesCount don't vanish! Buuut vinesCount resets back to 0 every time I re-enter the scene (nothing in my script causes that.. here's my not-so-pretty code, minus some dialog coroutines: https://pastebin.com/GjdgiM13 )

    Leaning towards making everything messy by storing it all as global vars :( I know there's better solutions out there, but I'm not sure what I can do that wouldn't take hours of refactoring to make the update compatible.
  • Which version are you using?  I can look to see if there was an issue with local variables that was in that release.

    You must always read the "Upgrade notes" section in the Changelog for each update, as they contain information about changes that may affect your project.

    If you're having issues after updating, by all means post them in another thread and we can look into them.
  • I'm on 1.56b, I'll take a look at the change log tonight thanks for the advice and help!
  • edited September 2017
    Ok, I've updated to 1.59a and I'm still experiencing the same issue.  I make a local var, onStart I take local var and add 1 to it:
    AC.LocalVariables.SetIntegerValue(0,(AC.LocalVariables.GetIntegerValue(0) + 1));  

    I leave the scene for another and come back, the var never goes above 1.

    I'm running into a similar issue with Remember scripts, not sure if it's relevant but they share similar symptoms... I add "remember convo" to a conversation (it's a scripted conversation, so that might be the reason?) and each time I leave/re-enter the scene it acts like the options added previously were not added.  (If that's a separate issue I can make a new post for it).
  • The Local Variable issue is because both your code, and the AC code that restores the variable values, occur in OnStart.

    You can fix this by giving your script a positive Execution Order value, which will cause it to run after AC's Start function.

    Re: Conversation issue - yes, that'll be a separate issue.  Please post a new thread for that.
  • Oo good to know about Execution Orders, thank you.  I put the var change in Start to test it, but it usually isn't in there. I tried changing the execution order and I'm still running into an issue but this time I got an error message:

    NullReferenceException: Object reference not set to an instance of an object
    AC.LocalVariables.GetIntegerValue (Int32 _id) (at Assets/AdventureCreator/Scripts/Variables/LocalVariables.cs:106)
    alexisConvo.Start () (at Assets/Resources/Scripts/Dialogs/alexisConvo.cs:38)

    (the line it calls to is the one I pasted earlier, I've seen this error message before but not regularly).  When I leave/re-enter the scene the var completely vanishes now (exectution order for the script is set to 100)
  • The error suggests that the variable doesn't exist - are you using the correct ID number?

    When you say the var completely vanishes, do you mean from the Variables Manager?  If so, that would account for the error you're seeing.

    Are you switching to a non-AC scene, and how are you switching to and from it?  Running the code works fine for me, so it sounds like there's some key detail missing that hasn't been accounted for.  Which platform are you working on, and what version of Unity, specifically?
  • Hm I think the other scene is an AC scene, it uses the game manager and all that, plus the global variables carry over.  It does vanish from the variables manager.. I'm a couple unity versions before (I think I'm on 5.2.2). I'll double check when I'm off work, thank you!
  • edited September 2017
    Yup, vanishes from the Variables Manager.  I'm using Unity 5.5.2 and Mac OS.  Both scenes have the AC game manager in it with the same managing asset file, but yeah it really looks like when I switch scenes it loses the consistency between?  I switch scenes using:

    UnityEngine.SceneManagement.SceneManager.LoadScene("BosHouse");
  • edited September 2017
    Wacky, so I changed how scenes load. I removed the line of code above in favor of an action script to load the next scene. That solved part of my remember dialog post, but I still have issues with the local variables getting erased.  But it gets ~weirder~ (aka probably my code somewhere..)

    - There are two scenes. One remembers local vars just fine (Scene A), the other one doesn't (Scene B).
    - If I start play mode in Scene A, then go to Scene B, and back and forth a few times, the error doesn't appear and the var loads fine.
    - If I start play mode in Scene B, I don't get the error until I leave for Scene A and re-enter.
    - Scene B throws me this error and refuses to load but the backtrace isn't going far enough to show me where in my code I'm goofing up... right now I have a local var and 0 lines of personal code touching the var (the scene loads fine the first time but on re-enter it throws this):

    NullReferenceException: Object reference not set to an instance of an object
    AC.LevelStorage.UnloadVariablesData (System.String data, AC.LocalVariables localVariables) (at Assets/AdventureCreator/Scripts/Save system/LevelStorage.cs:499)
    AC.LevelStorage.SendDataToScene (AC.SingleLevelData levelData, Boolean restoringSaveFile, AC.SubScene subScene) (at Assets/AdventureCreator/Scripts/Save system/LevelStorage.cs:154)
    AC.LevelStorage.ReturnCurrentLevelData (Boolean restoringSaveFile) (at Assets/AdventureCreator/Scripts/Save system/LevelStorage.cs:77)
    AC.SceneSettings.OnStart () (at Assets/AdventureCreator/Scripts/Game engine/SceneSettings.cs:107)
    AC.MultiSceneChecker.Start () (at Assets/AdventureCreator/Scripts/Game engine/MultiSceneChecker.cs:72)
  • OMG SOLVED I had two GameEngines in one scene

    XD

    Thanks so much for helping me walk through this mess :) 
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.