Set bool variables true or false from external script using Lua?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Automatic_Eye
Posts: 2
Joined: Tue Mar 29, 2022 5:31 pm

Set bool variables true or false from external script using Lua?

Post by Automatic_Eye »

Hi, and thanks so much for this amazing Plugin!

I've read about some similar scenarios in the forums but nothing that quite matches what I'm trying to do, so I'm hoping you can help me (I'm also pretty new to coding so I may be overlooking something very simple here):

In my game, the player can control any one of three player characters at will. Which player character is currently being controlled is set by a custom PlayerSwitcher script. My goal is to add something to that script to set variables within the dialogue system, so that NPC dialogue can branch in different directions depending on which character the player is controlling.

At first I set a boolean for each player in the Dialogue System variables tab (isShael is the default and set to 'true'):
Image

Example: If you're controlling "Nyla," the NPC uses one branch, if you're "Shael" they use the other branch depending on the bool being true or false. (All of this so far seems straightforward).
Image

In my PlayerSwitcher script, I added using PixelCrushers.DialogueSystem, and started by trying SetVariable as follows for each character, referring directly to the name of the variable:

Code: Select all

DialogueLua.SetVariable("isNyla", true);
That didn't work, so I watched the tutorial video on connecting C# methods to Lua, and used the script template to make another script called RegisterLua that's attached to the Dialogue Manager. I'm not sure exactly how to handle a boolean in this case- the video showed a string that returned a bool value- so perhaps I've got the syntax wrong or am missing an argument:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class RegisterLua : MonoBehaviour
{
    public bool isNyla;
    public bool isShael;
    public bool isDwen;

    public bool PlayerIsNyla()
    {
        return isNyla;
    }

    public bool PlayerIsShael()
    {
        return isShael;
    }

    public bool PlayerIsDwen()
    {
        return isDwen;
    }

    void OnEnable()
    {
        Lua.RegisterFunction("PlayerIsNyla", this, SymbolExtensions.GetMethodInfo(() => PlayerIsNyla()));
        Lua.RegisterFunction("PlayerIsShael", this, SymbolExtensions.GetMethodInfo(() => PlayerIsShael()));
        Lua.RegisterFunction("PlayerIsDwen", this, SymbolExtensions.GetMethodInfo(() => PlayerIsDwen()));
    }

    void OnDisable()
    {
        Lua.UnregisterFunction("PlayerIsNyla");
        Lua.UnregisterFunction("PlayerIsShael");
        Lua.UnregisterFunction("PlayerIsDwen");
    }
}
Since it looks like you have to register "PlayerIsNyla" in this format instead of the actual names of the variables I updated the SetVariable as follows for each character:

Code: Select all

DialogueLua.SetVariable("PlayerIsNyla", true);
The variables still weren't changing during gameplay, and the NPC still gave no dialogue, so next I tried creating custom Lua functions from the asset menu instead of trying to access the ones in the Variables tab:
Image
...and updated the conditions in the dialogue tree to use Custom.

The result was the same, the boolean isn't changing when SetVariable is triggered in the PlayerSwitcher script, and the NPC gives no dialogue. So I'm obviously missing something but I'm not sure where. It's also possible there's an easier/better way to set variables from outside scripts that I'm overlooking.

Any help would be appreciated. Thanks very much!
User avatar
Tony Li
Posts: 21980
Joined: Thu Jul 18, 2013 1:27 pm

Re: Set bool variables true or false from external script using Lua?

Post by Tony Li »

Hi,

Let's step back and go with your first approach: Set Dialogue System variables "isNyla", "isShael", and "isDwen".

Make sure to set all three. Example:

Code: Select all

DialogueLua.SetVariable("isNyla", true);
DialogueLua.SetVariable("isShael", false);
DialogueLua.SetVariable("isDwen", false);
Note also that the Dialogue Editor window's Variables tab shows the Initial Value of each variable, not their current values. To see current values, use the Watches tab or the separate Variable Viewer window.
Automatic_Eye
Posts: 2
Joined: Tue Mar 29, 2022 5:31 pm

Re: Set bool variables true or false from external script using Lua?

Post by Automatic_Eye »

This seems to have worked. :) Thank you so much for your help!
User avatar
Tony Li
Posts: 21980
Joined: Thu Jul 18, 2013 1:27 pm

Re: Set bool variables true or false from external script using Lua?

Post by Tony Li »

Glad to help!
Post Reply