Page 1 of 2

Accessing scripts

Posted: Thu Jun 06, 2024 5:18 pm
by hrohibil
Hello Tony

Please advice me here

I have a shop.
When player enters, he goes to the NPC. He has the option to buy food or guns.

But he can only buy food if he has cash. I have a shopManager script where i handle my cash, food, items.
I want to access this script so if he has cash to buy food, then proceed to buy an get the food and the amount should be drawn from my script which is a public method as well as adding the 1x food item.

Re: Accessing scripts

Posted: Thu Jun 06, 2024 7:43 pm
by Tony Li
Hi,

If you want to run the shop using a conversation:

1. Make your script's methods available to the Dialogue System by registering them with Lua.

2. Then use them in your conversation's Conditions and Script fields.

Re: Accessing scripts

Posted: Fri Jun 07, 2024 4:42 am
by hrohibil
Hey Toni

Thank you for reply.

Before I proceed I just want to confirm that this is the easiest approach for me. Because as I had a quick look to the LUA video tutorial and your written instructions it seems like is focusing on single situation like one int or one bool, in my situation I want to run a public method from my script and in this method for instance public void buyFood() I have several things taken place here, it will add 1 food, it will play a audio file, it will display a popup notification.

Is there not a “simple” way to fire this method within a dialogue node?


One more thing is, NPC do need to check if currentCash is above foodPrice. Would that be a lua condition to check? Though I have this check already in my script but for I guess for the dialogue nodes to recognise this I need LUA?

Here is the conversation example:

NPC:
Hello

Player:
Hi

NPC:
What can I help with today?

Player:
I want to buy food.

NPC:
(Condition to check if currentCash is above foodPrice )
If YES:
—Sure thing here is todays food.
Run the public method buyFood()
If NO:
-Sorry you don’t have enough cash
(After player buys a food and cash then goes below foodPrice, then the text above should run)


I hope you got all the info to understand what I am trying to achieve, I am not a programmer.

Thank you so much for your support Toni.

Br Hamid

Re: Accessing scripts

Posted: Fri Jun 07, 2024 8:27 am
by Tony Li
Hi,

You should register 2-3 methods with Lua. For example:

Code: Select all

void Awake()
{
    Lua.RegisterFunction(nameof(getCurrentCash), this, SymbolExtensions.GetMethodInfo(() => getCurrentCash()));
    Lua.RegisterFunction(nameof(getFoodPrice), this, SymbolExtensions.GetMethodInfo(() => getFoodPrice()));
    Lua.RegisterFunction(nameof(buyFood), this, SymbolExtensions.GetMethodInfo(() => buyFood()));
}

double getCurrentCash() { return currentCash; }
double getFoodPrice() { return foodPrice; }
void buyFood() { /* your code here */ }
Then, in your conversation:
  • Player: "I want to buy food."
    • NPC: "Sure thing. Here is today's food."
      Conditions: getCurrentCash() >= getFoodPrice()
      Script: buyFood()
    • NPC: "Sorry you don’t have enough cash"
      Conditions: getCurrentCash() < getFoodPrice()

Re: Accessing scripts

Posted: Fri Jun 07, 2024 10:50 am
by hrohibil
Toni


I am getting error on two, my currentMoney and foodPrice.
Non-invocable member 'shopManager.currentMoney' cannot be used like a method.

My int for currentMoney and foodPrice are just variables like this:
public int foodPrice = 20;



Re: Accessing scripts

Posted: Fri Jun 07, 2024 10:55 am
by Tony Li
Please see my example. I added methods to return those variable values.

Re: Accessing scripts

Posted: Fri Jun 07, 2024 12:04 pm
by hrohibil
Forgive me Toni.

Yes you did and I used your exact example as see you see in the attached screenshot.

I am not sure what I am doing wrong here..

Re: Accessing scripts

Posted: Fri Jun 07, 2024 12:17 pm
by hrohibil
Ahh wait a minute..

I think I see it.

The methods you gave me for the variables were not spelled correctly on my side..

Sorry Toni. I try again.

For setting them up to be available as condition and script I can follow your video tutorial on setting up the custom lua right?

Can I have as many variables and methods on the same script? This seems like insane nice integration between LUA and C#

Re: Accessing scripts

Posted: Fri Jun 07, 2024 2:25 pm
by hrohibil
Toni

Following your tutorial, i created a custom lua with the two variables "getCurrentCash" and "getFoodPrice" see screendump below.

But for the script metod, "BuyFood", how do i set that up here? I mean what parameter should it take as it is a method?

Furthermore, in my custom classes I only have EmeraldAi and Invector available, i cant find my new script



Re: Accessing scripts

Posted: Fri Jun 07, 2024 8:50 pm
by Tony Li
Hi,

If your BuyFood() method doesn't accept any parameters, then you don't need to define any parameters in your CustomLuaFunctionInfo asset. I recommend watching the Connect Your Code To Lua video tutorial.