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

Make C# static methods accessible. #81783

Merged
merged 1 commit into from
Sep 20, 2023

Conversation

zaevi
Copy link
Contributor

@zaevi zaevi commented Sep 17, 2023

Close #79896.

This PR adds a code generator to generate InvokeGodotClassStaticMethod and allows GDScript to access C# static methods via CSharpScript::callp -> ScriptManagerBridge.CallStatic -> YourClass.InvokeGodotClassStaticMethod -> YourClass.YourStaticMethod.

Example:

// res://CSharpScripts/Test.cs
public partial class Test : Node
{
    public static void StaticSayHello() => GD.Print("Hello from C#!");

    public static int StaticAdd(int a, int b) => a + b;

    public static int StaticAdd(int a, int b, int c) => a + b + c;
}

// res://CSharpScripts/Parent.cs
public partial class Parent : Node
{
    public static void ParentStaticMethod() => GD.Print("ParentStaticMethod");

    public static void StaticMethod() => GD.Print("StaticMethod from Parent");
}

// res://CSharpScripts/Child.cs
public partial class Child : Parent
{
    public static void ChildStaticMethod() => GD.Print("ChildStaticMethod");

    public new static void StaticMethod() => GD.Print("StaticMethod from Child");
}
@tool
extends EditorScript

func _run():
	var Test = load("res://CSharpScripts/Test.cs") as Script

	Test.StaticSayHello() # Hello from C#!
	print(Test.StaticAdd(1,2)) # 3
	print(Test.StaticAdd(1,2,3)) # 6
	print(Test.new().StaticAdd(4,5,6)) # 15

	var Parent = load("res://CSharpScripts/Parent.cs") as Script
	var Child = load("res://CSharpScripts/Child.cs") as Script

	Parent.StaticMethod(); # StaticMethod from Parent
	Child.StaticMethod(); # StaticMethod from Child

	Parent.ParentStaticMethod(); # ParentStaticMethod
	Child.ParentStaticMethod(); # ParentStaticMethod
	Child.ChildStaticMethod(); # ChildStaticMethod

csharp_static_method_test.zip

Copy link
Member

@raulsntos raulsntos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've tested with the provided project and works as expected. Just a few comments about the implementation.

Copy link
Member

@raulsntos raulsntos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Member

@AThousandShips AThousandShips left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save for this, the style looks good

@akien-mga akien-mga modified the milestones: 4.x, 4.2 Sep 20, 2023
@akien-mga akien-mga merged commit ce0fa4c into godotengine:master Sep 20, 2023
15 checks passed
@akien-mga
Copy link
Member

Thanks!

@akien-mga akien-mga changed the title C#: make C# static methods accessible. Make C# static methods accessible. Sep 20, 2023
@Hanprogramer
Copy link

Hanprogramer commented Mar 26, 2024

This doesn't work in tools scripts. Both GDScript and C# using tool attribute. Same error: Parse Error: Static function "CreateNewDB()" not found in base "CSharpScript".

C#:

using Godot;

[Tool]
[GlobalClass]
public partial class DBHAutoload : GodotObject
{
    public static void CreateNewDB(string path, string dbName, string namespacename, bool gen_class, string class_name, bool add_default_fields)
    {
        var content = ".. insert content here ...";
        var file = FileAccess.Open(path, FileAccess.ModeFlags.Write);
        file.StoreString(content);
        file.Close();
    }
}

GD:

@tool
extends Control

@export var create_dialog : Window

func _on_dbh_create_dialog_create(path, dbname, namespacename, gen_class, classname, add_defaultfields):
	print("Creating in: "  + path)
	DBHAutoload.CreateNewDB()#.CreateNewDB(path,dbname,namespacename,gen_class,classname, add_defaultfields)

@Zireael07
Copy link
Contributor

Make a separate issue, the PR was closed months ago

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CSharp static methods not visible/accessible
6 participants