Accessing values of variables from Dialogue Databases that are instantiated at runtime

Announcements, support questions, and discussion for the Dialogue System.
Sternutation
Posts: 22
Joined: Wed Aug 19, 2020 2:44 pm

Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime

Post by Sternutation »

Okay, that is super-helpful. Will use it this way.
Sternutation
Posts: 22
Joined: Wed Aug 19, 2020 2:44 pm

Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime

Post by Sternutation »

Code: Select all

        string luaScript = @"Actor['{ActorName}'] = \{ Name = '{ActorName}', Display_Name = '{ActorDisplayName}'\}";
        Lua.Run(luaScript);
        dialogueActor = gameObject.AddComponent<DialogueActor>();
        dialogueActor.actor = ActorName;
This is what I tried using, but I am getting syntax errors. Using the C# 6.0 version of string interpolation(as it was in your example), I get an error indicating unrecognised escape sequence wherever \ is used.

If I replace the $sign with the @ as shown in the above snippet, which at least allows me to get into playmode, I get a syntax error, indicating that the string has not been parsed successfully.

If I remove the @ and / entirely, I get a stack overflow error.


As someone unfamiliar with Lua(both Lua itself and the Dialogue System's Lua class), I am not entirely sure about how it parses a C# string, especially since the C# 6.0 version of string interpolation no longer works.

Apologies if it seems like I am asking for spoonfeeding.
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime

Post by Tony Li »

Hi,

String interpolation of the form $"...{variable}..." is only available in newer versions of Unity that are using the C# 6.0 standard or higher. Try String.Format() or regular string concatenation:

Code: Select all

string luaScript = "Actor['" + ActorName + "'] = { Name = '" + ActorName + "', Display_Name = '" + ActorDisplayName + "'}";
Lua.Run(luaScript);
Sternutation
Posts: 22
Joined: Wed Aug 19, 2020 2:44 pm

Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime

Post by Sternutation »

Hello,

That worked for me! I really appreciate all the help.
Sternutation
Posts: 22
Joined: Wed Aug 19, 2020 2:44 pm

Re: [SOLVED]Accessing values of variables from Dialogue Databases that are instantiated at runtime

Post by Sternutation »

I may have spoken too soon, I see that I am getting a stack overflow error.

Code: Select all

Lua code 'Actor['Bob'] = { Name = 'Bob', Display_Name = 'BobTheCapsule'}' threw exception 'The requested operation caused a stack overflow.'
Where 'Bob' is derived from a parameter called ActorName and 'BobTheCapsule' is derived from a parameter called ActorDisplayName. For testing purposes I had put these as serialized variables.

Lol I end up returning to this thread again and again, thinking that I have it solved, only to see that it is not the case. The weirdest thing is that I could have sworn that BobTheCapsule was working when I implemented the solution on Friday.
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime

Post by Tony Li »

Something else is going on. That line by itself runs fine in Lua.

Can you post the C# code that you're using to run that?
Sternutation
Posts: 22
Joined: Wed Aug 19, 2020 2:44 pm

Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime

Post by Sternutation »

Sure, I am posting it below.

Code: Select all

    public string ActorName;
    //public string ActorDisplayName;
    private DialogueActor dialogueActor;
    void Awake()
    {
        string luaScript = "Actor['" + ActorName + "'] = { Name = '" + ActorName + "', Display_Name = '" + gameObject.name + "'}";
        Lua.Run(luaScript);
        dialogueActor = gameObject.AddComponent<DialogueActor>();
        dialogueActor.actor = ActorName;
    }
Tested it with both ActorDisplayName and gameObject.name(latter used only because of saving the gameObject as the same name as what we want to display on screen)
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime

Post by Tony Li »

If this happens in the same scene that the Dialogue Manager first initializes itself in, change your Awake method to Start. This will allow the Dialogue Manager to initialize itself first.

I tested this script, and it works:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class CreateRuntimeActor : MonoBehaviour
{
    public string ActorName;

    private void Start()
    {
        string luaScript = "Actor['" + ActorName + "'] = { Name = '" + ActorName + "', Display_Name = '" + gameObject.name + "'}";
        Lua.Run(luaScript);
    }
}
If that doesn't help, please feel free to send a reproduction project to tony (at) pixelcrushers.com.
Sternutation
Posts: 22
Joined: Wed Aug 19, 2020 2:44 pm

Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime

Post by Sternutation »

Okay, I shall try putting it in Start. I was testing it out in a SampleScene so the DialogueManager does initialize in this scene as well for this particular context.
Post Reply