Page 1 of 1
How to change quest state when a scene loads
Posted: Sun Jun 02, 2019 1:45 am
by AoF
This was really hard for me to figure out on my own, so I'm posting this because it may help others. Here's how you can set a quest's entry state from a scene loading:
Code: Select all
using PixelCrushers.DialogueSystem;
using UnityEngine;
public class Field : MonoBehaviour
{
public DialogueSystemTrigger visitedFieldTrigger;
public void OnApplyPersistentData()
{
Debug.LogWarning("Called");
QuestLog.SetQuestEntryState("Plant some crops!", 1, QuestState.Success);
Debug.LogWarning("Call finished");
}
public void OnEnable()
{
PersistentDataManager.RegisterPersistentData(this.gameObject);
}
public void OnDisable()
{
PersistentDataManager.RegisterPersistentData(this.gameObject);
}
}
If you simply put OnApplyPersistentData in a Start(), it would be overwritten because the saved Lua state would get loaded AFTER that. For the same reason, this won't work if you do it in an "On Scene Load" from the save system events script. Putting it in OnApplyPersistentData makes the code run after the Lua state is loaded.
Re: How to change quest state when a scene loads
Posted: Sun Jun 02, 2019 3:16 pm
by AoF
OK, I do have a follow up question to this though. If I hit play, that first scene doesn't trigger the OnApplyPersistentData. I'm having to write a hacky solution (the Invoke) like this:
Code: Select all
private bool executed = false;
void Start()
{
Invoke("ExecuteIfNoPersistentData", 1f); // because when you load a scene, it doesn't load data, only if you move to a new scene
}
public void ExecuteIfNoPersistentData()
{
OnApplyPersistentData();
}
public void OnApplyPersistentData()
{
if (!executed)
{
executed = true;
QuestLog.SetQuestEntryState("Plant some crops!", 1, QuestState.Success);
}
}
public void OnEnable()
{
PersistentDataManager.RegisterPersistentData(this.gameObject);
}
public void OnDisable()
{
PersistentDataManager.RegisterPersistentData(this.gameObject);
}
I just want to execute a piece of code after a scene loads without it being overwritten by save data. How do I do that?
Re: How to change quest state when a scene loads
Posted: Mon Jun 03, 2019 10:28 pm
by Tony Li
Here's how do it without code:
1. Add an empty GameObject to the scene. (Or use an existing GameObject if you prefer.)
2. Add a Dialogue System Trigger.
- Set the trigger dropdown to OnUse.
- Select Add Action > Run Lua Code.
- Set the Lua Code field to:
Code: Select all
SetQuestEntryState("Plant some crops!", 1, "success")
3. Add a Timed Event component to the same GameObject.
- Set Mode to Frames.
- Set Frames to 2 (i.e., one higher than your Dialogue Manager's Save System component's Frames To Wait For Apply Data).
- Tick Activate On Start.
- Configure the OnTimeReached() event to call the Dialogue System Trigger's OnUse() method.
Re: How to change quest state when a scene loads
Posted: Mon Jun 03, 2019 10:30 pm
by AoF
Thank you! That's much better than my solution
Re: How to change quest state when a scene loads
Posted: Mon Jun 03, 2019 10:37 pm
by AoF
While trying this, I noticed there's no way to say, "nevermind, I don't want a Lua action." What i mean is, there's no X box to delete the Lua action after I've created it:
Let me know if there's a preferred method of reporting things like this. I'm just piggybacking off this thread because it's somewhat related.
Re: How to change quest state when a scene loads
Posted: Mon Jun 03, 2019 10:44 pm
by Tony Li
If you leave that inspector and come back to it, the Lua Code action will be gone if the Lua Code field is left blank. But that's a good point. I'll add an 'x' button in the next version to make it explicit.
Re: How to change quest state when a scene loads
Posted: Mon Jun 03, 2019 10:45 pm
by AoF
Cool, thanks!