Trigger a reset number variable to zero

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
62qBruv
Posts: 22
Joined: Sun Jul 12, 2020 5:38 pm

Trigger a reset number variable to zero

Post by 62qBruv »

I am using Invector integration. One of my quests uses a number variable to record the number of enemies killed. The enemies have an "Increment On Destroy" component attached. However, if my Player dies in combat the level is reset and the Player re-starts the level. This means that any enemies not destroyed are destroyed when the level resets and my variable continues to increment thereby messing up the "enemies killed" quest when it is assigned for the second time because the variable says the enemies are already dead.

But - the game restarts in an area where I can place a trigger across the path and I want to trigger lua code to *re-set* the variable to zero. After passing through that trigger the player can then go through a second trigger to deactivate the first trigger. This would fix the issue I hope. If it does, what sort of trigger can I use to call lua code to reset the variable to zero and what lua code would I use to do that please?
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Trigger a reset number variable to zero

Post by Tony Li »

Hi,

Two questions:

1. Are you using Invector to reset the level (e.g., it calls SceneManager.LoadScene) or the Dialogue System?

2. If you're using the Dialogue System, are you using its save system?

If you're not using the Dialogue System's save system, call PersistentData.Manager.LevelWillBeUnloaded(). This tells Increment On Destroy components that they're about to be destroyed by a scene unload, so they shouldn't increment.

If you are using the Dialogue System's save system, reset the level by using PixelCrushers.SaveSystem.LoadScene(). Internally, this calls SaveSystem.BeforeSceneChange(), which tells the DialogueSystemSaver component to call PersistentDataManager.LevelWillBeUnloaded().

That's one way to handle it.

Alternatively, if you want to reset the variable to zero, add a Dialogue System Trigger component to your trigger collider GameObject. Set it to OnTriggerEnter. Select Add Action > Run Lua Code. Configure the Lua Code to reset the variable to zero.
62qBruv
Posts: 22
Joined: Sun Jul 12, 2020 5:38 pm

Re: Trigger a reset number variable to zero

Post by 62qBruv »

Thank you. I used the last method. I put the Dialogue trigger on a box collider set to trigger and used the "run Lua code" option. To be sure, I set all quests to unassigned and then set the kill variable to zero using commands I found on this forum.

To be honest, I would rather use the Dialogue Save System you describe but I need to watch some more tutorials and study the demo scenes a little more before I'm ready for that.

I'll keep it simple for now. Thank you for your help.
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Trigger a reset number variable to zero

Post by Tony Li »

Happy to help!
Post Reply