LUA Function - Object does not match Target Type

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
AvalonGames
Posts: 10
Joined: Wed Apr 27, 2022 11:01 pm

LUA Function - Object does not match Target Type

Post by AvalonGames »

Hey Tony,

I was trying to register my custom Methods and followed your tutorial : Connecting C# Methods to LUA.

So I wrote similar code as shown below
LUA.png
LUA.png (88.61 KiB) Viewed 244 times
I registered 3 methods to test. The first two are calling LevelUp() and GainExp() from a Character ScriptableObject, and GainXp is calling a local method.

GainXp works without any issues.

When I try to call my two functions from my scriptable Object I run into Object does not match Target Type error. I have the methods registered to the LUA Function
lua2.png
lua2.png (2.86 KiB) Viewed 244 times
lua3.png
lua3.png (31.05 KiB) Viewed 244 times
I know there's another thread that was similar to my question, but I wasn't able to fully draw a solution to my issue.

Thanks again as always for helping me debug.
Cheers
User avatar
Tony Li
Posts: 21679
Joined: Thu Jul 18, 2013 1:27 pm

Re: LUA Function - Object does not match Target Type

Post by Tony Li »

Hi,

Try creating methods inside your PlayerController script:

Code: Select all

Lua.RegisterFunction("GainExp", this, SymbolExtensions.GetMethodInfo(() => PlayerGainExp((double)0)));
...
private void PlayerGainExp(double exp)
{
    this.player.GainExp(exp);
}
AvalonGames
Posts: 10
Joined: Wed Apr 27, 2022 11:01 pm

Re: LUA Function - Object does not match Target Type

Post by AvalonGames »

Hey Tony,

Thanks as always, this way works and fixes the issue I'm having, but I was wondering why calling the method directly from the Scriptable Object seems to cause an error?

Correct me if I'm wrong, but from my understanding with OOP if you need a behavior to change properties of an object you’d define it as a function in that class (In this case I have the functions in the SO class). So creating a local method to call player.GainExp() seems kind of redundant to me.

Thanks! :)
User avatar
Tony Li
Posts: 21679
Joined: Thu Jul 18, 2013 1:27 pm

Re: LUA Function - Object does not match Target Type

Post by Tony Li »

The issue is in the Lua registration. You're mapping the Lua function to an instance of your PlayerController script (this), but you're mapping a method that's in a different class and instance (this.player).

Technically you could do something like this, which will map the Lua function to the CharacterDataSO instance:

Code: Select all

Lua.RegisterFunction("GainExp", this.player, SymbolExtensions.GetMethodInfo(() => this.player.GainExp((double)0)));
However, I just think it's easier if you always map functions to methods in the same class.
AvalonGames
Posts: 10
Joined: Wed Apr 27, 2022 11:01 pm

Re: LUA Function - Object does not match Target Type

Post by AvalonGames »

Ohh, I understanding what's happening now :) Thanks for explaining.

Also I agree, I think it is easier to just do as you suggested!
User avatar
Tony Li
Posts: 21679
Joined: Thu Jul 18, 2013 1:27 pm

Re: LUA Function - Object does not match Target Type

Post by Tony Li »

Happy to help!
Post Reply