![]() |
VOOZH | about |
GoDotNet is no longer maintained and has been superseded by other Chickensoft projects. We really appreciate you using it, but there are better ways to do everything this package offered
State machines — please use Chickensoft.LogicBlocks — it is well tested, supports bindings, and can even generate a diagram of your code for you.
Notifiers - use C#'s BehaviorSubject from the Reactive Extensions.
Scheduler - use Godot's built-in call_deferred mechanism.
Thank you for your understanding. Please reach out in our Discord server if you have any questions. https://discord.gg/MjA6HUzzAE
dotnet add package Chickensoft.GoDotNet --version 1.5.16
NuGet\Install-Package Chickensoft.GoDotNet -Version 1.5.16
<PackageReference Include="Chickensoft.GoDotNet" Version="1.5.16" />
<PackageVersion Include="Chickensoft.GoDotNet" Version="1.5.16" />Directory.Packages.props
<PackageReference Include="Chickensoft.GoDotNet" />Project file
paket add Chickensoft.GoDotNet --version 1.5.16
#r "nuget: Chickensoft.GoDotNet, 1.5.16"
#:package Chickensoft.GoDotNet@1.5.16
#addin nuget:?package=Chickensoft.GoDotNet&version=1.5.16Install as a Cake Addin
#tool nuget:?package=Chickensoft.GoDotNet&version=1.5.16Install as a Cake Tool
👁 Chickensoft Badge
👁 Discord
👁 Read the docs
State machines, notifiers, and other utilities for C# Godot development.
<p align="center"> <img alt="Chickensoft.GoDotNet" src="Chickensoft.GoDotNet/icon.png" width="200"> </p>
🚨 Looking for node-based dependency injection with providers and dependents? That functionality has been moved to it's own package, GoDotDep!
While developing our own game, we couldn't find any simple C# solutions for simple state machines, notifiers, and mechanisms for avoiding unnecessary marshalling with Godot. So, we built our own systems — hopefully you can benefit from them, too!
Are you on discord? If you're building games with Godot and C#, we'd love to see you in the Chickensoft Discord server!
Find the latest version of GoDotNet on nuget.
In your *.csproj, add the following snippet in your <ItemGroup>, save, and run dotnet restore. Make sure to replace *VERSION* with the latest version.
<PackageReference Include="Chickensoft.GoDotNet" Version="*VERSION*" />
GoDotNet is itself written in C# 10 for netstandard2.1 (the highest language version currently supported by Godot). If you want to setup your project the same way, look no further than the file for inspiration!
Internally, GoDotNet uses GoDotLog for logging. GoDotLog allows you to easily create loggers that output nicely formatted, prefixed messages (in addition to asserts and other exception-aware execution utilities).
An autoload can be fetched easily from any node. Once an autoload is found on the root child, GoDotNet caches it's type, allowing it to be looked up instantaneously without calling into Godot.
public class MyEntity : Node {
private MyAutoloadType _myAutoload => this.Autoload<MyAutoloadType>();
public override void _Ready() {
_myAutoload.DoSomething();
var otherAutoload = this.Autoload<OtherAutoload>();
}
}
A Scheduler node is included which allows callbacks to be run on the next frame, similar to CallDeferred. Unlike CallDeferred, the scheduler uses vanilla C# to avoid marshalling types to Godot. Since Godot cannot marshal objects that don't extend Godot.Object/Godot.Reference, this utility is provided to perform the same function for records, custom types, and C# collections which otherwise couldn't be marshaled between C# and Godot.
Create a new autoload which extends the scheduler:
using GoDotNet;
public class GameScheduler : Scheduler { }
Add it to your project.godot file (preferably the first entry):
[autoload]
GameScheduler="*res://autoload_folder/GameScheduler.cs"
...and simply schedule a callback to run on the next frame:
this.Autoload<Scheduler>().NextFrame(
() => _log.Print("I won't execute until the next frame.")
)
GoDotNet provides a simple state machine implementation that emits a C# event when the state changes (since Godot signals are more fragile). If you try to update the machine to a state that isn't a valid transition from the current state, it throws an exception. The machine requires an initial state to avoid nullability issues during construction.
State machines are not extensible — instead, GoDotNet almost always prefers the pattern of composition over inheritance. The state machine relies on state equality to determine if the state has changed to avoid issuing unnecessary events. Using record or other value types for the state makes equality checking work automatically for free.
States used with a state machine must implement IMachineState<T>, where T is just the type of the machine state. Your machine states can optionally implement the method CanTransitionTo(IMachineState state), which should return true if the given "next state" is a valid transition. Otherwise, the default implementation returns true to allow transitions to any state.
To create states for use with a machine, create an interface which implements IMachineState<IYourInterface>. Then, create record types for each state which implement your interface, optionally overriding CanTransitionTo for any states which only allow transitions to specific states.
public interface IGameState : IMachineState<IGameState> { }
public record GameMainMenuState : IGameState {
public bool CanTransitionTo(IGameState state) => state is GameLoadingState;
}
public record GameLoadingState : IGameState {
public bool CanTransitionTo(IGameState state) => state is GamePlayingState;
}
// States can store values!
public record GamePlayingState(string PlayerName) {
public bool CanTransitionTo(IGameState state) => state is GameMainMenuState;
}
Simply omit implementing CanTransitionTo for any states which should allow transitions to any other state.
public interface GameSuspended : IGameState { }
Machines are fairly simple to use: create one with an initial state (and optionally register a machine state change event handler). A state machine will announce the state has changed as soon as it is constructed.
public class GameManager : Node {
private readonly Machine<IGameState> _machine;
public override void _Ready() {
_machine = new Machine<IGameState>(new GameMainMenuState(), OnChanged);
}
/// <summary>Starts the game.</summary>
public void Start(string name) {
_machine.Update(new GameLoadingState());
// do your loading...
// ...
// start the game!
_machine.Update(new GamePlayingState(name);
// Read the current state at any time:
var state = _machine.State;
if (state is GamePlayingState) { /* ... */ }
}
/// <summary>Goes back to the menu.</summary>
public void GoToMenu() => _machine.Update(new GameMainMenuState());
public void OnChanged(IGameState state) {
if (state is GamePlayingState playingState) {
var playerName = playingState.Name();
// ...
}
}
}
If you want another object to only be able to read the current state of a state machine and subscribe to changes, but not be able to update the state of the machine, you can expose the machine as an IReadOnlyMachine<TState> instead of as a Machine<TState>.
public class AnObjectThatOnlyListensToAMachine {
public IReadOnlyMachine<string> Machine { get; set; }
// This object can listen and respond to state changes in Machine, but
// cannot mutate the state of the machine via `Machine.Update` :)
}
A notifier is an object which emits a signal when its value changes. Notifiers are similar to state machines, but they don't care about transitions. Any update that changes the value (determined by comparing the new value with the previous value using Object.Equals) will emit a signal. Like state machines, the value of a notifier can never be null — make sure you initialize with a valid value!
Using "value" types (primitive types, records, and structs) with a notifier is a natural fit since notifiers check equality to determine if the value has changed. Like state machines, notifiers also invoke an event to announce their value as soon as they are constructed.
var notifier = new Notifier<string>("Player", OnPlayerNameChanged);
notifier.Update("Godot");
// Elsewhere...
private void OnPlayerNameChanged(string name, string previous) {
_log.Print($"Player name changed from ${previous} to ${name}");
}
To easily inject dependencies to descendent nodes, check out go_dot_dep.
As with state machines, a notifiers can be referenced as an IReadOnlyNotifier<TValue> to prevent objects using them from causing unwanted changes. The object(s) that own the notifier can reference it as a Notifier<TValue> and mutate it accordingly, while the listener objects can simply reference it as an IReadOnlyNotifier<TValue>.
public class AnObjectThatOnlyListensToANotifier {
public IReadOnlyNotifier<string> Notifier { get; set; }
// This object can listen and respond to changes in the Notifier's value, but
// cannot mutate the value of the notifier via `Notifier.Update` :)
}
Godot supports emitting signals from C#. Because Godot signals pass through the Godot engine, any arguments given to the signal must be marshalled through Godot, forcing them to be classes which extend Godot.Object/Godot.Reference (records aren't allowed). Likewise, all the fields in the class must also be the same kind of types so they can be marshalled, and so on.
It's not possible to have static typing with signal parameters, so you don't find out until runtime if you accidentally passed the wrong parameter. The closest you can do is the following, which wouldn't break at compile time if the receiving function signature happened to be wrong.
public class ObjectType {
[Signal]
public delegate void DoSomething(string value);
}
public class MyNode : Node {
// Inside your node
public override void _Ready() {
_ = object.Connect(
nameof(ObjectType.DoSomething),
this,
nameof(MyDoSomething)
);
}
private void DoSomething(int value) {
// WARNING: Value should be string, not int!!
}
}
Because of these limitations, GoDotNet will avoid Godot signals except when necessary to interact with Godot components. For communication between C# game logic, it will typically be preferable to use C# events instead.
// Declare an event signature — no [Signal] attribute necessary.
public delegate void Changed(Type1 value1, Type2 value2);
// Add an event field to your emitter
public event Changed? OnChanged;
// Trigger an event from your emitter:
OnChanged?.Invoke(argument1, argument2);
// Listen to an event in your receiver:
var emitter = new MyEmitterObject();
emitter.OnChanged += MyOnChangedHandler;
// Event handler in your receiver:
private void MyOnChangedHandler(Type1 value1, Type2 value2) {
// respond to event we received
}
GoDotNet includes a testable convenience wrapper for System.Random.
public partial class MyNode : Node {
public IRng Random { get; set; } = new Rng();
public override void _Ready() {
int i = Rng.NextInt(); // 0 (inclusive) to int.MaxValue (exclusive)
float f = Rng.NextFloat(); // 0 (inclusive) to 1 (exclusive)
double d = Rng.NextDouble(); // 0 (inclusive) to 1 (exclusive)
// 0 (inclusive) to 25 (exclusive)
int ir = Rng.RangeInt(0, 25);
// 0.0f (inclusive) to 25.0f (exclusive)
float fr = Rng.RangeFloat(0f, 25f);
// 0.0d (inclusive) to 25.0d (exclusive)
double dr = Rng.RangeDouble(0d, 25d);
}
}
🐣 Package generated from a 🐤 Chickensoft Template — https://chickensoft.games
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
Chickensoft.GoDotNet release.