We've got a "KnowledgeBase" as we called it in our game. Huge table of true/false keys, containing various information known to player, like "Location01Visited", "Secret04Found" or "NPC07Conversation01Finished"
So any piece of this info might be used in any place in the game. One secret will be opened after another, visited location will be shown on map for example and so on ... and we want to use this pieces of knowledge in conversations as well. Like, add dialogue option if some location is visited. Or on specific dialogue entry in conversation NPC will give player knowledge about something, like a secret passage or other knowledge that'll be used in another conversation with another NPC.
KnowledgeBase got fancy editor to add, categorize etc.
So what I want is to sync our base with Dialogue Variables on edit time. This way we wont have to add same entries with the same name in two different places.
Then I thought to, maybe, specify what variables will be used in specific dialogue, so I'll be able to set current variables from KnowledgeBase to DialogueVariables on conversation start and sync back when dialogues ends from DialogueVariables to KnowledgeBase to check if we've got some Knowledge set to true during the conversation.
Maybe you'll come up with better idea on how to implement this.
Anyways. I tried to dig into the API and figured out that "CreateNewVariable()" is hidden somwhere in Dialogue Editor Scripts.
Maybe I'm missing something, is there some way to add variables with api?
Add Variable and Get All Variables in code
Re: Add Variable and Get All Variables in code
I have two different suggestions. You can choose the one you prefer.
1. Sync variables back-and-forth:
To add variables to the database at edit time (not runtime):
To sync variables, add OnConversationStart and OnConversationEnd methods to a script on the Dialogue Manager. Use DialogueLua.SetVariable and GetVariable:
(Note: To add variables at runtime, simply use DialogueLua.SetVariable. But in this case you can't use the Lua dropdowns in the editor because the variables aren't in the database yet.)
API:
2. Register Lua functions to read knowledgebase directly:
(I like this method because it keeps the data in only one place.)
Use Lua.RegisterFunction to register a Lua function that reads/writes directly to the knowledgebase. (See Registering Functions)
Then use those functions in dialogue entry nodes' Conditions and Script fields:
1. Sync variables back-and-forth:
To add variables to the database at edit time (not runtime):
Code: Select all
var template = TemplateTools.LoadFromEditorPrefs();
var variableID = template.GetNextVariableID(database);
var variable = template.CreateVariable(variableID, "VariableName", true, FieldType.Boolean);
database.variables.Add(variable);
Code: Select all
foreach (var key in knowledgebase)
{
DialogueLua.SetVariable(key, value);
}
API:
2. Register Lua functions to read knowledgebase directly:
(I like this method because it keeps the data in only one place.)
Use Lua.RegisterFunction to register a Lua function that reads/writes directly to the knowledgebase. (See Registering Functions)
Code: Select all
void Awake()
{
Lua.RegisterFunction("GetKBValue", this, SymbolExtensions.GetMethodInfo(() => GetKBValue(string.Empty)));
Lua.RegisterFunction("SetKBValue", this, SymbolExtensions.GetMethodInfo(() => SetKBValue(string.Empty)));
}
bool GetKBValue(string key)
{
return knowledgebase[key].playerKnowsThis;
}
- Conditions: GetKBValue("Aliens") == true
- Dialogue Text: "I see you know about the aliens!"
Re: Add Variable and Get All Variables in code
Incredible! Just what I wanted
Re: Add Variable and Get All Variables in code
Happy to help!
Re: Add Variable and Get All Variables in code
We implemented the second option. Very useful feature, I made a few more useful lua functions. I've gon one more question. Is it possible to use UI to compose Lua fuictionality, like on screenshot? Is there a way to add custom things in there? Like along with "Quest" option add "Knowledge" option and instead of "Candies" - dropdown with knowledge options?
Re: Add Variable and Get All Variables in code
Hi,
Currently, no. That's on my wish list.
Currently, no. That's on my wish list.