Page 1 of 1

Int Value from Script checked inside Lua

Posted: Tue Sep 27, 2022 2:37 pm
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.

Re: Int Value from Script checked inside Lua

Posted: Tue Sep 27, 2022 2:52 pm
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

Re: Int Value from Script checked inside Lua

Posted: Tue Sep 27, 2022 3:03 pm
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.