Registering methods for scripts that are attached to multiple game objects

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Sternutation
Posts: 22
Joined: Wed Aug 19, 2020 2:44 pm

Registering methods for scripts that are attached to multiple game objects

Post by Sternutation »

Hello,

I was wondering about registering methods with Lua for scripts that are used in multiple gameobjects.

If, for example, I wanted to create a relationship system where the player's relationship with the NPC is associated only with that NPC and not with all NPCs, then I would also like to use that system in Lua.

The problem being that registering methods may not be that straightforward. I have registered a method for a script that only had one instance in the scene, but what if I had a script that was attached to multiple objects in the scene?

For example, if I create and register a method that returns the relationship value for a NPC, and in the conversation nodes I want to use it as a condition check(example- GetRelationship()<5), then how does the System know where to check for this, since the script is being used across multiple objects and the values of GetRelationship() will differ for each?


The second question I had was related to the "relationship type" parameter in the https://www.pixelcrushers.com/dialogue_ ... pFunctions that the dialogue system provides. Is this "relationship type" created by us or are there multiple relationship types already present and built-in?
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Registering methods for scripts that are attached to multiple game objects

Post by Tony Li »

Hi,

Relationship types are defined by you. They can be any strings you choose.

Lua.RegisterFunction registers the function globally. You can register differently-named functions for different NPCs. Otherwise, if you use the same function, you can pass the NPC name to the function or write the function to assume it's operating on the current conversation conversant (DialogueManager.currentConversant).
Sternutation
Posts: 22
Joined: Wed Aug 19, 2020 2:44 pm

Re: Registering methods for scripts that are attached to multiple game objects

Post by Sternutation »

Ahh, I see. I will first attempt to use the built-in relationship functions.

Here's an implementation that I have created for testing within a sample scene, based on the built-in relationship functions-

Code: Select all

    public float relationValue;
    public float initialValue;
    void Start()
    {
        dialogueActor = gameObject.AddComponent<DialogueActor>();
        dialogueActor.actor = ActorName;
        string luaScript = "Actor['" + ActorName + "'] = { Name = '" + ActorName + "', Display_Name = '" + gameObject.name + "'}";
        Lua.Run(luaScript);
        SetRelationship = "SetRelationship(Actor[" + @"""" + dialogueActor.actor + @"""" + "], Actor[" + @"""Player""" + "]," + @""""+relationshipType+@""""+ ", "+initialValue+")";
        Lua.Run(SetRelationship);
        GetRelationship = "GetRelationship(Actor[" + @"""" + dialogueActor.actor + @"""" + "], Actor[" + @"""Player""" + "]," + @""""+relationshipType+@"""" + ")";
        relationValue = Lua.Run(GetRelationship).AsFloat;
        
    }
    private void Update()
    {
        relationValue = Lua.Run(GetRelationship).asFloat;
    }
Where relationshipType is an enum value.

However, it seems to me that the relationValue is not getting the value of the Relationship. As you see, I used SetRelationship to set the relationship to a default value, given by the float variable initialValue, and then used GetRelationship to draw it into relationValue as a float.

But in the Unity inspector, relationValue remains at 0, not changing at all to what the initialValue may be(in my case I set it to 5)
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Registering methods for scripts that are attached to multiple game objects

Post by Tony Li »

Hi,

Two minor issues to fix:

1. When you want to return a value from Lua.Run(), you must include the word "return" in front. Change this line:

Code: Select all

GetRelationship = "GetRelationship(Actor[" + @"""" + dialogueActor.actor + @"""" + "], Actor[" + @"""Player""" + "]," + @""""+relationshipType+@"""" + ")";
to this:

Code: Select all

GetRelationship = "return GetRelationship(Actor[" + @"""" + dialogueActor.actor + @"""" + "], Actor[" + @"""Player""" + "]," + @""""+relationshipType+@"""" + ")";
2. If your actor name has non-alphanumeric characters, the Actor[..] index will be changed to replace those characters. For example, "Private Hart" will be Actor["Private_Hart"] in the Actor[..] table. (See Important Note About Table Indices) Use DialogueLua.StringToTableIndex() to replace those characters. For example, change this:

Code: Select all

GetRelationship = "return GetRelationship(Actor[" + @"""" + dialogueActor.actor + @"""" + ...
to this:

Code: Select all

GetRelationship = "return GetRelationship(Actor[" + @"""" + DialogueLua.StringToTableIndex(dialogueActor.actor) + @"""" + ...
Post Reply