How to use custom states?

Posted: 15 Jul 2022 22:21
by Vitriolic_Crux

I'm having trouble understanding how to use the custom states feature. I've created a new blank project to test this, and I'm pretty much ready to start bashing my head into my desk because I'm doing exactly what the guide says to do and it's not working. All I have is an object with the example custom state code attached, from https://naninovel.com/guide/state-manag ... stom-state.

  1. I start the game.

  2. I hit "New Game".

  3. I change myCustomString to "apple" with the inspector.

  4. I save the game.

  5. Then I quit back to the main menu.

  6. I change myCustomString to "banana" with the inspector.

  7. Then I load the save.

  8. myCustomString is still "banana".

How do I get the SerializeState and DeserializeState methods to work? Should I be calling them somewhere? Am I missing some crucial step? Even after looking at the inventory system example, I'm completely at a loss about what to do next.


Re: How to use custom states?

Posted: 16 Jul 2022 08:29
by idaot

I suggest checking out the Inventory example project, specifically InventoryUI.cs, which provides a simple template for serializing a custom state.

https://naninovel.com/guide/inventory.html#inventory


Re: How to use custom states?

Posted: 16 Jul 2022 18:55
by Vitriolic_Crux

Thanks for the suggestion, but as I already stated, I've reviewed the Inventory system example and it didn't make things any clearer to me. I can't find anything explaining how the SerializeState() and DeserializeState() methods are supposed to be invoked.


Re: How to use custom states?

Posted: 16 Jul 2022 20:10
by Elringus

The methods are invoked by the engine when the game is saved/loaded. You don't have to invoke them manually.


Re: How to use custom states?

Posted: 17 Jul 2022 07:48
by idaot
Vitriolic_Crux wrote: 16 Jul 2022 18:55

Thanks for the suggestion, but as I already stated, I've reviewed the Inventory system example and it didn't make things any clearer to me. I can't find anything explaining how the SerializeState() and DeserializeState() methods are supposed to be invoked.

Sorry, I missed that part of the question. But yeah as stated by Elringus, these are invoked automatically via the tasks added during OnEnable() and OnDisable() in the custom state example.


Re: How to use custom states?

Posted: 18 Jul 2022 01:14
by Vitriolic_Crux

Thank you both for your help, but I'm afraid I'm not any closer to getting this working. I've created a completely new project in the April 2019 Unity build that's mentioned as being the most stable, and I've redownloaded the Naninovel package from the asset store. All I have in the project is a single GameObject tied to the example MyCustomBehavior script found here, copy/pasted https://naninovel.com/guide/state-manag ... obal-state and then modified to include a setter for myCustomString. I've also created a custom command whose only purpose is to set the value of myCustomString.

But when I launch, I get this error:

Code: Select all

NullReferenceException: Object reference not set to an instance of an object
MyCustomBehaviour.OnEnable () (at Assets/MyCustomBehaviour.cs:29)

Line 29 is the first line in private void OnEnable().

Code: Select all

private void OnEnable()
{
    stateManager.AddOnGameSerializeTask(SerializeState); // this is the line where the NullReferenceException occurs
    stateManager.AddOnGameDeserializeTask(DeserializeState);
}

And, needless to say, the changes that I make to myCustomString are not being saved or restored.


Re: How to use custom states?

Posted: 18 Jul 2022 01:18
by Elringus

That explains the issue: stateManager is not assigned at the time you're attempting to access it. Make sure to assign the variable before using it. Also, check this: https://naninovel.com/guide/integration ... engine-api


Re: How to use custom states?

Posted: 18 Jul 2022 13:53
by Vitriolic_Crux

That did it! Thank you both so much for your help.