Jumping directly to DialogueEntry via ID #, while mid-conversation?

Announcements, support questions, and discussion for the Dialogue System.
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: Jumping directly to DialogueEntry via ID #, while mid-conversation?

Post by Tony Li »

Hi,

"Variable" is a Lua table (like a Dictionary<> in C#). It doesn't have a callback when its elements are changed. Instead, you can write a C# method and register it with Lua. For example:

Code: Select all

void Awake() 
{
    Lua.RegisterFunction(nameof(IncrementVariable), this, SymbolExtensions.GetMethodInfo(() => IncrementVariable("", 0)));
}

void IncrementVariable(string variableName, double incrementAmount)
{
    float newValue = DialogueLua.GetVariable(variableName).asFloat + (float)incrementAmount;
    if (isServer)
    {
        DialogueLua.SetVariable(variableName, newValue);
    }
    else
    {
        RpcSetVariableOnClient(variableName, newValue);
    }
}
I don't recall what network code your using. Assume RpcSetVariableOnClient is an RPC method you've written that runs DialogueLua.SetVariable() on the client(s).
Jamez0r
Posts: 59
Joined: Fri Nov 20, 2020 8:18 pm

Re: Jumping directly to DialogueEntry via ID #, while mid-conversation?

Post by Jamez0r »

Thanks for the quick reply!
Tony Li wrote: Thu Oct 06, 2022 2:39 pm "Variable" is a Lua table (like a Dictionary<> in C#). It doesn't have a callback when its elements are changed.
Sorry, my explanation might have been ambiguous with what I am looking to do - I was hoping to inject some code in wherever this sort of command would be processed (for any time the built-in 'Set Variable' Script command is used)

Image

Is that possible?
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: Jumping directly to DialogueEntry via ID #, while mid-conversation?

Post by Tony Li »

The catch is that's not a command per se. When you click Apply, it will generate this Lua code:

Code: Select all

Variable["Examples.MeanGuy.NumTimesSpoken"] = Variable["Examples.MeanGuy.NumTimesSpoken"] + 0
Instead, you can write your own C# method (like my IncrementVariable() example) that does the same thing except it can also make an RPC call to tell other clients to update their Variable[] table value, too:

customIncrementVariable.png
customIncrementVariable.png (12.64 KiB) Viewed 772 times

Or, if you're using UNET or Mirror, you could use the LuaNetworkCommands component, and do this:

Code: Select all

NetSetNumber("Examples.MeanGuy.NumTimesSpoken", Variable["Examples.MeanGuy.NumTimesSpoken"] + 0)
Jamez0r
Posts: 59
Joined: Fri Nov 20, 2020 8:18 pm

Re: Jumping directly to DialogueEntry via ID #, while mid-conversation?

Post by Jamez0r »

Tony Li wrote: Thu Oct 06, 2022 3:25 pm The catch is that's not a command per se. When you click Apply, it will generate this Lua code:

Code: Select all

Variable["Examples.MeanGuy.NumTimesSpoken"] = Variable["Examples.MeanGuy.NumTimesSpoken"] + 0
Instead, you can write your own C# method (like my IncrementVariable() example) that does the same thing except it can also make an RPC call to tell other clients to update their Variable[] table value, too:


customIncrementVariable.png


Or, if you're using UNET or Mirror, you could use the LuaNetworkCommands component, and do this:

Code: Select all

NetSetNumber("Examples.MeanGuy.NumTimesSpoken", Variable["Examples.MeanGuy.NumTimesSpoken"] + 0)
Ohhh, now I see what you're saying with the Dictionary. My bad, thanks for clearing that up!

I'll make a set of custom Lua Commands and use them - thanks for the examples too!
Jamez0r
Posts: 59
Joined: Fri Nov 20, 2020 8:18 pm

Re: Jumping directly to DialogueEntry via ID #, while mid-conversation?

Post by Jamez0r »

Tony Li wrote: Thu Oct 06, 2022 3:25 pm The catch is that's not a command per se. When you click Apply, it will generate this Lua code:

Code: Select all

Variable["Examples.MeanGuy.NumTimesSpoken"] = Variable["Examples.MeanGuy.NumTimesSpoken"] + 0
Instead, you can write your own C# method (like my IncrementVariable() example) that does the same thing except it can also make an RPC call to tell other clients to update their Variable[] table value, too:


customIncrementVariable.png


Or, if you're using UNET or Mirror, you could use the LuaNetworkCommands component, and do this:

Code: Select all

NetSetNumber("Examples.MeanGuy.NumTimesSpoken", Variable["Examples.MeanGuy.NumTimesSpoken"] + 0)
Alright I thought my previous question was going to be the final one, but I have one more quickie :)

Lets say I was using this function (from LuaNetworkCommands.cs)

Code: Select all

Lua.RegisterFunction("NetSetBool", this, SymbolExtensions.GetMethodInfo(() => NetSetBool(string.Empty, false)));

...

 public void NetSetBool(string variableName, bool value)
        {
            CmdSetBool(variableName, value);
        }
When I am using the command from within a dialogueEntry, I have to manually enter the string for "variableName". We have a ton of variables and they have pretty long names (like NPC.Blacksmith.PerformedUpgrade1, etc).

I'd like to be able to select the name of the variable from a list, like in this screenshot:
Image

Is there any built-in way to have that "selection menu" popup, so I could select the variable that I want to be applied to the "string variableName" field? I assume this would be related to parameter settings on the CustomLuaFunctionInfo scriptableObject.

If there isn't a built-in way to do it, I was thinking I could maybe do some hacking and add another enum value to the "CustomLuaParameterType", and look at the code behind the CustomLuaParameterType.Variable (which has the variable-selection-popup-menu as part of it). I don't mind looking through the code to figure out a way to hack that together, but if for some reason it wouldn't be possible I'd appreciate it if you'd let me know, LOL.

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

Re: Jumping directly to DialogueEntry via ID #, while mid-conversation?

Post by Tony Li »

You should be able to set your CustomLuaFunctionInfo to use the Variable type as-is:
customIncrementVariableType.png
customIncrementVariableType.png (26.98 KiB) Viewed 769 times

This will make it a variable dropdown where you can select a variable from your dialogue database or even add a new variable right there.
Jamez0r
Posts: 59
Joined: Fri Nov 20, 2020 8:18 pm

Re: Jumping directly to DialogueEntry via ID #, while mid-conversation?

Post by Jamez0r »

Tony Li wrote: Thu Oct 06, 2022 8:02 pm You should be able to set your CustomLuaFunctionInfo to use the Variable type as-is:

customIncrementVariableType.png


This will make it a variable dropdown where you can select a variable from your dialogue database or even add a new variable right there.
Thanks for the quick response!

If I use the provided Variable parameter type it results in adding this to the line of Lua code:

Code: Select all

Variable["myVariable"]
Like this:
Image

What I'm looking to do is have a selection-menu just like how the Variable parameter shows, but for it to result in just:

Code: Select all

"myVariable"
so that the result can be used as the string parameter in my custom function, like this:
Image

I'm wondering if I can add an extra entry to the CustomLuaParameterType (like "VariableNameOnly"), and then simply copy/paste everything that CustomLuaParameterType.Variable does, and just change this section of code:

Image

And just modify the part that has "Variable[/""... etc so that it results in just the variable's name itself. Might work? :D
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: Jumping directly to DialogueEntry via ID #, while mid-conversation?

Post by Tony Li »

Ah, you're right. I didn't think that through. I'll add a Variable Name option to the dropdown in version 2.2.33.
Jamez0r
Posts: 59
Joined: Fri Nov 20, 2020 8:18 pm

Re: Jumping directly to DialogueEntry via ID #, while mid-conversation?

Post by Jamez0r »

Tony Li wrote: Thu Oct 06, 2022 8:25 pm Ah, you're right. I didn't think that through. I'll add a Variable Name option to the dropdown in version 2.2.33.
Awesome, thanks Tony!

Also, just tested it out and I was indeed able to create my own enum value and splice things together to get it to work as desired (inserting the variable's name only into the Lua code) 8-)

Hopefully I'll be out of your hair now, haha. Thanks for your patience with all of my questions!
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: Jumping directly to DialogueEntry via ID #, while mid-conversation?

Post by Tony Li »

Glad to help!
Post Reply