Int Value from Script checked inside Lua

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
DeidreReay
Posts: 93
Joined: Wed Jan 22, 2020 1:20 pm

Int Value from Script checked inside Lua

Post by DeidreReay »

So I have been wracking my head for an hour and sure its something simple I am just missing.
I have public Int Xp
So far i have got it working correctly to GIVE XP from dialouge and is set up inside custom lua function for ease of use.

public void GiveXP(double amount)
{
Xp += (int)amount;
}

void OnEnable()
{

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

Then setting custom lua to
Name: GiveXP
Parameter: Double
Return Value: None

That works great. However, I need to setup method and custom lua function for the XP Ammount (total ammount of xp player has) and then be able to check for conditions like XPAmount greater than, equal to etc. Hoping this can then make real easy way to set up RPG like quests and dialogue. Hope that was clear enough.
DeidreReay
Posts: 93
Joined: Wed Jan 22, 2020 1:20 pm

Re: Int Value from Script checked inside Lua

Post by DeidreReay »

P.S. Followed tutorials and not sure why I just cant get the dang INT to be recognized by the dialogue system. Gotta be something silly I am missing. Any help would be appreciated
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Int Value from Script checked inside Lua

Post by Tony Li »

Hi,

Lua doesn't have C# properties, but you can write a write a C# method to return the int's value:

Code: Select all

public Int Xp

public void GiveXP(double amount)
{
    Xp += (int)amount;
}

public double CurrentXP()
{
    return Xp;
}

void OnEnable()
{
    Lua.RegisterFunction(nameof(GiveXP), this, SymbolExtensions.GetMethodInfo(() => GiveXP((double)0)));
    Lua.RegisterFunction(nameof(CurrentXP), this, SymbolExtensions.GetMethodInfo(() => CurrentXP()));
}
Note: In Lua.RegisterFunction(), I used the newer C# "nameof(type)" syntax instead of a string "GiveXP". It's not compatible with older versions of Unity/.NET, but current versions all support it, and it helps prevent typos. The manual uses the older string form in case devs are using older versions of Unity.
Post Reply