godot-docs icon indicating copy to clipboard operation
godot-docs copied to clipboard

C#: A "From GDScript to C# gotchas" guide

Open RiverMesa opened this issue 8 years ago • 7 comments

It would be worthwhile to have, alongside the general "getting started" guide (perhaps as another section thereof?), a guide that outlines specifically the things to watch out for when using C#, coming over from GDScript - namely, the API naming conventions (from snake_case to PascalCase), and how to achieve certain GDScript features that don't have a direct C# equivalent - for instance, using the [Export] attribute in lieu of the export GDScript keyword, how to make tool mode scripts*, and other such gotchas.

* - once that becomes available, since right now it isn't IIRC, which would be also have to be mentioned (for the time being)

RiverMesa avatar Jan 05 '18 08:01 RiverMesa

All of those are now contained in https://github.com/godotengine/godot-docs/pull/934. Anything else you want me to add? :)

mhilbrunner avatar Jan 05 '18 19:01 mhilbrunner

There are a few cool things missing there. I must stop one of this days and make a list.

One that comes to my mind right now is the NativeInstance property. Like in GDScript, you cannot store a non-null Godot.Object and later check if it was deleted with a null comparison. You must use a weakref instead. However, in C# you have NativeInstance which is automatically updated to IntPtr.Zero when the native instance is freed. This is not only a bit less annoying because you can just store the Object/Node itself, but it's also faster because you don't have to resolve the instance for the ObjectID that weakref stores internally.

This is how you do it in GDScript:

extends Node

var my_node_ref

func _ready():
    my_node_ref = weakref(get_node("MyNode"))

func _this_is_called_later():
    var my_node = my_node_ref.get_ref()
    if my_node:
        my_node.do_something()

C# with weakref:

using Godot;

public class Whatever : Node
{
    WeakRef myNodeRef;

    public override void _Ready()
    {
        myNodeRef = GD.Weakref(GetNode("MyNode"));
    }

    void _ThisIsCalledLater()
    {
        myNodeRef.GetRef()?.DoSomething();
    }
}

C# with NativeInstance:

using Godot;

public class Whatever : Node
{
    MyNode myNode;

    public override void _Ready()
    {
        myNode = (MyNode)GetNode("MyNode");
    }

    void _ThisIsCalledLater()
    {
        if (myNode?.NativeInstance != IntPtr.Zero)
            myNode.DoSomething();
    }
}

I want to add an InstanceAlive property so you can type myNode?.InstanceAlive ?? false instead (or myNode?.InstanceAlive == true). Maybe a method that returns this if alive otherwise null, so you can do something like myNode?.Self?.DoSomething(). Not sure if that would be a bad practice :smile:

neikeq avatar Jan 05 '18 20:01 neikeq

@neikeq Cool! Thanks, I'll use that in https://github.com/godotengine/godot-docs/issues/580, weakref needs to be documented anyway :)

mhilbrunner avatar Jan 05 '18 20:01 mhilbrunner

I would +1 this issue and would love to see a full guide on how to interop between C# and GDScript back and forth

NovemberDev avatar Jan 25 '20 15:01 NovemberDev

@NovemberDev Please don't bump issues without contributing significant new information; use the :+1: reaction button on the first post instead.

Calinou avatar Jan 25 '20 15:01 Calinou

Is this considered covered by Cross-language scripting?

Calinou avatar Jun 15 '20 08:06 Calinou

Closing as there's been no activity for some time. Feel free to open a new issue if you feel the need is still there.

cbscribe avatar Feb 21 '22 22:02 cbscribe