Accessing Variables from within C# Scripts

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Lentor
Posts: 2
Joined: Wed Mar 28, 2018 9:41 pm

Accessing Variables from within C# Scripts

Post by Lentor »

Really sorry if there is already a post that discusses this but I can't find anything on this and I'm lost for what to do...Can I access my Dialogue Databases through C# code? or do I have to learn Lua and start writing Lua scripts?
User avatar
Tony Li
Posts: 22059
Joined: Thu Jul 18, 2013 1:27 pm

Re: Accessing Variables from within C# Scripts

Post by Tony Li »

Hi,

You can use the DialogueLua C# functions. For example, if you've defined a Number variable "Score" in your dialogue database, you can get its value in a C# script with this code:

Code: Select all

using PixelCrushers.DialogueSystem; // Put at top of script to access Dialogue System stuff.
...
int score = DialogueLua.GetVariable("Score").AsInt;
Lentor
Posts: 2
Joined: Wed Mar 28, 2018 9:41 pm

Re: Accessing Variables from within C# Scripts

Post by Lentor »

Thanks for the quick reply! If you can, can you point me somewhere where I can learn more about getting different kinds of data from the database through C#? I find a lot on how to handle the GUI's and all, but I'm having a really hard time finding anything on the Quest system and the quest system video only seems to scratch the surface.
User avatar
Tony Li
Posts: 22059
Joined: Thu Jul 18, 2013 1:27 pm

Re: Accessing Variables from within C# Scripts

Post by Tony Li »

Hi,

Here are the API reference pages:

- Quest management: QuestLog
- Database actors, items, and variables: DialogueLua

Here are some commonly-used methods in QuestLog:

Code: Select all

QuestState state = QuestLog.GetQuestState("My Quest");
QuestLog.SetQuestState("My Quest", QuestState.Active);

int numEntries = QuestLog.GetQuestEntryCount("My Quest");
QuestState state = QuestLog.GetQuestEntryState("My Quest", 1);
QuestLog.SetQuestEntryState("My Quest", 1, QuestState.Success);

string[] QuestLog.GetAllQuests(QuestState.Success | QuestState.Failure);
Here are some commonly-used methods in DialogueLua:

Code: Select all

bool DialogueLua.GetVariable("My Variable").AsBool;
DialogueLua.SetVariable("My Variable", true);

int age = DialogueLua.GetActorField("Player", "Age").AsInt;
DialogueLua.SetActorField("NPC 1", "Age", 21);
At runtime, dialogue database information that can change (such as variable values) are put into Lua. You can get other information, such as the Dialogue Text of dialogue entry nodes, by accessing DialogueManager.MasterDabase. (The DialogueManager class itself has a lot of useful functions.) Here's an example of logging all dialogue texts:

Code: Select all

foreach (var conversation in DialogueManager.MasterDatabase.conversations) {
    Debug.Log("Conversation: " + conversation.Title);
    foreach (var dialogueEntry in conversation.dialogueEntries) {
        Debug.Log("Dialogue Text: " + dialogueEntry.DialogueText);
    }
}
Post Reply