Page 2 of 3
Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime
Posted: Thu Aug 20, 2020 5:07 pm
by Sternutation
Okay, that is super-helpful. Will use it this way.
Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime
Posted: Thu Aug 20, 2020 6:29 pm
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.
Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime
Posted: Thu Aug 20, 2020 8:35 pm
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);
Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime
Posted: Mon Aug 24, 2020 1:38 pm
by Sternutation
Hello,
That worked for me! I really appreciate all the help.
Re: [SOLVED]Accessing values of variables from Dialogue Databases that are instantiated at runtime
Posted: Mon Aug 24, 2020 2:06 pm
by Tony Li
Glad to help!
Re: [SOLVED]Accessing values of variables from Dialogue Databases that are instantiated at runtime
Posted: Mon Aug 24, 2020 5:09 pm
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.
Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime
Posted: Mon Aug 24, 2020 5:21 pm
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?
Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime
Posted: Mon Aug 24, 2020 5:22 pm
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)
Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime
Posted: Mon Aug 24, 2020 5:39 pm
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.
Re: Accessing values of variables from Dialogue Databases that are instantiated at runtime
Posted: Mon Aug 24, 2020 7:32 pm
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.