Get all variables that contain "specific_string"

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
lemonjolly_anna
Posts: 18
Joined: Tue May 16, 2023 11:41 pm

Get all variables that contain "specific_string"

Post by lemonjolly_anna »

Hello!

Is there a way to DialogueLua.GetVariable any variables that contain "specific_string"?

Use Case:
  • have int variables named "visits.thisrun.etc"
  • resets on game loop

Cheers!
User avatar
Tony Li
Posts: 23256
Joined: Thu Jul 18, 2013 1:27 pm

Re: Get all variables that contain "specific_string"

Post by Tony Li »

Hi,

It's easiest if your variables are defined in your dialogue database (i.e., at design time, not newly-added at runtime). Loop through the variables list:

Code: Select all

foreach (var variable in DialogueManager.masterDatabase.variables)
{
    if (variable.Name.Contains("visits.thisrun"))
    {
        Debug.Log($"Found {variable.Name}: {DialogueLua.GetVariable(variable.Name).asInt}");
    }
}
If the variables are created at runtime, you'll need to loop through the Lua table:

Code: Select all

var variables = Lua.Run("return Variable").asTable;
foreach (var variableName in variables.Keys)
{
    if (variableName.Contains("visits.thisrun")) 
    {
        Debug.Log($"Found {variableName}: {DialogueLua.GetVariable(variableName).asInt}");
    }
}
Post Reply