How to pass Variable parameter in Custom Lua Function

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
tomwilhelm
Posts: 13
Joined: Sun Jan 24, 2021 6:18 pm

How to pass Variable parameter in Custom Lua Function

Post by tomwilhelm »

I am trying to take advantage of the Lua Dropdown Wizard and hopefully setup conversation conditions without writing a lot of unique Lua code.
I noticed that Custom Lua Function Info has the option of Variable as a Parameter.
Screenshot (6).png
Screenshot (6).png (90.65 KiB) Viewed 1038 times
How should I construct a Lua Registered Function to accept a Variable as a parameter?
Thanks! :D
User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to pass Variable parameter in Custom Lua Function

Post by Tony Li »

Hi,

If you choose Variable for the parameter type, then it will pass a value from the Variable[] table. The dropdown will show the variables defined in the Variables panel of the Dialogue Editor. Since these variables can be any type, make sure to match the type of the variable with the type used in your C# method.

For example, if your C# ContainsEntry method is:

Code: Select all

public bool ContainsEntry(string entryTitle) {...}
then you should select a Text variable in the dropdown.

If your C# ContainsEntry method is:

Code: Select all

public bool ContainsEntry(double entryID) {...}
then you should select a Number variable in the dropdown like this:
customLuaFuncVariable.png
customLuaFuncVariable.png (24.5 KiB) Viewed 1036 times
tomwilhelm
Posts: 13
Joined: Sun Jan 24, 2021 6:18 pm

Re: How to pass Variable parameter in Custom Lua Function

Post by tomwilhelm »

Hi,
Thank you for responding.
I got it working with Number and Text fields.
Can other field types work?
I tried making a CustomFieldType with a storeFieldAsType = Text but it seems to return null when passed to the condition function.
User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to pass Variable parameter in Custom Lua Function

Post by Tony Li »

Hi,

Custom fields are always added as Text (strings).

For example, say you've defined a custom field type named Relationship with three enum values: { Friend, Neutral, Enemy }

And you've defined a variable in the Dialogue Editor's Variables section named RelationshipToPlayer initially set to Neutral.

Internally, the Lua variable will be set to:

Code: Select all

Variable["RelationshipToPlayer"] = "Neutral"
If you pass this variable to your custom Lua function, it should be registered to a C# method that accepts a string:

Code: Select all

public bool IsFriendly(string relationship)
{
    return relationship == "Friend";
}
tomwilhelm
Posts: 13
Joined: Sun Jan 24, 2021 6:18 pm

Re: How to pass Variable parameter in Custom Lua Function

Post by tomwilhelm »

Great I got it working.
Thanks!
User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to pass Variable parameter in Custom Lua Function

Post by Tony Li »

Glad to help!
Post Reply