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:
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.
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:
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?
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.
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.