Best practice to get GameObject of conversant whose conversation called a registered Lua function?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
James
Posts: 2
Joined: Wed Oct 13, 2021 7:36 pm

Best practice to get GameObject of conversant whose conversation called a registered Lua function?

Post by James »

My situation is the following:

I have multiple enemies in the scene (GameObject + Monobehavior), who will start a conversation with the player on sight. Depending on the answers, I want the enemies to attack the player, which is basically done by calling a function of the enemy.

I tried doing this with the Lua RegisterFunction method in the Monobehavior of the enemies, but as the documentation already says, I cannot use RegisterFunction for MonoBehaviors which are placed multiple times in the scene, since now, the "attack player" function of some other enemy is called and not the enemy who the player had the conversation with.

So, basically, what I need is a function that I can call from the dialogue system that calls a C# function for that enemy which was the conversant of that dialogue. Ultimately, I would like to call something like:

Code: Select all

enemy.attackPlayer();
or

Code: Select all

TriggerEnemyAttacksPlayer(GameObject enemy);
but both are not really possible from Lua.

What might be possible is something like:

Code: Select all

public void TriggerEnemyAttacksPlayer()
{
	GameObject enemy = GetGameObjectOfConversantWhoseConversationCalledThisFunction();
	enemy.attackPlayer();
}
But can I get that GameObject? What is the best practice for that?
User avatar
Tony Li
Posts: 22158
Joined: Thu Jul 18, 2013 1:27 pm

Re: Best practice to get GameObject of conversant whose conversation called a registered Lua function?

Post by Tony Li »

Hi,

That GameObject is in DialogueManager.currentConversant.

Alternatively, you could use a scene event to call the GameObject's attackPlayer() method.
James
Posts: 2
Joined: Wed Oct 13, 2021 7:36 pm

Re: Best practice to get GameObject of conversant whose conversation called a registered Lua function?

Post by James »

Thanks a lot for the reply!

In the mean time, I was researching some more and it seems that I can use the Sequence field with "SendMessage(attackPlayer,,listener)" to achieve what I wanted.
Took me some time to find that because I did not expect the Sequence field to be relevant for me and neither the "SendMessage" function to be so powerful.

Can this be considered the best practice here, or should I look more into the scene events?
User avatar
Tony Li
Posts: 22158
Joined: Thu Jul 18, 2013 1:27 pm

Re: Best practice to get GameObject of conversant whose conversation called a registered Lua function?

Post by Tony Li »

That's totally fine to do, too.
Post Reply