[SOLVED] "Lua Conditions" only in inspector

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Jamez0r
Posts: 59
Joined: Fri Nov 20, 2020 8:18 pm

[SOLVED] "Lua Conditions" only in inspector

Post by Jamez0r »

Hey Tony! Hope you're doing well.

I want to add a set of Lua Conditions to a script that I can configure using the Wizard, and run a simple 'are conditions true' check via code.

Is there anything out of the box to have only the Lua Conditions (with the wizard), and not the Quest Conditions / Accepted Tags / Accepted Game Objects?
lua condition.png
lua condition.png (29.31 KiB) Viewed 801 times
If there isn't anything out of the box for that - how would you suggest I go about setting that up? Maybe I could make my own custom version of Condition.cs and ConditionEditor.cs and delete everything except the Lua Condition related code? Would it be as easy as that?

Thanks for any help!
Last edited by Jamez0r on Thu Sep 21, 2023 2:22 pm, edited 1 time in total.
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: "Lua Conditions" only in inspector

Post by Tony Li »

Hi,

Use the [LuaConditionWizard] attribute. Example:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class TestLuaWizard : MonoBehaviour
{
    [LuaConditionsWizard(true)] public string luaConditions;
    
    public bool IsTrue()
    {
        return Lua.IsTrue(luaConditions);
    }
}
Jamez0r
Posts: 59
Joined: Fri Nov 20, 2020 8:18 pm

Re: "Lua Conditions" only in inspector

Post by Jamez0r »

Tony Li wrote: Wed Sep 20, 2023 9:06 pm Hi,

Use the [LuaConditionWizard] attribute. Example:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class TestLuaWizard : MonoBehaviour
{
    [LuaConditionsWizard(true)] public string luaConditions;
    
    public bool IsTrue()
    {
        return Lua.IsTrue(luaConditions);
    }
}
Thanks for the help Tony!

That did indeed work (awesome! :D ), but immediately brought up another question. I am actually looking to use these LuaConditions as part of a ScriptableObject. I noticed that if I have the scene open that has the "Dialogue Manager" prefab instance in it, then the Reference Database is automatically set to my database (my game only needs a single database).

However, if the scene I currently have open doesn't have the "Dialogue Manager" in it, then when I recompile my code, the scriptable object's Reference Database loses the reference. My guess is that behind the scenes the "Dialogue Manager" is automatically setting the "master database" after code recompilation, maybe?

How could I set things up so that my scriptable object maintains the Reference Database link? Ideally I wouldn't ever have to drag-and-drop my database into the reference for these scriptable objects, since I only have one database in my project. I don't mind doing a bit of custom code / tweaking if it's required.

Thanks a ton!
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: "Lua Conditions" only in inspector

Post by Tony Li »

Hi,

The [LuaConditionsWizard] attribute doesn't store a reference to the database, since this is just extra data that you don't need at runtime.

However, you could add that. Add a field to your ScriptableObject such as:

Code: Select all

public class MySO : ScriptableObject
{
    public DialogueDatabase referenceDatabase;
    
    [LuaConditionsWizard] public string luaConditions;
}
Then write a custom editor script for your ScriptableObject, something like:

Code: Select all

[CustomEditor(typeof(MySO))]
public class MySOEditor ; Editor
{
    public override OnInspectorGUI()
    {
        EditorTools.selectedDatabase = (target as MySO).referenceDatabase;
        EditorTools.SetInitialDatabaseIfNull();
        base.OnInspectorGUI();
    }
}
Jamez0r
Posts: 59
Joined: Fri Nov 20, 2020 8:18 pm

Re: "Lua Conditions" only in inspector

Post by Jamez0r »

Tony Li wrote: Thu Sep 21, 2023 9:30 am Hi,

The [LuaConditionsWizard] attribute doesn't store a reference to the database, since this is just extra data that you don't need at runtime.

However, you could add that. Add a field to your ScriptableObject such as:

Code: Select all

public class MySO : ScriptableObject
{
    public DialogueDatabase referenceDatabase;
    
    [LuaConditionsWizard] public string luaConditions;
}
Then write a custom editor script for your ScriptableObject, something like:

Code: Select all

[CustomEditor(typeof(MySO))]
public class MySOEditor ; Editor
{
    public override OnInspectorGUI()
    {
        EditorTools.selectedDatabase = (target as MySO).referenceDatabase;
        EditorTools.SetInitialDatabaseIfNull();
        base.OnInspectorGUI();
    }
}
Hey Tony, thanks for the response!

It seems like FindInitialDatabase() (called by SetInitialDatabaseIfNull) is expecting the currently open scene to have a DialogueSystemController. Since I'm working with a ScriptableObject here, I won't usually have a scene open that has one.
database.png
database.png (8.19 KiB) Viewed 755 times
Should I make my own custom function similar to FindInitialDatabase(), that just points directly to my database in the Assets folder, and assigns it to EditorTools.selectedDatabase?

Thanks a lot for your help!
Jamez0r
Posts: 59
Joined: Fri Nov 20, 2020 8:18 pm

Re: "Lua Conditions" only in inspector

Post by Jamez0r »

Update: Believe I got this working. Sorry if my question/info wasn't very clear!

In my CustomEditor for my Scriptable Object I have:

Code: Select all

 public override void OnInspectorGUI() {
        if (EditorTools.selectedDatabase == null) {
            EditorTools.selectedDatabase = DialogueInterface.Editor_GetDialogueDatabaseAsset();
        }
        base.OnInspectorGUI();
    }
and then I have this static function in DialogueInterface.cs:

Code: Select all

#if UNITY_EDITOR
    public static DialogueDatabase Editor_GetDialogueDatabaseAsset() {
        return AssetDatabase.LoadAssetAtPath("Assets/Dialogue Database/Dialogue Database.asset", typeof(DialogueDatabase)) as DialogueDatabase;
    }
#endif
Seems to be working great!
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: [SOLVED] "Lua Conditions" only in inspector

Post by Tony Li »

Looks good!
Jamez0r
Posts: 59
Joined: Fri Nov 20, 2020 8:18 pm

Re: [SOLVED] "Lua Conditions" only in inspector

Post by Jamez0r »

I quickly realized I needed a 'global' way of setting the database reference (things like [VariablePopup] on scripts, etc, wouldn't work since the reference would get lost every time I recompile).

I created an EditorWindow that handles it - it sets the reference OnEnable() and also "afterAssemblyReload". So as long as you have this EditorWindow open, it should maintain the reference.

Code: Select all

using PixelCrushers.DialogueSystem;
using UnityEditor;
using UnityEngine;

public class GardenfiendGamesProjectToolsWindow : EditorWindow
{


    [MenuItem("Window/Gardenfiend Games/ProjectTools")]
    public static void ShowWindow() {
        EditorWindow.GetWindow<GardenfiendGamesProjectToolsWindow>("ProjectTools");
    }

    void OnEnable() {
        Debug.Log("PROJECTTOOLS: OnEnable called (assembly reload callback added)");
        EnsureDialogueDatabaseStaticReference();
        AssemblyReloadEvents.afterAssemblyReload -= OnAfterAssemblyReload;
        AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
    }

    void OnDisable() {
        Debug.Log("PROJECTTOOLS: OnDisabled called");
        AssemblyReloadEvents.afterAssemblyReload -= OnAfterAssemblyReload;
    }


    public void OnAfterAssemblyReload() {
        Debug.Log("PROJECTTOOLS: OnAfterAssemblyReload"); 
        EnsureDialogueDatabaseStaticReference();
    }

    public static void EnsureDialogueDatabaseStaticReference() {
        if (EditorTools.selectedDatabase == null) {
            EditorTools.selectedDatabase = DialogueInterface.Editor_GetDialogueDatabaseAsset();
        }
    }

}
Note: See my previous response for what DialogueInterface.Editor_GetDialogueDatabaseAsset() is.

Hope it helps anyone that needs it! :D
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: [SOLVED] "Lua Conditions" only in inspector

Post by Tony Li »

Thanks for sharing!
Post Reply