Hey there!
I am wondering if there is a easy way to do the following:
Lets say at the begin of my game I want to spawn 10 actors which all share the same conversation tree.
But each of those actors has a different condition, for example a item which he responds to.
So [actor1] only responds positively to [item1], [actor2] to [item2]...
The way I think it works is I have to add an additional script to the actor which sets a global variable of say: 'validItem' to the one the actor I'm interacting with will respond to.
In the condition of the dialogue I will then compare this set value, with the items the player is currently holding.
That way the condition can stay generic, but will adapt to a local value I can either set in the editor or while instantiating actors.
This method however, feels kinda off to me because it features a separate script and setting global values 'on use'.
My question is, is there a better way to do this, or would this be the intended route?
Generic actors and dynamic conditions
Re: Generic actors and dynamic conditions
Hi,
You can probably take advantage of Variable["ConversantIndex"].
When you start a conversation, you can specify the two primary participants, the actor and conversant. Details: Character GameObject Assignments.
When a conversation starts, it sets 4 variables:
To access this in Lua, you could use:
Say the active conversation is using NPC 3 as the conversant. Then this would be equivalent to:
Now let's say you have a C# method PlayerHasItem("itemName") that checks if the player has an item. You've registered this C# method with Lua so you can use it in conversations' Conditions fields. Then you could check it like this:
If you don't want to do that, you could always check it all in a C# method. Let's say you have a C# method IsDynamicConditionTrue() that you've registered with Lua. Example:
Then you can just set the dialogue entry node's Conditions field to IsDynamicConditionTrue().
You can probably take advantage of Variable["ConversantIndex"].
When you start a conversation, you can specify the two primary participants, the actor and conversant. Details: Character GameObject Assignments.
When a conversation starts, it sets 4 variables:
- Variable["ActorIndex"]: The index in the Actor[] table of the character currently being used as the actor (e.g., "Player").
- Variable["Actor"]: The display name of the character currently being used as the actor (e.g., "The Chosen One").
- Variable["ConversantIndex"]: The Index in the Actor[] table of the character currently being used as the conversant (e.g., "NPC_3")
- Variable["Conversant"]: The display name of the character currently being used as the conversant (e.g., "Joe the NPC").
To access this in Lua, you could use:
Code: Select all
Actor["NPC_3"].validItem
Code: Select all
Actor[Variable["ConversantIndex"]].validItem
If you don't want to do that, you could always check it all in a C# method. Let's say you have a C# method IsDynamicConditionTrue() that you've registered with Lua. Example:
Code: Select all
bool IsDynamicConditionTrue()
{
string conversantIndex = DialogueLua.GetVariable("ConversantIndex").asString;
string validItem = DialogueLua.GetActorField(conversantIndex, "validItem").asString;
return PlayerHasItem(validItem);
}
Re: Generic actors and dynamic conditions
Hi Tony,
Thank you so much for your swift response!
I get what you are saying, but, and correct me if I am wrong.
But if I spawn X amount of duplicates of a prefab actor, won't they all have the exact same conversantIndex assigned to them?
The intended goal is to spawn X actors which share the same dialogue tree, but (determined at random) they all require a different valid item and won't accept anything else.
So maybe I am misreading part of your explanation, but right now you have banana 'hardcoded' on NPC_3.
I know it is possible to change these values from outside, but my worry is that each NPC_3 conversant will want the exact same valid item.
Thank you so much for your swift response!
I get what you are saying, but, and correct me if I am wrong.
But if I spawn X amount of duplicates of a prefab actor, won't they all have the exact same conversantIndex assigned to them?
The intended goal is to spawn X actors which share the same dialogue tree, but (determined at random) they all require a different valid item and won't accept anything else.
So maybe I am misreading part of your explanation, but right now you have banana 'hardcoded' on NPC_3.
I know it is possible to change these values from outside, but my worry is that each NPC_3 conversant will want the exact same valid item.
Re: Generic actors and dynamic conditions
I assumed each actor would be unique. You can create new actors at runtime. Example:
---
However, if you don't want to do that, you could take a completely different approach and go by the actor's GameObject. For example, say the prefab has this script:
Then:
You can write a C# method that checks the current conversant's validItem:
Then register that method with Lua, and check it in a dialogue entry's Conditions field.
Code: Select all
for (int i = 1; i <= 10; i++)
{
string npcName = "NPC " + i;
Lua.Run("Actor['NPC_" + i + "'] = { Name='" + npcName + "' }); // Create an empty actor.
DialogueLua.SetActorField(npcName, "validItem", SomeRandomItem());
}
However, if you don't want to do that, you could take a completely different approach and go by the actor's GameObject. For example, say the prefab has this script:
Code: Select all
public class ActorItemInfo : MonoBehaviour
{
public string validItem;
}
Code: Select all
var actor = Instantiate(myPrefab);
actor.GetComponent<ActorItemInfo>().validItem = SomeRandomItem();
Code: Select all
bool DoesPlayerHaveValidItem()
{
var actorItemInfo = DialogueManager.currentConversant.GetComponent<ActorItemInfo>();
return DoesPlayerHaveItem(actorItemInfo.validItem);
}
Re: Generic actors and dynamic conditions
Ah excellent!
Both options would work very well with what I have in mind!
I wasn't aware that it was that easy to create new actors at runtime with custom values.
Right now I have the system working with the second method you supplied and it works wonders.
Thank you very much for your help with figuring this out!
Another reason why I don't mind that I bought this asset right before it went on sale
Both options would work very well with what I have in mind!
I wasn't aware that it was that easy to create new actors at runtime with custom values.
Right now I have the system working with the second method you supplied and it works wonders.
Thank you very much for your help with figuring this out!
Another reason why I don't mind that I bought this asset right before it went on sale