Running method with Lua

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Kadum1013
Posts: 9
Joined: Fri Mar 15, 2024 9:10 pm

Running method with Lua

Post by Kadum1013 »

Hello all, I am still new to this and was wondering a couple of things. Can't seem to find anything on it.
I have a dialogue for 3 different NPC, an Arena Master, Weapon shop and Armor shop. Each one has there own dialogue.
Currently I had when the player interacted with one of the NPC it would open an UI of said category Armor, Weapon, Arena.
To open the UI on the shop and arena it would pass that script to the player. I.E Armor would pass the info the shop script to the player to update the UI with the info on that Armor shop info. The problem I am having is the armor shop and weapon shop both have a Shop script on the NPC. So when running the dialogue it seems to randomly pick between which one its going to open and then stay with the choice no matter who I talk to.

Is there a way to separate it? So it will only run the script thats on that specific NPC or when the dialogue runs the Lua script on the shop it will call it on all shops?

Any help would be wonderful! Just need a direction to move towards and have a better understanding on out the Dialogue system works. Thank you!
Kadum1013
Posts: 9
Joined: Fri Mar 15, 2024 9:10 pm

Re: Running method with Lua

Post by Kadum1013 »

I think I might have figured out a way. If I have on the the shops a run execute that will RegisterFunction to opening the UI, it will only Register Function on that game object I pass into the execute. That seems to work or at least for now haha.
I will go with that ATM unless there is a better way and I am just missing it XD
User avatar
Tony Li
Posts: 21059
Joined: Thu Jul 18, 2013 1:27 pm

Re: Running method with Lua

Post by Tony Li »

I recommend using a custom sequencer command. (See part 6 of the Cutscene Sequence Tutorials.) You can use the sequencer command's 'speaker' or 'listener' property to refer to the dialogue entry's speaker or listener GameObject. (More info: Conversation GameObject Assignments)

Rough example:

Code: Select all

// Sequencer command OpenShop() opens Shop script on speaker.
public class SequencerCommandOpenShop : SequencerCommand
{
    void Awake()
    {
        if (speaker == null) 
        {
            Debug.LogWarning("No GameObject is assigned to the speaker property.");
        }
        else
        {
            var shop = speaker.GetComponent<Shop>();
            if (shop == null)
            {
                Debug.LogWarning($"{speaker} doesn't have a Shop component.", speaker);
            }
            else
            {
                shop.Open();
            }
        }
        Stop();
    }
}
Kadum1013
Posts: 9
Joined: Fri Mar 15, 2024 9:10 pm

Re: Running method with Lua

Post by Kadum1013 »

Ahh I see, I will change it to do it that way!
Also I am wondering the variables on the Dialogue system, I am using it as a way to check if the NPC has meet the player before, is there a way to have it set to true only for that NPC or do I need to make a variable for each NPC?
User avatar
Tony Li
Posts: 21059
Joined: Thu Jul 18, 2013 1:27 pm

Re: Running method with Lua

Post by Tony Li »

Use a variable for each NPC. You can use '.' to group variables, such as "Met.Ann", "Met.Bob","Met.Carl", etc. These will group into the submenu/foldout "Met". Or, if each NPC has several variables, you can do for example "Ann.Met", "Ann.Age", "Ann.Money", "Ann.Health", etc.
Kadum1013
Posts: 9
Joined: Fri Mar 15, 2024 9:10 pm

Re: Running method with Lua

Post by Kadum1013 »

Okay, let me see if I understand this correctly. I have 3 different NPC's I can talk to, I would set a variable for each one I.E Met Weapon shop, Met Arena Master, Met Armor shop. Now the only thing is I will have different zones the player can be in which in turn as another set of NPC's. So in total i would have 4 different arena masters, 4 different weapon shops and 4 different armor shops. Would I then need to make 4 different weapon shop variables? Met weapon shop 1, met weapon shop 2 and so on with the others as well?
User avatar
Tony Li
Posts: 21059
Joined: Thu Jul 18, 2013 1:27 pm

Re: Running method with Lua

Post by Tony Li »

Yes, they're all separate data points, so they should be separate variables. Or, if you want to bundle that data inside the character definitions themselves, you could use custom fields in the Actor template instead of variables. However, in general, I find variables easier to use.
Kadum1013
Posts: 9
Joined: Fri Mar 15, 2024 9:10 pm

Re: Running method with Lua

Post by Kadum1013 »

Perfect! Thank you, I will go with the variables.

I have another question for you haha.
How would I save the variables? I have a saving system I am already using and don't want to switch over with the project I am currently working on and am wondering how would I go about saving the variables for the dialogue? I looked at the tutorial for using the saving system that comes with the dialogue. I tired just adding the Save system methods script on my saving system game object and when ever I save/load it will also call that save system methods save and load. I have the load method at the last part of my loading save method. So it will run my saving systems load method which in turn loads the current scene the save is on and then load the dialogue systems load method which then puts me make to the main menu.

Hopefully I explained that correctly lol.
Kadum1013
Posts: 9
Joined: Fri Mar 15, 2024 9:10 pm

Re: Running method with Lua

Post by Kadum1013 »

Welp I am stupid haha. it was staring me in the face. On the Save system script in the Inspector I found the save current scene and just unchecked the box and now it works fine!!

Thank you again for all the help you have provided me!
User avatar
Tony Li
Posts: 21059
Joined: Thu Jul 18, 2013 1:27 pm

Re: Running method with Lua

Post by Tony Li »

Glad you got it working!
Post Reply