Accessing Methods on other scripts though Lua

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Timeslip
Posts: 39
Joined: Fri Apr 05, 2019 6:31 am

Accessing Methods on other scripts though Lua

Post by Timeslip »

Hi Tony,

I have a main dialogue class (Dialogue System Helper Methods), which contains the registered LUA functions and references to the classes which contain methods to handle dialogue system interactions for each map (MapA, MapB).

How do i register a function in the main class, to call a function in the map class? Pretty sure I had this working before, but struggling with it. Here's what I've tried:

Code: Select all

Lua.RegisterFunction("AMethod", mapA, SymbolExtensions.GetMethodInfo(() => mapA.AMethod()));
Lua.RegisterFunction("AMethod", mapA, SymbolExtensions.GetMethodInfo(() => AMethod())); 
Lua.RegisterFunction("AMethod", this, SymbolExtensions.GetMethodInfo(() => mapA.AMethod())); 
Thanks!
User avatar
Tony Li
Posts: 22106
Joined: Thu Jul 18, 2013 1:27 pm

Re: Accessing Methods on other scripts though Lua

Post by Tony Li »

The second one is probably closest to what you want. I'm assuming AMethod() is a method in the same script that calls Lua.RegisterFunction(). A few notes:

1. To avoid the potential of typos in strings, use the nameof() function. The Dialogue System carefully uses strings instead of nameof() because it still supports older Unity versions that don't have C# 6.0's nameof() function.

Code: Select all

Lua.RegisterFunction(nameof(AMethod), this, SymbolExtensions.GetMethodInfo(() => AMethod()));
2. If you're putting this script on the Dialogue Manager, use the Awake() method and don't unregister the function in OnDisable():

Code: Select all

void Awake()
{
    Lua.RegisterFunction(nameof(AMethod), this, SymbolExtensions.GetMethodInfo(() => AMethod()));
}
If you're putting the script on a GameObject that only appears in a specific scene and disappears when the scene is unloaded, you can use OnEnable() and OnDisable():

Code: Select all

void OnEnable()
{
    Lua.RegisterFunction(nameof(AMethod), this, SymbolExtensions.GetMethodInfo(() => AMethod()));
}
void OnDisable()
{
    Lua.UnregisterFunction(nameof(AMethod));
}
You register functions with Lua globally. You can't register the same function name (e.g., "AMethod") for MapA and MapB. However, you can register them under different names. Say GameObjects MapA and MapB both have the same script named Map that has a method named Method(). Then you can do this to register MapA.Method as "AMethod" and MapB.Method as "BMethod":

Code: Select all

void OnEnable()
{
    Lua.RegisterFunction(gameObject.name + nameof(Method), this, SymbolExtensions.GetMethodInfo(() => Method()));
}
void OnDisable()
{
    Lua.UnregisterFunction(gameObject.name + nameof(Method));
}
Timeslip
Posts: 39
Joined: Fri Apr 05, 2019 6:31 am

Re: Accessing Methods on other scripts though Lua

Post by Timeslip »

Hi - apologies, I didn't explain the situation very well.

Lua.RegisterFunction() is called in DialogueHelperMethods. Dialogue Helper methods has a reference to MapA, a script attached to the map gameobject, which contains AMethod. Previously, I was setting it up like this:

Code: Select all

public class DialogueSystemHelperMethods : MonoBehaviour
{
	public MapA mapA;
	
	Lua.RegisterFunction("CallAMethod", this, SymbolExtensions.GetMethodInfo(() => CallAMethod()));
	
	public void CallAMethod()
	{
		mapA.AMethod();
	}
}

public class MapA : MonoBehaviour
{
	public void AMethod()
	{
		// perform instructions
	}
}
What I'd like to do is remove CallAMethod() from DialogueSystemHelperMethods, and call AMethod() in MapA directly from Dialogue System Helper Methods.
User avatar
Tony Li
Posts: 22106
Joined: Thu Jul 18, 2013 1:27 pm

Re: Accessing Methods on other scripts though Lua

Post by Tony Li »

Try something like:

Code: Select all

public class DialogueSystemHelperMethods : MonoBehaviour
{
    public MapA mapA;

    void OnEnable()
    {	
	Lua.RegisterFunction(nameof(MapA.AMethod), mapA, SymbolExtensions.GetMethodInfo(() => mapA.AMethod()));
    }
    
    void OnDisable()
    {
        Lua.UnregisterFunction(nameof(MapA.AMethod));
    }
}
Timeslip
Posts: 39
Joined: Fri Apr 05, 2019 6:31 am

Re: Accessing Methods on other scripts though Lua

Post by Timeslip »

Cheers!
Post Reply