Unity Script Calling Function From Conversation (Lua)

Announcements, support questions, and discussion for the Dialogue System.
c166
Posts: 38
Joined: Fri Oct 14, 2022 11:08 pm

Re: Unity Script Calling Function From Conversation (Lua)

Post by c166 »

Is there a way to parse/ concatenate the result of another function into the script component of the dialogue entry?
addpartycall_dialogue.PNG
addpartycall_dialogue.PNG (3.26 KiB) Viewed 918 times
From my understanding the name of the Lua function would be `AddPartyMember+name` (when using the 2nd method of registering a different Lua function per party member), so I'd need to call that specifically in the script/ conversation section since the generic function of `AddPartyMember` does not exist as a Lua function (and is the function called under the hood).
addpartyfunction_dialogue.PNG
addpartyfunction_dialogue.PNG (6.63 KiB) Viewed 918 times
I assume it's something close to the below as from what I've found `..` is how you concat strings together in Lua.

Code: Select all

"AddPartyMember" .. Actor["NPC"].Name .. "()"
While it doesn't cause an error (e.g. not registered as a Lua function error), it doesn't actually run as a function + the log message for it seems to NOT parse the `Actor[NPC]` variable.
parsed_lua_function.PNG
parsed_lua_function.PNG (13.62 KiB) Viewed 918 times
I've tried the variations of the following as well, which SEEMS to be how you call functions from strings in Lua but got an error. This error occurs before the function is actually called (I added a debug `Debug.Log("Adding Party Member");` to the very start of AddPartyMember (underyling Unity function)

Code: Select all

loadstring("return" .. "AddPartyMember" .. Actor["NPC"].Name .. "()");

Code: Select all

loadstring("return ".."AddPartyMember"..Actor["NPC"].Name.."()")();
lua_concat_function_error.PNG
lua_concat_function_error.PNG (108.58 KiB) Viewed 918 times
Not sure what I'm doing wrong here / whether I'm close or way off :/
Attachments
lua_function_error.PNG
lua_function_error.PNG (8 KiB) Viewed 918 times
User avatar
Tony Li
Posts: 22142
Joined: Thu Jul 18, 2013 1:27 pm

Re: Unity Script Calling Function From Conversation (Lua)

Post by Tony Li »

Hi,

I'm going to assume the NPC is the conversation conversant. So instead of Actor["NPC"].Name, I'll use Variable["ConversantIndex"], which is the sanitized version of the NPC's actor name. It's sanitized to make it valid as a variable name, so spaces and other punctuation are replaced by underscores.

The loadstring() function requires a second parameter. Just use "_G" for the second parameter.
Explanation: _G
The second parameter is the environment that the function should run in. It's kind of like the execution stack. "_G" refers to the global environment.
Try something like this:

Code: Select all

func = loadstring("AddPartyMember" .. Variable["ConversantIndex"] .. "()", _G)
func()
The first line defines func() as a function that runs the code inside the string.

The second line actually calls the func() function.
c166
Posts: 38
Joined: Fri Oct 14, 2022 11:08 pm

Re: Unity Script Calling Function From Conversation (Lua)

Post by c166 »

I've updated the code to the following as suggested (tried both with and without `;` as that seems to be optional/ explicit new line of code) and unfortunately it doesn't seem to work when I try to call the function. I also tried replacing `loadstring` with `load` to the same result (as apparently `loadstring` is deprecated in later versions of Lua)
lua_script_update.PNG
lua_script_update.PNG (5.99 KiB) Viewed 913 times
It seems to establish the creation of the function via loadstring
lua_func_establish.PNG
lua_func_establish.PNG (81.66 KiB) Viewed 913 times
However, when I try to call it it says that I'm invoking a non-function value/ is it registered with Lua
lua_func_error.PNG
lua_func_error.PNG (84.98 KiB) Viewed 913 times
That seems a bit weird to me given that the script box there seems to be a Lua interface/ everything there should be registered as Lua by default?

I've named the GameObject `FemaleSamurai`, so I think that rules out a mismatch of name due to sanitization.
lua_register_name.PNG
lua_register_name.PNG (6.4 KiB) Viewed 913 times
c166
Posts: 38
Joined: Fri Oct 14, 2022 11:08 pm

Re: Unity Script Calling Function From Conversation (Lua)

Post by c166 »

Okay scratch the ruling out of function mismatch, I tried printing the string and it seems to be calling `AddPartyMemberNPC` and not referring to the gameobject name. I then printed both `Actor["NPC"].Name` and `Variable["ConversantIndex"]` by themselves and they both just return NPC.
lua_print_check.PNG
lua_print_check.PNG (6.6 KiB) Viewed 912 times
lua_print_name.PNG
lua_print_name.PNG (13.67 KiB) Viewed 912 times
The conversant GameObject is FemaleSamurai so it seems that the variables are returning the generic variable name that splits actions between NPCs and Player?
conversant_transforms.PNG
conversant_transforms.PNG (13.01 KiB) Viewed 912 times
Attachments
lua_print_func.PNG
lua_print_func.PNG (8.19 KiB) Viewed 912 times
User avatar
Tony Li
Posts: 22142
Joined: Thu Jul 18, 2013 1:27 pm

Re: Unity Script Calling Function From Conversation (Lua)

Post by Tony Li »

Create an actor named "FemaleSamurai" in your database. Add a Dialogue Actor component to your FemaleSamurai GameObject, and set the Actor dropdown to FemaleSamurai. Your conversation can still be assigned to Player + NPC. Since you've assigned the FemaleSamurai GameObject to the Dialogue System Trigger's Conversation Conversant field, the conversation will use the FemaleSamurai actor wherever the conversation originally references the NPC actor.

If you really don't want to add an actor named "FemaleSamurai" to your database, you don't have to. Instead, click the circle next to the Dialogue Actor component's Actor dropdown and manually enter "FemaleSamurai".
c166
Posts: 38
Joined: Fri Oct 14, 2022 11:08 pm

Re: Unity Script Calling Function From Conversation (Lua)

Post by c166 »

Thanks! Works perfectly now - hadn't realized I'd missed setting new Actors up in the database/ that it'd link to the Variable["ConversantIndex"] like that. Some really nice functionality here, and appreciate your guidance through that convoluted set of questions

Merry Christmas/ Happy holidays and hope you have a great new year! :)
User avatar
Tony Li
Posts: 22142
Joined: Thu Jul 18, 2013 1:27 pm

Re: Unity Script Calling Function From Conversation (Lua)

Post by Tony Li »

Merry Christmas! Glad to help!
Post Reply