Loading...
Loading...
Use C#/.NET in Godot 4.x: partial classes extending nodes, the PascalCase lifecycle (_Ready/_Process/_PhysicsProcess), [Export] fields, [Signal] delegates as C# events, GetNode<T>, and calling between C# and GDScript. Use when writing Godot game code in C# (.cs files, .csproj), needing the Godot .NET build, converting GDScript patterns to C#, or wiring Godot signals as C# events.
npx skill4agent add gamedev-skills/awesome-gamedev-agent-skills godot-csharp.cs.csproj[Export][Signal]godot-gdscriptgodot-*.csproj.slndotnet buildpartialpartialdouble_Ready()_Process(double delta)_PhysicsProcess(double delta)[Export]@export[Signal]XxxEventHandlerEmitSignal(SignalName.Xxx, ...)eventGetNode<T>("Path")%UniqueCallGetSetusing Godot;
public partial class Player : CharacterBody2D
{
[Export] public float Speed = 200.0f; // editable in the Inspector
[Export] public float JumpVelocity = -400.0f;
private const float Gravity = 1200.0f;
private AnimatedSprite2D _sprite;
public override void _Ready()
{
_sprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
}
public override void _PhysicsProcess(double delta)
{
Vector2 v = Velocity; // Velocity is a property here
if (!IsOnFloor())
v.Y += Gravity * (float)delta; // delta is double; cast for float math
if (Input.IsActionJustPressed("jump") && IsOnFloor())
v.Y = JumpVelocity;
float dir = Input.GetAxis("move_left", "move_right");
v.X = dir != 0 ? dir * Speed : Mathf.MoveToward(v.X, 0, Speed);
Velocity = v;
MoveAndSlide(); // no args, like GDScript 4.x
}
}using Godot;
public partial class Health : Node
{
// Delegate name MUST end with "EventHandler"; generator creates the event + SignalName.
[Signal] public delegate void HealthChangedEventHandler(int current, int max);
private int _hp = 100;
public void TakeDamage(int amount)
{
_hp = Mathf.Max(_hp - amount, 0);
EmitSignal(SignalName.HealthChanged, _hp, 100); // type-safe signal name
}
public override void _Ready()
{
HealthChanged += OnHealthChanged; // subscribe like a normal C# event
}
private void OnHealthChanged(int current, int max) => GD.Print($"HP {current}/{max}");
}public partial class Spawner : Node2D
{
// Load once; PackedScene is the C# equivalent of preload's result.
private readonly PackedScene _bullet = GD.Load<PackedScene>("res://bullet.tscn");
public void Shoot(Vector2 at)
{
var b = _bullet.Instantiate<Node2D>(); // typed instantiate
b.GlobalPosition = at;
AddChild(b);
}
}public override void _Ready()
{
Node gd = GetNode("GDScriptNode");
// Call a GDScript method and read/write its properties dynamically.
gd.Call("take_damage", 10);
int score = (int)gd.Get("score");
gd.Set("score", score + 5);
// Connect to a GDScript signal by name:
gd.Connect("died", Callable.From(OnDied));
}
private void OnDied() => GD.Print("entity died");partialpartial[Export][Signal]_Ready_Process(double)_PhysicsProcess(double)doublefloat[Signal]EventHandlerSignalName.XeventGD.PrintConsole.WriteLineGD.PrintGD.PrintErrConsoleVector2ColorTransform2Dvar v = Velocity; v.X = ...; Velocity = v;Velocity.XQueueFree()Free()QueueFree()ObjectDisposedException[ExportGroup]await ToSignal(...)Godot.Collectionsreferences/csharp-setup-and-interop.mdgodot-gdscriptgodot-signals-groupsgodot-resources[Export]Resourceunity-csharp-scripting