How to get reference to actor GO from dialogue in custom lua function?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Fitbie
Posts: 44
Joined: Tue Dec 07, 2021 6:30 pm

How to get reference to actor GO from dialogue in custom lua function?

Post by Fitbie »

Hey Tony and everybody!
I have my own inventory system, and i wish to check for some items during conversations.
So i implemented and register Custom Lua Functions.
But there is 1 problem: i wish i can get a reference to actor gameobject or any component, so i could

Code: Select all

actorGO.GetComponent<IHaveInventory>()
and get reference to Player's inventory.

In CustomLuaFunctionInfo wizard i can see the only acceptable parameter: actor.
So, my questions is
1) What is actor parameter on C# side? Is it a string?
2) How can i get reference to dialogue actor from my custom lua function to search for required interfaces?
Thanks!

(I know it can be done with singletons and custom actor databases, but is there a shortcut?)
User avatar
Tony Li
Posts: 22145
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to get reference to actor GO from dialogue in custom lua function?

Post by Tony Li »

Hi,

> 1) What is actor parameter on C# side? Is it a string?

It's an actor ID number.

> 2) How can i get reference to dialogue actor from my custom lua function to search for required interfaces?

If the actor is the current conversation's conversation actor, use DialogueManager.currentActor. If it's the conversation conversant, use DialogueManager.currentConversant.
Fitbie
Posts: 44
Joined: Tue Dec 07, 2021 6:30 pm

Re: How to get reference to actor GO from dialogue in custom lua function?

Post by Fitbie »

Hey! Thanks for reply!
I figured out another way, i don't know about pifalls, but it seems like it is working

Code: Select all

 private bool InventoryContainsItem(string actorName, double itemId, double amount) // Condition
    {
        Transform character = PixelCrushers.DialogueSystem.CharacterInfo.GetRegisteredActorTransform(actorName);
        if (!character.TryGetComponent<IHaveInventory>(out var inventoryOwner))
        {
            throw new ArgumentException($"You are trying to use custom dialogue method \"InventoryContainsItem()\", but GameObject, associated with passed actor {actorName} does not have ANY component, implementing IHaveInventory!");
        }
        
        return inventoryOwner.Inventory.ContainsItem((int)itemId, (int)amount);
    }
User avatar
Tony Li
Posts: 22145
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to get reference to actor GO from dialogue in custom lua function?

Post by Tony Li »

Hi,

That's perfectly fine. It's exactly what I would have suggested next if you weren't using the conversation actor or conversation conversant.
Post Reply