WeaselGames / godot_luaAPI

Godot LuaAPI
https://luaapi.weaselgames.info
Other
361 stars 27 forks source link

Feature Request: Actually support C# like the docs says that it does. #164

Open michieal opened 10 months ago

michieal commented 10 months ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Issue: This addon says that you can use it with C#... Yet, I cannot find any way to do that. And, it's frustrating that there are Zero examples of even trying to use this with C#. Additionally, there's no support in Rider... no includable namespace that would allow for basic things (classes, etc.) to be seen / searched through.

Describe the solution you'd like A clear and concise description of what you want to happen.

I'd like to see a valid example done in C#. I say this, because I genuinely don't think that the support is actually there. I mean, even in Godot, if you build it, it errors out. So, it's not just hidden from the IDE... it's hidden from MSBUILD too.

I'm okay with not having full descriptions in the IDE. But, It creating errors, and not having anything on how to even start with RE to C# (Though "Supported") ... just makes for a very frustrating experience all-around. (I really want to use this add-in, as I need the functionality, and I want to make modding a major aspect of my game.)

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Haven't got that far.

Additional context Add any other context or screenshots about the feature request here.

Made a basic godot 4.1.1 - stable-mono project. downloaded the LuaAPI from the asset library. Looked in the WIki docs as to what things do, made a cs script on a base node, tried to add a reference variable of type LuaAPI. watched as things turned red on screen, and the project refused to build. Looked for any c# examples in the documentation, and found none. My guess, is that the C/C++ code, doesn't have the wrappers built for the C# code to use.

Trey2k commented 10 months ago

Hello, and thanks for the feedback.

Issue: This addon says that you can use it with C#... Yet, I cannot find any way to do that. And, it's frustrating that there are Zero examples of even trying to use this with C#. Additionally, there's no support in Rider... no includable namespace that would allow for basic things (classes, etc.) to be seen / searched through.

Your assessment is correct that we do claim you can use this add-on with C#, C# in general has a lot of issues currently with Godot. The current mono system is pretty much the same system from 3.x and has a lot of known issues.

I currently do not provide tutorials as I do not want to give off the impression that it works just as well as GDScript.

The issue you are running into is the old mono module does not have access to GDExtension classes. A work around is to create a wrapper in GDScript to the GDExtension classes. But that's not a great solution.

Until the new GDExtension version of c# support is here we offer mono module builds on our releases (https://github.com/WeaselGames/godot_luaAPI/releases/download/v2.1-beta6/windows-editor-mono.zip). This version has the add-on built as a core Godot engine module along side c# which it can access. However I would not expect this to be bug free. That being said if an issue is found I can fix I will.

Trey2k commented 10 months ago

Here is a good summery of where c# support currently is in 4.x and where it is going https://gamefromscratch.com/the-future-of-c-in-godot/

michieal commented 10 months ago

I see the issue. Hmmn. Is there a Linux Editor Mono.zip? (I'm running on Ubuntu, as there's a few bugs in Windows that tries to destroy my files, and Linux doesn't have those bugs.)

Trey2k commented 10 months ago

I see the issue. Hmmn. Is there a Linux Editor Mono.zip? (I'm running on Ubuntu, as there's a few bugs in Windows that tries to destroy my files, and Linux doesn't have those bugs.)

Yes there is https://github.com/WeaselGames/godot_luaAPI/releases/download/v2.1-beta6/linux-editor-mono.zip

Also if you would like to get c# documentation started, now that we do have the mono builds with suffcient disclaimers i think it should be fine. Feel free to contribute to the in engine docs, or the docs site https://github.com/WeaselGames/godot_luaAPI_docs

michieal commented 10 months ago

Here is a good summery of where c# support currently is in 4.x and where it is going https://gamefromscratch.com/the-future-of-c-in-godot/

Reading this now. Thank you, btw, for the super quick reply.

michieal commented 10 months ago

I see the issue. Hmmn. Is there a Linux Editor Mono.zip? (I'm running on Ubuntu, as there's a few bugs in Windows that tries to destroy my files, and Linux doesn't have those bugs.)

Yes there is https://github.com/WeaselGames/godot_luaAPI/releases/download/v2.1-beta6/linux-editor-mono.zip

Does this need the GodotSharp/tools and GodotSharp/api folders? I ask, because when I just tried to run it, it said that it couldn't find the installation of DotNet on the system. Yet, the regular version of Godot Linux stable mono 4.1.1 works just fine. When I looked into the GodotSharp sub dir, it only has Generated... whereas the other has API and Tools... The editor, after displaying the error message, allows you to go to the projects, and when I opened my project, it displayed the splash screen, then the error message again. And, when I hit okay, thinking that I could point to the location inside... it just closes.

Also if you would like to get c# documentation started, now that we do have the mono builds with sufficient disclaimers i think it should be fine. Feel free to contribute to the in engine docs, or the docs site https://github.com/WeaselGames/godot_luaAPI_docs

I can do that, assuming that I can get things to work.

Trey2k commented 10 months ago

Looks like a step was missed for glue generation, im going to quickly generated it on my end and update the builds

michieal commented 10 months ago

Thank you!

Trey2k commented 10 months ago

Okay, builds have been updated https://github.com/WeaselGames/godot_luaAPI/releases/tag/v2.1-beta6

michieal commented 10 months ago

Thank you, trying it out now!

michieal commented 10 months ago

So, I took what you you gave me, and I rewrote the main example in the Readme to be C#. It gives no errors nor warnings. But it also doesn't do anything. It should, but it looks like when you run it, it's doing a dotnet restore and is using the default nuget packages, rather than the ones that I downloaded and defined. That's a big issue.

using Godot;

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

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

    public void _Ready() {
        GD.Print("Starting Node2D.cs.");

        // All builtin libraries are available to bind with. Use OS and IO at your own risk.
        // BindLibraries requires a "Godot Array" so, let's build one.
        Godot.Collections.Array libraries = new Godot.Collections.Array();
        libraries.Add("base");   // Base Lua commands
        libraries.Add("table");  // Table functionality.
        libraries.Add("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("cs_print", print);
        // Assign a Lua Variable named "message" and give it a value.
        lua.PushVariant("message", "Hello lua!");

        // Use .DoString() to execute our Lua code.
        LuaError error = lua.DoString("cs_print(message)");
        // Check for errors, and if there are any, Print them to the Godot Console.
        if (error != null && error.Message != "") {
            GD.Print("An error occurred calling DoString.");
            GD.Print("ERROR %d: %s", error.Type, error.Message);
        }

        error = lua.DoString(@"
                                  for i=1,10,1 do
                                    cs_print(message)
                                  end
                                  function get_message()
                                    return ""This message was sent from 'get_message()'""
                                  end
                                  ");

        // Check for errors, and if there are any, Print them to the Godot Console.
        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");

        // Check to see if it returned an error, or a value.
        if (val.GetType() == typeof(LuaError)) {
            GD.Print("ERROR %d: %s", error.Type, error.Message);
            return;
        }

        // LuaAPI.CallFunction requires a Godot.Collections.Array as the container
        // for the parameters passed in, for the lua function. 
        Godot.Collections.Array Params = new Godot.Collections.Array();

        // We use .CallFunction to actually call the lua function within the Lua State.
        var message = lua.CallFunction("get_message", Params);
        // And, finally, we log the output of the function to Godot Output Console.
        GD.Print(message);
    }
}

Can you try putting this script (Lua.cs) onto a Node2D and run it?

Also, did you have a chance to look into the missing Linux Debug template?

For the second example, I would like to define a class (with functions) and define that as a bound library. I figure that the main aspect of what people are going to want to do with this (much like what I want to do with this) is make an API for their lua constructs to use.

Trey2k commented 10 months ago

Can you try putting this script (Lua.cs) onto a Node2D and run it?

public void _Ready() must be changed to public override void _Ready() and it runs correctly for me.

Also, did you have a chance to look into the missing Linux Debug template?

Not yet, but I will be getting to those soon. These are a little lower priority for me as some missing debug templates should not cause many issues atm. You can still debug with the editor. Only thing missing will be export debugging. If I dont get to it by tomorrow I will this weekend for sure.

michieal commented 10 months ago

Can you try putting this script (Lua.cs) onto a Node2D and run it?

public void _Ready() must be changed to public override void _Ready() and it runs correctly for me.

🤦🏼‍♂️ doh! Well, I will have to change that. lmao.

Also, did you have a chance to look into the missing Linux Debug template?

Not yet, but I will be getting to those soon. These are a little lower priority for me as some missing debug templates should not cause many issues atm. You can still debug with the editor. Only thing missing will be export debugging. If I dont get to it by tomorrow I will this weekend for sure.

Okay, cool! Also, I do not mind helping out, as this fits into my mental schedule. Besides... In doing this, I am getting the examples that I wanted. lmao.

michieal commented 10 months ago

Yep. That fixed it. Thank you.

Trey2k commented 10 months ago

Also, I do not mind helping out, as this fits into my mental schedule. Besides... In doing this, I am getting the examples that I wanted. lmao.

Help would be more then welcome. If you need help getting up to speed with anything in the build proc feel free to join our discord too.