Registered Lua function throwing exception.

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
emeldi
Posts: 3
Joined: Wed Oct 06, 2021 2:48 pm

Registered Lua function throwing exception.

Post by emeldi »

Registered Lua functions are throwing exception when accessed via dialogue during play. Not sure what to do about this error--but it basically cripples my ability to move forward, so any help is much appreciated.

Registered the following function on a scriptable object in the scene:

Code: Select all

public void AddStoryScore(double value)
{
    storyScore += value;
    OnScoreChange?.Invoke();
}
Registered in OnEnable() with the following:

Code: Select all

Lua.RegisterFunction("AddStoryScore", this, SymbolExtensions.GetMethodInfo(() => AddStoryScore(0)));

Successful registration log:

Code: Select all

Dialogue System: Registering Lua function AddStoryScore
UnityEngine.Debug:Log (object)
PixelCrushers.DialogueSystem.Lua:RegisterFunction (string,object,System.Reflection.MethodInfo) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:286)
DungeonMasterTycoon.Score:OnEnable () (at Assets/Scripts/DM/Score.cs:76)
Exception log:

Code: Select all

Dialogue System: Lua code 'AddStoryScore(1)' threw exception 'Tried to invoke a function call on a non-function value. If you're calling a function, is it registered with Lua?'
UnityEngine.Debug:LogError (object)
PixelCrushers.DialogueSystem.Lua:RunRaw (string,bool,bool) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:228)
PixelCrushers.DialogueSystem.Lua:Run (string,bool,bool) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:129)
PixelCrushers.DialogueSystem.ConversationModel:GetState (PixelCrushers.DialogueSystem.DialogueEntry,bool,bool,bool) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Model/ConversationModel.cs:234)
PixelCrushers.DialogueSystem.ConversationModel:GetState (PixelCrushers.DialogueSystem.DialogueEntry) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Model/ConversationModel.cs:304)
PixelCrushers.DialogueSystem.ConversationController:OnSelectedResponse (object,PixelCrushers.DialogueSystem.SelectedResponseEventArgs) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Controller/ConversationController.cs:304)
PixelCrushers.DialogueSystem.ConversationView:SelectResponse (PixelCrushers.DialogueSystem.SelectedResponseEventArgs) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/View/View/ConversationView.cs:494)
PixelCrushers.DialogueSystem.ConversationView:OnSelectedResponse (object,PixelCrushers.DialogueSystem.SelectedResponseEventArgs) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/View/View/ConversationView.cs:487)
PixelCrushers.DialogueSystem.AbstractDialogueUI:OnClick (object) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/UI/Abstract/Dialogue/AbstractDialogueUI.cs:346)
PixelCrushers.DialogueSystem.StandardDialogueUI:OnClick (object) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/UI/Standard/Dialogue/StandardDialogueUI.cs:260)
PixelCrushers.DialogueSystem.StandardUIResponseButton:OnClick () (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/UI/Standard/Dialogue/StandardUIResponseButton.cs:132)
UnityEngine.EventSystems.EventSystem:Update () (at Z:/Program Files/Unity Editors/2020.3.6f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:385)
User avatar
Tony Li
Posts: 22158
Joined: Thu Jul 18, 2013 1:27 pm

Re: Registered Lua function throwing exception.

Post by Tony Li »

Hi,

That exception means it's not finding "AddStoryScore" in the list of functions registered with Lua. Is it possible that it's getting unregistered, perhaps by a corresponding OnDisable() method?

If your Score script is on a singleton GameObject, it's possible that a second instance is being destroyed by the original singleton instance. If Score.OnDisable() or OnDestroy() unregisters "AddStoryScore", then it would unregister the function that the original singleton instance registered. The TemplateCustomLua.cs script has some example code that suggests how to prevent this.

Side note: Lua functions use doubles for numbers, so you might want to make it explicit here:

Code: Select all

Lua.RegisterFunction("AddStoryScore", this, SymbolExtensions.GetMethodInfo(() => AddStoryScore((double)0)));
emeldi
Posts: 3
Joined: Wed Oct 06, 2021 2:48 pm

Re: Registered Lua function throwing exception.

Post by emeldi »

Tony Li wrote: Wed Oct 06, 2021 3:52 pm That exception means it's not finding "AddStoryScore" in the list of functions registered with Lua. Is it possible that it's getting unregistered, perhaps by a corresponding OnDisable() method?
I tried commenting out the Unregister calls in OnDisable() to make sure--no change. The log shows the function being registered, and does not show anything being unregistered.
If your Score script is on a singleton GameObject...
The Score script is not on a singleton. It's a Scriptable Object .asset stored in a serialized field on a MonoBehaviour in-scene.
User avatar
Tony Li
Posts: 22158
Joined: Thu Jul 18, 2013 1:27 pm

Re: Registered Lua function throwing exception.

Post by Tony Li »

Is it possible there's a typo, such as a blank space in the Lua.RegisterFunction name, or a typo in the Lua call in your conversation?

Let's make sure the function is registered. Add a Lua Console to your scene. Press ~+L to open it during play. Then enter "return AddStoryScore". It should return "Language.Lua.LuaMethodFunction". For example, in the screenshot below I checked the built-in Lua function "SetQuestState":

checkLuaRegistered.png
checkLuaRegistered.png (22.8 KiB) Viewed 857 times

If it isn't registered, it will return "nil" instead.
emeldi
Posts: 3
Joined: Wed Oct 06, 2021 2:48 pm

Re: Registered Lua function throwing exception.

Post by emeldi »

I managed to solve the problem by moving the registration calls into the MonoBehaviour, along with methods that point to the methods in the scriptable object. I tried registering the methods through the reference to the object:

Code: Select all

 Lua.RegisterFunction("AddStoryScore", this, SymbolExtensions.GetMethodInfo(() => score.AddStoryScore(0))


but that threw a type mismatch between the Game Manager (the monobehaviour) and the Score scriptable object. If I register a local method that points to the method I want to call through reference, that works:

Code: Select all

void AddStoryScore(double value) => score.AddStoryScore(value);
void OnEnable() => Lua.RegisterFunction("AddStoryScore", this, SymbolExtensions.GetMethodInfo(() => AddStoryScore(0)));
So it seems like the real problem was trying to register methods from a scriptable object.
User avatar
Tony Li
Posts: 22158
Joined: Thu Jul 18, 2013 1:27 pm

Re: Registered Lua function throwing exception.

Post by Tony Li »

Hi,

Glad you got it working!
Post Reply