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
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
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
LUA Function - Object does not match Target Type
-
- Posts: 10
- Joined: Wed Apr 27, 2022 11:01 pm
Re: LUA Function - Object does not match Target Type
Hi,
Try creating methods inside your PlayerController script:
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);
}
-
- Posts: 10
- Joined: Wed Apr 27, 2022 11:01 pm
Re: LUA Function - Object does not match Target Type
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!
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!
Re: LUA Function - Object does not match Target Type
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:
However, I just think it's easier if you always map functions to methods in the same class.
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)));
-
- Posts: 10
- Joined: Wed Apr 27, 2022 11:01 pm
Re: LUA Function - Object does not match Target Type
Ohh, I understanding what's happening now Thanks for explaining.
Also I agree, I think it is easier to just do as you suggested!
Also I agree, I think it is easier to just do as you suggested!
Re: LUA Function - Object does not match Target Type
Happy to help!