Add Variable and Get All Variables in code

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Deadcow
Posts: 28
Joined: Tue Apr 30, 2019 8:21 am

Add Variable and Get All Variables in code

Post by Deadcow »

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?
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: Add Variable and Get All Variables in code

Post by Tony Li »

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):

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);
To sync variables, add OnConversationStart and OnConversationEnd methods to a script on the Dialogue Manager. Use DialogueLua.SetVariable and GetVariable:

Code: Select all

foreach (var key in knowledgebase)
{
    DialogueLua.SetVariable(key, value);
}
(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)

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;
}
Then use those functions in dialogue entry nodes' Conditions and Script fields:
  • Conditions: GetKBValue("Aliens") == true
  • Dialogue Text: "I see you know about the aliens!"
Deadcow
Posts: 28
Joined: Tue Apr 30, 2019 8:21 am

Re: Add Variable and Get All Variables in code

Post by Deadcow »

Incredible! Just what I wanted
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: Add Variable and Get All Variables in code

Post by Tony Li »

Happy to help! :-)
Deadcow
Posts: 28
Joined: Tue Apr 30, 2019 8:21 am

Re: Add Variable and Get All Variables in code

Post by Deadcow »

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?

2019-05-06_15-40-21.png
2019-05-06_15-40-21.png (19.99 KiB) Viewed 1499 times
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: Add Variable and Get All Variables in code

Post by Tony Li »

Hi,

Currently, no. That's on my wish list.
Post Reply