Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LuaFuntionRef type #172

Merged
merged 4 commits into from Oct 9, 2023
Merged

Add LuaFuntionRef type #172

merged 4 commits into from Oct 9, 2023

Conversation

Trey2k
Copy link
Member

@Trey2k Trey2k commented Oct 9, 2023

Since C# does not currently support CallableCustoms this PR adds a setting to the LuaAPI class, luaAPI.use_callables. It defaults to true. When set to true Lua functions pulled from the Lua state will use the LuaCallable type which is a CallableCustom. For c# or even gdScript to get around #155 it can be set to false to use the LuaFunctionRef type instead which is a RefCounted. It behaves identical to the LuaCallable type but uses an invoke method instead.

This is what the README example would look like for c# now

using Godot;

using System;

public partial class Node2D : Godot.Node2D
{
	private LuaApi lua = new LuaApi();

	public void LuaPrint(string message) {
		GD.Print(message);
	}

	public override void _Ready() {
		lua.UseCallables = false;
		Godot.Collections.Array libraries = new()
        {
            "base",   // Base Lua commands
            "table",  // Table functionality.
            "string" // String Specific functionality.
        };

		lua.BindLibraries(libraries); // Assign the specified libraries to the LuaAPI object.

		// In C#, .PushVariant does not work with Methods, so we use Callable to wrap our function.
		Callable print = new Callable(this, MethodName.LuaPrint);
		// Assign the Callable, so that the API can call our function.
		// Note, the lua function "cs_print" is now callable within Lua script.
		lua.PushVariant("print", print);
		// Assign a Lua Variable named "message" and give it a value.
		lua.PushVariant("message", "Hello lua!");

		LuaError error = lua.DoString(@"
									for i=1,10,1 do
										print(message)
									end
									function get_message()
										return ""Hello gdScript!""
									end
                                  ");

		if (error != null && error.Message != "") {
			GD.Print("An error occurred calling DoString.");
			GD.Print("ERROR %d: %s", error.Type, error.Message);
		}

		var val = lua.PullVariant("get_message");
		if (val.GetType() == typeof(LuaError)) {
			GD.Print("ERROR %d: %s", error.Type, error.Message);
			return;
		}

		LuaFunctionRef get_message = val.As<LuaFunctionRef>();
		if (get_message == null) {
			GD.Print("ERROR: get_message is null.");
			return;
		}

		Godot.Collections.Array Params = new();
		GD.Print(get_message.Invoke(Params));
	}
}

@Trey2k Trey2k merged commit 954e452 into main Oct 9, 2023
21 checks passed
@Trey2k Trey2k deleted the dev/functionRef branch October 9, 2023 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant