using C# with lua
Posted: Wed Jun 17, 2020 4:47 pm
Hi, I having a problem calling c# functions registered in Lua, maybe you can help me? Thanks a lot! (probably it is a noob problem, but I stood several hour testing this and reading the documentation and cannot find the answer)
I created some c# functions and added/registered in Lua. They work well when they don't receive parameters.
I can also use them in the text to dynamically show information.
An example of one function that works is like this:
c# code
// returns a string with your last ending
public string UltimoFinal()
{
return ThisRun.Ultimofinal;
}
and somewhere in my code I register it this way:
Lua.RegisterFunction("UltimoFinal", this, SymbolExtensions.GetMethodInfo(() => UltimoFinal()));
Then in the text i call it this way in the dialogue text:
[lua(UltimoFinal())]
and it fills it OK with the rest of the writing
Now I want to do the same with a function that returns another string, but receives a string parameter also, lets say I want to fill the name of a character this way
c# code
//returns the name of the player
public string Nombre(string Id)
{
return Personaje.Nombre;
}
and register it in the same way and place of the other one in this way, as I saw in the documentation:
Lua.RegisterFunction("Nombre", this, SymbolExtensions.GetMethodInfo(() => Nombre(string.Empty)));
Then I call it in the dialoguetext in this way:
[lua(Nombre("Yo"))]
I also tried different formats like [lua(Nombre["Yo"])] or [lua(Nombre(Yo))] etc without good results
In the end I receive a runtime error, and it is not working at all
Dialogue System: Lua code 'return Nombre("Yo")' threw exception 'Exception has been thrown by the target of an invocation.'
UnityEngine.Debug:LogError(Object)
PixelCrushers.DialogueSystem.Lua:RunRaw(String, Boolean, Boolean) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:226)
PixelCrushers.DialogueSystem.Lua:Run(String, Boolean, Boolean) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:127)
PixelCrushers.DialogueSystem.Lua:Run(String, Boolean) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:137)
PixelCrushers.DialogueSystem.<>c:<ReplaceLuaTags>b__58_0(Match) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Text/FormattedText.cs:262)
System.Text.RegularExpressions.Regex:Replace(String, MatchEvaluator)
PixelCrushers.DialogueSystem.FormattedText:ReplaceLuaTags(String&) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Text/FormattedText.cs:256)
PixelCrushers.DialogueSystem.FormattedText:Parse(String, EmphasisSetting[]) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Text/FormattedText.cs:181)
PixelCrushers.DialogueSystem.ConversationModel:GetState(DialogueEntry, Boolean, Boolean, Boolean) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Model/ConversationModel.cs:244)
PixelCrushers.DialogueSystem.ConversationModel:GetState(DialogueEntry) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Model/ConversationModel.cs:303)
PixelCrushers.DialogueSystem.ConversationController:OnFinishedSubtitle(Object, EventArgs) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Controller/ConversationController.cs:264)
PixelCrushers.DialogueSystem.ConversationView:FinishSubtitle() (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/View/View/ConversationView.cs:397)
PixelCrushers.DialogueSystem.ConversationView:OnFinishedSubtitle() (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/View/View/ConversationView.cs:404)
PixelCrushers.DialogueSystem.Sequencer:FinishSequence() (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Sequencer/Sequencer.cs:502)
PixelCrushers.DialogueSystem.Sequencer:Update() (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Sequencer/Sequencer.cs:495)
I created some c# functions and added/registered in Lua. They work well when they don't receive parameters.
I can also use them in the text to dynamically show information.
An example of one function that works is like this:
c# code
// returns a string with your last ending
public string UltimoFinal()
{
return ThisRun.Ultimofinal;
}
and somewhere in my code I register it this way:
Lua.RegisterFunction("UltimoFinal", this, SymbolExtensions.GetMethodInfo(() => UltimoFinal()));
Then in the text i call it this way in the dialogue text:
[lua(UltimoFinal())]
and it fills it OK with the rest of the writing
Now I want to do the same with a function that returns another string, but receives a string parameter also, lets say I want to fill the name of a character this way
c# code
//returns the name of the player
public string Nombre(string Id)
{
return Personaje.Nombre;
}
and register it in the same way and place of the other one in this way, as I saw in the documentation:
Lua.RegisterFunction("Nombre", this, SymbolExtensions.GetMethodInfo(() => Nombre(string.Empty)));
Then I call it in the dialoguetext in this way:
[lua(Nombre("Yo"))]
I also tried different formats like [lua(Nombre["Yo"])] or [lua(Nombre(Yo))] etc without good results
In the end I receive a runtime error, and it is not working at all
Dialogue System: Lua code 'return Nombre("Yo")' threw exception 'Exception has been thrown by the target of an invocation.'
UnityEngine.Debug:LogError(Object)
PixelCrushers.DialogueSystem.Lua:RunRaw(String, Boolean, Boolean) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:226)
PixelCrushers.DialogueSystem.Lua:Run(String, Boolean, Boolean) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:127)
PixelCrushers.DialogueSystem.Lua:Run(String, Boolean) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/Lua/Lua Wrapper/Lua Interpreter/Lua.cs:137)
PixelCrushers.DialogueSystem.<>c:<ReplaceLuaTags>b__58_0(Match) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Text/FormattedText.cs:262)
System.Text.RegularExpressions.Regex:Replace(String, MatchEvaluator)
PixelCrushers.DialogueSystem.FormattedText:ReplaceLuaTags(String&) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Text/FormattedText.cs:256)
PixelCrushers.DialogueSystem.FormattedText:Parse(String, EmphasisSetting[]) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Text/FormattedText.cs:181)
PixelCrushers.DialogueSystem.ConversationModel:GetState(DialogueEntry, Boolean, Boolean, Boolean) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Model/ConversationModel.cs:244)
PixelCrushers.DialogueSystem.ConversationModel:GetState(DialogueEntry) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Logic/Model/ConversationModel.cs:303)
PixelCrushers.DialogueSystem.ConversationController:OnFinishedSubtitle(Object, EventArgs) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Controller/ConversationController.cs:264)
PixelCrushers.DialogueSystem.ConversationView:FinishSubtitle() (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/View/View/ConversationView.cs:397)
PixelCrushers.DialogueSystem.ConversationView:OnFinishedSubtitle() (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/View/View/ConversationView.cs:404)
PixelCrushers.DialogueSystem.Sequencer:FinishSequence() (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Sequencer/Sequencer.cs:502)
PixelCrushers.DialogueSystem.Sequencer:Update() (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Sequencer/Sequencer.cs:495)