GodotSharpExtras 0.3.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package GodotSharpExtras --version 0.3.6                
NuGet\Install-Package GodotSharpExtras -Version 0.3.6                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GodotSharpExtras" Version="0.3.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GodotSharpExtras --version 0.3.6                
#r "nuget: GodotSharpExtras, 0.3.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GodotSharpExtras as a Cake Addin
#addin nuget:?package=GodotSharpExtras&version=0.3.6

// Install GodotSharpExtras as a Cake Tool
#tool nuget:?package=GodotSharpExtras&version=0.3.6                

GodotSharpExtras

C# Extras for Godot, provides tools that are not currently implemented by the Core GodotSharp library. One being the onready keyword. Instead, this is replaced with using two C# attributes, called NodePath() and ResolveNode() attributes, that allows easily to reference a variable to their proper node in the Scene / SceneTree.

Installation

To install this library, simply use dotnet command line tool, to add a package reference for GodotSharpExtras, using the following command:

dotnet add package GodotSharpExtras

Compilation

In order to compile a custom version of this project, you will need the GodotSharp libraries from an existing Project which can be found under .mono folder. In order to make it easier to build, simply copy the .mono folder from one of your projects, directly into the root of this folder, and then run the following command:

dotnet build

This will compile the DLL file, as well as create a Nuget package, that you can install locally, or you can simply copy the DLL file over to your project, under any folder you want, and add the following to your project's csproj file.

<ItemGroup>
    <Reference Include="GodotSharpExtras">
        <HintPath>Path\To\GodotSharpExtras.dll</HintPath>
    </Reference>
</ItemGroup>

OnReady()

This function is what wires everything up from the Godot side of things, to the C# Side of things. In order for C# to be able to get the nodes it needs to reference with the proper C# Variables, OnReady() needs to be called. By the Godot documentation, it isn't recommended to use this when calling various functions on the node, but due to the nature in which the system needs to wire things up, it needs a reference to the node that we are running the OnReady() function on to be able to ensure that the variables are properly setup with the values. This is done through the C#'s Extension Methods. This allows us to add OnReady() to a node reference, without having to recompile GodotSharp in order to add this method. See examples for NodePath and ResolveNode on how OnReady() works.

NodePath

NodePath will use a String Path to the Node that you want to access from the Godot side of things, in C#. It takes care of actually getting the instance when you call the OnReady() function in C#.

Example:

using Godot;
using Godot.Sharp.Extras;

public class MyNode : Container {
    [NodePath("Container/Label")]
    Label MyLabel;
    
    public override void _Ready() {
      this.OnReady();
      
      MyLabel.Text = "Hello World!";
    }
}

With the release of Godot 3.5 (available in RC releases, and soon Stable Releases), NodePath can now more efficently find nodes in the Scene Tree. Two additonal options can be used with NodePath, the first being that when No path is specified, it will attempt to find the node by using the Variable name, the Variable name with the '%' unqiue name and finally by the Type name. For example:

Scene Tree:

SceneTree Example

Code:

using Godot;
using Godot.Sharp.Extras;

public class MyNode : Control {
    [NodePath] Control MySpecialControl = null;
    [NodePath] Control MyNestedControl = null;
    [NodePath] Timer Countdown = null;

    public override void _Ready() {
      this.OnReady();
    }
}

ResolveNode

ResolveNode allows an exported variable to the editor, to receive a NodePath, that can still be resolved to an actual node used within the C# code, as a reference to the node in the same way that NodePath is used. The difference is, the NodePath is an Assignment from the Editor, or code, instead of it being a string path to the node itself.

Example:

using Godot;
using Godot.Sharp.Extras;

public class MyNode : Node2D {
  [Export]
  public string SpritePath;
  
  [ResolveNode(nameof(SpritePath))]
  Sprite PlayerSprite;
  
  public override void _Ready() {
    this.OnReady();
    
    PlayerSprite.Location = new Vector2(120,120);
  }
}

Resource

Resource attribute, added in 0.3.4 allows you to do the same thing as preload(), allowing you to load up your resources into variables, making it easer to reference what are resources, and what are nodes.

Example:

using Godot;
using Godot.Sharp.Extras;

public class MyNode : Node2D {
  [NodePath] Sprite MySprite = null;
  [Resource("res://Assets/Player/Sprite.png")] StreamTexture SpriteTexture = null;
  [Resource("res://Scenes/Bullets/Fireball.tscn")] PackedScene Fireball = null;

  public override void _Ready() {
    this.OnReady();
    MySprite.Texture = SpriteTexture;
  }

  public override void _Input(InputEvent event) {
    if (event.IsActionJustPressed("fire")) {
      var fireball = Fireball.Instance<Fireball>();
      fireball.Position = MySprite.Position;
      fireball.Fire();
    }
  }
}

Singleton

Singletons, or AutoLoads as they are referenced in Godot, are now easily accessable in C# through two different ways. The first is with the Singleton Attribute, allowing you to declare a singleton at the top of your script, for easy access. The other, is with the static generic method Singleton.Get<T>();

Example 1: Singleton Attribute

using Godot;
using Godot.Sharp.Extras;

public class MyNode : Node2D {
  [Singleton] SceneManager SceneManager;

  public override void _Ready() {
    this.OnReady();

    GD.Print(SceneManager.GetCurrentSceneName());
  }
}

Example 2: C# Method of Singleton Pattern
```cs
using Godot;
using Godot.Sharp.Extras;

public class SceneManager : Node {
  private static SceneManager _instance = null;
  public static SceneManager Instance {
    get {
      if (_instance == null)
        _instance = Singleton.Get<SceneManager>();
      return _instance;
    }
  }

  public string GetCurrentSceneName() {
    return "Current Scene";
  }
}

SignalHandler

SignalHandler allows you to connect node signals to methods to automatically be connected when OnReady is executed. You provide the Signal you wish to connect, along with the property or field of the variable holding the Node reference, to connect said signal. If the property or field does not exist, will throw an exception.

Example 1 (Standard Connect to node defined by NodePath/ResolvePath):

using Godot;
using Godot.Sharp.Extras;

public class MyNode : Node2D {
  [NodePath("VBoxContainer/MyButton")]
  Button _myButton = null;

  public override void _Ready() {
    this.OnReady();
  }

  [SignalHandler("pressed", nameof(_myButton))]
  void OnPressed_MyButton() {
    GD.Print("Hello World!");
  }
}

Example 2 (Connect to the node itself):

using Godot;
using Godot.Sharp.Extras;

public class MyControl : Control
{
  [NodePath("ColorRect")]
  ColorRect _color = null;
    
  public override void _Ready()
  {
    this.OnReady();
  }
    
  [SignalHandler("mouse_entered")]
  void OnMouseEntered() {
    _color.Color = new Color(0,1,0);
  }
    
  [SignalHandler("mouse_exited")]
  void OnMouseExited() {
    _color.Color = new Color(0,0,1);
  }
}

Example 3 (Chaining Multiple Nodes to the Same Function):

using Godot;
using Godot.Sharp.Extras;

public class MyPanel : Panel
{
  [NodePath("Button1")]
  Button _button1 = null;

  [NodePath("Button2")]
  Button _button2 = null;

  public override void _Ready()
  {
    this.OnReady();
  }

  [SignalHandler("pressed", nameof(_button1))]
  [SignalHandler("pressed", nameof(_button2))]
  void OnButtonPressed() {
    GD.Print("A button has been pressed.");
  }
}

Fluent Signals

Added in 3.1, Fluent Signals now allow for easy chaining of signal connect methods, in the form of a sentance. An example of this in action:

using Godot;
using Godot.Sharp.Extras;
using Godot.Sharp.Extras.Fluent;

public class MyPanel : Panel
{
  [NodePath] Button MyTestButton = null;
  ConnectedBinding ButtonSignalHandler = null;
  
  public override void _Ready() {
    this.OnReady();

    ButtonSignalHandler = MyTestButton.Connect("pressed")
          .WithBinds("Optional Params", MyTestButton, this)
          .WithFlags(ConnectFlags.Deferred,ConnectFlags.ReferenceCounted)
          .To(this,"OnButtonPressed");
  }

  public OnButtonPressed(string Param, Button theButton, Control parent) {
    // process stuff
    ButtonSignalHandler.Disconnect();
  }
}

Caveats

When compiling using NodePath, and ResolveNode, the compiler will give a warning about a variable associated with these two attributes, will never be assigned to, or have a value. This is because the compiler doesn't recognize that we are using the Extension Method OnReady() to actually assign values to these variables, the way to surpress this warning, is simply to assign null to the value of the variable, to surpress this warning.

Example:

using Godot;
using Godot.Sharp.Extras;

public class MyNode : Container {
  [Export]
  public Label MyLabel;
  
  public override void _Ready() {
    this.OnReady();
    
    MyLabel.Text = "Hello World";
  }
}

Will Generate this warning:

C:\MyProject\MyNode.cs (6,15): warning CS0649: Field 'MyNode.MyLabel' is never assigned to, and will always have its default value null [C:\MyProject\MyProject.csproj]

While doing this will solve the issue:

using Godot;
using Godot.Sharp.Extras;

public class MyNode : Container {
  [Export]
  public Label MyLabel = null;
  
  public override void _Ready() {
    this.OnReady();
    
    MyLabel.Text = "Hello World";
  }
}

Future items will be added as they are worked on.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.7.2

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on GodotSharpExtras:

Repository Stars
eumario/godot-manager
A Project, Version and Addons/Plugins manager for Godot Game Engine.
Version Downloads Last updated
0.5.0 83 9/25/2024
0.4.0 1,105 3/1/2023
0.4.0-beta.16 120 1/29/2023
0.4.0-beta.12 117 1/14/2023
0.4.0-beta.10 112 12/25/2022
0.4.0-beta.7 109 12/2/2022
0.4.0-beta.4 127 11/12/2022
0.3.6 914 11/3/2022
0.3.5.1 845 10/31/2022
0.3.4 1,181 7/16/2022
0.3.1 890 7/7/2022
0.3.0 861 6/30/2022
0.2.3 1,219 2/18/2022
0.2.2 929 2/18/2022
0.2.1 886 2/18/2022
0.2.0 922 2/17/2022
0.1.0 780 11/23/2021

Needs Godot 3.5.1 or later. For earlier versions, please use 0.3.1 or 0.2.3.