Page 1 of 1

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

Posted: Wed Oct 13, 2021 7:49 pm
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?

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

Posted: Wed Oct 13, 2021 8:35 pm
by Tony Li
Hi,

That GameObject is in DialogueManager.currentConversant.

Alternatively, you could use a scene event to call the GameObject's attackPlayer() method.

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

Posted: Wed Oct 13, 2021 8:43 pm
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?

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

Posted: Wed Oct 13, 2021 8:53 pm
by Tony Li
That's totally fine to do, too.