Page 1 of 2
Unity Script Calling Function From Conversation (Lua)
Posted: Sat Dec 17, 2022 7:23 pm
by c166
Hey am wondering about the best way to call a custom script based on how the conversation goes. I.e. call an "add to party" function if the NPC gets asked to join the party.
From what I could see in the documentation, the best way seems to be to connect/ register your code to Lua so you can call like all the pixel crusher pre-packaged functions.
https://www.pixelcrushers.com/dialogue_ ... d_lua.html
However, the reference that the documentation points to is blank (or does not load properly for me). I've tried Chrome + Edge and it appears as a blank page + does not prompt a download of anything. I have turned off ad blocker as well. All the other template/ reference files also seem to have the same problem.
- blank_lua_reference.PNG (12.46 KiB) Viewed 1766 times
https://www.pixelcrushers.com/dialogue_ ... a_8cs.html
Not sure if it's a local problem on my end, or if I'm looking at deprecated files. Hope you can point me in the right direction + sense-check that the approach above suits my use-case of adding party members via conversation
In the meantime, I plan to manually copy the code shown in the youtube tutorial, but would be good to see the reference since it seems to contain a lot of info.
https://www.youtube.com/watch?v=55I0psn ... elCrushers
Re: Unity Script Calling Function From Conversation (Lua)
Posted: Sat Dec 17, 2022 7:42 pm
by Tony Li
Hi,
The online reference doesn't show the actual source code of that file (since it doesn't differentiate between that file and other actual asset source files), but the file itself is in your project in Assets / Plugins / Pixel Crushers / Dialogue System / Templates / Scripts / TemplateCustomLua.cs. Duplicate this file, rename it and move it into your own scripts folder, and fill in your code where indicated.
Re: Unity Script Calling Function From Conversation (Lua)
Posted: Sat Dec 17, 2022 8:29 pm
by c166
Hey Tony,
Thanks for the prompt reply - should have taken a look through the internal templates in the pack better. Though hyperlinking it in the documentation + having it available under templates is a bit confusing (just feedback from a non-savvy user - probably a bunch of reasons as to why it's structured the way it is)
I've given it a crack, but I've bumped into some unexpected problems and I'm not sure why. A possible culprit is probably the instructions to "add script to dialogue manager" which I haven't explicitly done?
The function that I want to call is in the PartyMember script, and it's on the same object as the dialogue actor/ dialogue system trigger
- lua_party_member.PNG (47.79 KiB) Viewed 1762 times
I followed the youtube tutorial and registered my function to Lua as follows:
Code: Select all
void onEnable()
{
Lua.RegisterFunction("AddPartyMember", this, SymbolExtensions.GetMethodInfo(() => AddPartyMember()));
}
void OnDisable()
{
Lua.UnregisterFunction("AddPartyMember");
}
Set the dropdown options so I can use the interface (works)
- Lua_function_dropdown.PNG (21.84 KiB) Viewed 1762 times
Set the function call (seems to just call the function + provide an alert as per the tutorial)
- lua_function_call.PNG (3.9 KiB) Viewed 1762 times
But get an error message saying that the function is not registered.
- Lua_error_message.PNG (80.41 KiB) Viewed 1762 times
Any idea as to what the cause of that may be/ what I'm missing?
~~~~~
Likely irrelevant, but the function itself
Code: Select all
public void AddPartyMember()
{
if(partyMemberList.Contains(heroTemplate.id))
{
Debug.Log("Member already in party");
}
else if(partyMemberList.Count==maxPartySize)
{
Debug.Log("Party already full");
SceneManager.LoadScene("PartyScreen");
}
else
{
partyMemberList.Add(heroTemplate.id);
Debug.Log("Added party member with id: " + heroTemplate.id);
}
}
Re: Unity Script Calling Function From Conversation (Lua)
Posted: Sat Dec 17, 2022 9:13 pm
by Tony Li
I'll try to fix that documentation issue. I agree it's confusing.
How many instances of this GameObject and/or the PartyMember script are there?
Lua.RegisterFunction() registers it globally; there can't be two AddPartyMember()s.
Is it possible that the last activity is that one of them ran its OnDisable() method and unregistered your method from Lua? The template script handles the "Unregister On Disable" checkbox, but it looks like maybe you changed the OnDisable() method to not handle it. (The comments in TemplateCustomLua.cs should supercede the basic code on the manual page. TemplateCustomLua has some extra helper code.)
If you temporarily set the Dialogue Manager's Other Settings > Debug Level to Info and then play, the Dialogue System will log a lot of info to the Console. Look for lines like:
Dialogue System: Registering Lua function AddPartyMember
and:
Dialogue System: Unregistering Lua function AddPartyMember
If you don't see "Registering Lua function" then it's not being registered.
If the last one you see it "Unregistering Lua function" then the function has been unregistered.
Re: Unity Script Calling Function From Conversation (Lua)
Posted: Sat Dec 17, 2022 9:36 pm
by c166
That's a nifty toggle to know about, great that you place different debug levels like that. So much functionality in this asset, with the toggles and options (makes it hard to figure out -> but can see that once you do, it'll open a lot of options). Also love the amount of documentation + tutorials
Anyway, thanks to that - discovered that the function wasn't being registered. Reason was that since I was copying from the youtube tutorial - I had `onEnable` instead of `OnEnable`
I have also updated the unregister function according to the template to the version with the boolean toggle. Everything seems to be working as expected after I updated the `OnEnable`
Currently, there's only one - but I WAS going to put it on every possible party member NPC. Though, thinking about it now - if it's embedded in the dialogue system; and can be called via the LuaFunction interface, I don't NEED to have the script on every character. Instead it seems like as long as I place the script to register the function SOMEWHERE (i.e. a singular "Game" instance object to hold all the custom Lua Functions per scene), it'll work as intended (party members to join via dialogue)
Appreciate the help and responses!
Re: Unity Script Calling Function From Conversation (Lua)
Posted: Sat Dec 17, 2022 10:30 pm
by Tony Li
Glad to help!
Re: Unity Script Calling Function From Conversation (Lua)
Posted: Sat Dec 17, 2022 10:39 pm
by c166
Hmm so thinking about it some more, the fact that the function registers a specific incidence of it, in order to make it generalizable (i.e. different party members with different party ids); I would need to have the function accept a parameter.
The function parameter would then have to be hardcoded in the dialogue option for each character; since I can't just retrieve it from the underlying object (say it's specified in another script `characteristics`) to insert into the lua function programmatically (please correct me if I'm wrong).
If it's possible to pass in characteristics that are defined by a monobehaviour on the object (e.g. a variable that is unique to each character); directly into lua function parameter; can you please send the link for the documentation?
Edit: Hardcoding each character id isn't impossible (since I reckon I'll have <20), but seems ripe for mistakes to be made as things get changed (so programmatically retrieving the underlying ids would be best).
Re: Unity Script Calling Function From Conversation (Lua)
Posted: Sat Dec 17, 2022 11:09 pm
by Tony Li
Lua functions take basic types (string, int, etc.) so that dialogue databases can be exported and imported from external formats such as articy:draft, Twine, etc.
Here are a few ideas:
- If you'll always call AddPartyMember() while in a conversation with an NPC that can join the party, then that NPC will be the conversation conversant. In AddPartyMember(), you can use the property DialogueManager.currentConversant to get the transform of the NPC.
- Or each NPC can add their own variant of the Lua function:
Code: Select all
Lua.RegisterFunction("AddPartyMember" + name, this, SymbolExtensions.GetMethodInfo(() => AddPartyMember(name)));
...
Lua.UnregisterFunction("AddPartyMember" + name);
...
public void AddPartyMember(string npcName) { ... }
- Or you could use a scene event instead of a Lua function. They're specific to a scene, though, and they don't survive exports if you plan to export your dialogue database to some other format. (Most people don't export; if anything, they import from some other format.)
Re: Unity Script Calling Function From Conversation (Lua)
Posted: Mon Dec 19, 2022 7:47 am
by c166
Thanks for the ideas, I'll give them a crack to see what I can do
Probably start with getting the transform and seeing how far I can get with that (think it'd be a useful exercise to see what I can do there).
The registering a unique Lua function seems to be the most straightforward/ guaranteed to work with what I've got. Is there a disadvantage to doing that? Are they expensive/ would they cause performance issues if there are too many? (just off the top of your head)
The scene event option is interesting for potential future use-cases, and might get back to that later (just trying to get basics working to start)
Re: Unity Script Calling Function From Conversation (Lua)
Posted: Mon Dec 19, 2022 8:54 am
by Tony Li
Unless you start to accumulate many thousands, the number of Lua functions doesn't matter, much in the same way that the number of C# methods in your project doesn't really matter. (Although that's note entirely a fair comparison because the C# compiler does additional optimizations, whereas the Dialogue System's Lua is just a fairly vanilla straightforward implementation. But, in any case, don't worry about the number of functions.)