using C# with lua

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
jeuazarru
Posts: 10
Joined: Mon Jun 15, 2020 6:42 pm

using C# with lua

Post by jeuazarru »

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)
User avatar
Tony Li
Posts: 21980
Joined: Thu Jul 18, 2013 1:27 pm

Re: using C# with lua

Post by Tony Li »

Hi,

This message is not very clear, but it means your C# method had an error:

Code: Select all

Dialogue System: Lua code 'return Nombre("Yo")' threw exception 'Exception has been thrown by the target of an invocation.'
Try changing your method to this:

Code: Select all

public string Nombre(string Id)
{
    try
    {
        return Personaje.Nombre;
    }
    catch (System.Exception e)
    {
        Debug.LogException(e);
    }
}
This will log the actual error that your method had. It's probably a NullReferenceException. Are you sure Personaje is assigned?
jeuazarru
Posts: 10
Joined: Mon Jun 15, 2020 6:42 pm

Re: using C# with lua

Post by jeuazarru »

Thank you Tony! When looking back the code again I found a error in the C# script. Now it is working!

I changed so many times the scripts because of a previous error, that in the end solved the original problem but introduced a new one :roll:

I have other questions about nested conversations, but will bother you tomorrow with that if I am not able to solve myself.
Post Reply