how to query C# values from dialogue script?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Arcanor
Posts: 81
Joined: Sun Oct 05, 2014 4:20 am

how to query C# values from dialogue script?

Post by Arcanor »

After looking over the documentation, I see that there are several ways to monitor lua variables from C#, but not vice-versa. I realize there are also Sequences, but as far as I can see those can only call methods which may set values, but cannot receive any return value.

Is there any way for a dialogue text (maybe using [lua()] tags?), or associated lua script/condition to receive a return value or variable that I keep in a C# class on an object in my game scene?

I'd prefer to keep all my data in one place if possible, and I strongly prefer C# as that place. Frankly, I feel that lua is inferior due to:
  • primitive types
  • lack of type enforcement
  • lack of intellisense (easy to misspell a variable name)
  • lack of encapsulation
  • potential performance issues
  • etc.
For example, I have a custom class called PlayerData attached to my player object in the scene. Is there a reasonable way to get access to those variables and methods from within lua? Is this a hopeless wish?
Arcanoria Games - http://www.arcanoria.com - indie game development blog
twitter: https://twitter.com/WaltCollins3 @WaltCollins3
Game titles: Pesticide Patrol, Arcanoria Chronicles, CyberGhost, Arcanoria
PLAY NOW: http://arcanor.itch.io/
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: how to query C# values from dialogue script?

Post by Tony Li »

Hi Walt,

You can register your own C# methods with Lua. That's what the Third Party Support packages do to give you in-conversation access to UFPS's ammo count, RPG Kit's hit points, etc. Here's an example:

Code: Select all

public class PlayerData : MonoBehaviour {
    public int hp; // Player's hit points.
    
    void Start() {
        // Register Lua functions:
        Lua.RegisterFunction("GetHP", this, SymbolExtensions.GetMethodInfo(() => GetHP()));
        Lua.RegisterFunction("SetHP", this, SymbolExtensions.GetMethodInfo(() => SetHP((double) 0)));
    }
    
    public double GetHP() {
        return hp;
    }
    
    public void SetHP(double newHP) {
        hp = newHP;
    }
}
The only thing to watch out for is that the default Lua implementation, LuaInterpreter, treats all numbers as doubles, so just use doubles with your functions.

In a conversation, you can use these functions in your dialogue entries' Conditions and Script fields. For example:
  • Dialogue Text: "You're hurt! Do you need healing?"
  • Conditions: GetHP() < 100
and
  • Dialogue Text: "Be healed!"
  • Script: SetHP(100)
  • Sequence: AudioWait(magicSound)
Arcanor
Posts: 81
Joined: Sun Oct 05, 2014 4:20 am

Re: how to query C# values from dialogue script?

Post by Arcanor »

Thanks Tony. I'll look into that more thoroughly!
Arcanoria Games - http://www.arcanoria.com - indie game development blog
twitter: https://twitter.com/WaltCollins3 @WaltCollins3
Game titles: Pesticide Patrol, Arcanoria Chronicles, CyberGhost, Arcanoria
PLAY NOW: http://arcanor.itch.io/
Post Reply