Page 1 of 1

How to pass Variable parameter in Custom Lua Function

Posted: Sun Jan 24, 2021 6:48 pm
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 1042 times
How should I construct a Lua Registered Function to accept a Variable as a parameter?
Thanks! :D

Re: How to pass Variable parameter in Custom Lua Function

Posted: Sun Jan 24, 2021 8:05 pm
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 1040 times

Re: How to pass Variable parameter in Custom Lua Function

Posted: Sun Jan 24, 2021 11:28 pm
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.

Re: How to pass Variable parameter in Custom Lua Function

Posted: Mon Jan 25, 2021 9:03 am
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";
}

Re: How to pass Variable parameter in Custom Lua Function

Posted: Mon Jan 25, 2021 2:26 pm
by tomwilhelm
Great I got it working.
Thanks!

Re: How to pass Variable parameter in Custom Lua Function

Posted: Mon Jan 25, 2021 3:08 pm
by Tony Li
Glad to help!