agentleFS
vl4dtvl4dt/godot-skills/skills/godot-csharp-patterns/SKILL.md

godot-csharp-patterns

Skill8 starsChanged 5 months agoMITView on GitHub

C# Mono patterns for Godot 4.x: Export attributes, signal handling, performance optimization, GDExtension interop, and best practices for first-class C# support. Use when writing C# code in Godot.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Create a .gdextension file to expose C# classes to GDScript projects.

    line 219
  2. Very specific:

    Use await with tweens for async animation sequences

    line 224
  3. Distinctive:

    When the MCP bridge is running (Phase 2), these tools can inspect and modify your project live:

    line 229

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

C# Patterns for Godot 4.x

Best practices for Godot's first-class C# (Mono) support.

Export Attributes

Inspector Properties

using Godot;

public partial class Player : CharacterBody2D
{
    // Basic export
    [Export] public float Speed { get; set; } = 200f;

    // With range
    [Export(PropertyHint.Range, "0,100,0.5")]
    public float JumpForce { get; set; } = 400f;

    // File path
    [Export(FileFilter = "*.png,*.svg")]
    public string IconPath { get; set; } = "";

    // Resource export
    [Export] public PackedScene BulletScene { get; set; }

    // Enum with hint
    [Export] public MovementMode MoveMode { get; set; }

    public enum MovementMode
    {
        Walk,
        Run,
        Sprint
    }
}

Export Categories

[ExportGroup("Combat")]
[Export] public int AttackDamage { get; set; } = 10;
[Export] public float AttackCooldown { get; set; } = 0.5f;

[ExportGroup("Movement")]
[Export] public float MoveSpeed { get; set; } = 200f;
[Export] public float JumpForce { get; set; } = 400f;

Signal Handling

C# Event Syntax vs Godot Signals

// Godot signal declaration in GDScript (autoload)
// class_name EventBus extends Node
// signal player_died(Vector2 position)

// C# subscriber
public override void _Ready()
{
    // Method reference (recommended — auto-disconnect on free)
    EventBus.PlayerDied += OnPlayerDied;

    // Lambda (requires manual disconnect)
    EventBus.PlayerDied += (pos) => GD.Print($"Died at {pos}");
}

public override void _ExitTree()
{
    // Clean up lambda subscriptions
    EventBus.PlayerDied -= OnPlayerDied;
}

private void OnPlayerDied(Vector2 position)
{
    GD.Print($"Player died at {position}");
}

Using Godot.Signal for C#-Side Signals

public partial class Enemy : CharacterBody2D
{
    [Signal] public delegate void HealthChangedEventHandler(int newHealth);
    [Signal] public delegate void DefeatedEventHandler();

    private int health = 100;

    public void TakeDamage(int amount)
    {
        health -= amount;
        EmitSignal(SignalName.HealthChanged, health);

        if (health <= 0)
        {
            EmitSignal(SignalName.Defeated);
            QueueFree();
        }
    }
}

Performance Considerations

Avoiding GC Pressure

// Bad: Creates garbage every frame
public override void _Process(double delta)
{
    var text = $"Score: {score}";  // String allocation
    label.Text = text;
}

// Good: Reuse objects, avoid allocations
private readonly StringBuilder _sb = new();

public override void _Process(double delta)
{
    _sb.Clear();
    _sb.Append("Score: ");
    _sb.Append(score);
    label.Text = _sb.ToString();
}

Structs vs Classes

// Use structs for small, short-lived data
public struct Vector2Int
{
    public int X { get; }
    public int Y { get; }
    public Vector2Int(int x, int y) => (X, Y) = (x, y);
}

// Use classes for objects with identity/lifetime
public class PlayerData : Resource
{
    public string Name { get; set; }
    public int Level { get; set; }
}

Instance Pooling

public partial class BulletPool : Node
{
    private readonly Queue<Bullet> _pool = new();
    private readonly PackedScene _bulletScene;

    public BulletPool(PackedScene bulletScene) => _bulletScene = bulletScene;

    public Bullet Get()
    {
        if (_pool.TryDequeue(out var bullet))
        {
            bullet.Visible = true;
            return bullet;
        }
        return _bulletScene.Instantiate<Bullet>();
    }

    public void Return(Bullet bullet)
    {
        bullet.Visible = false;
        bullet.QueueFree();
        _pool.Enqueue(bullet);
    }
}

GDExtension Interop

Calling GDScript from C#

// GDScript (autoload "GameData")
// class_name GameData extends Node
// var high_score: int = 0

// C# access
var gameData = Engine.GetSingleton("GameData") as GodotObject;
var highScore = gameData.Get("high_score");

Exposing C# to GDScript via GDExtension

Create a .gdextension file to expose C# classes to GDScript projects.

Godot 4.7 C# Notes

  • C# fully supports new 4.7 features including VirtualJoystick and tween_await()
  • Use await with tweens for async animation sequences
  • Export attributes work the same — no changes in 4.7

MCP Bridge Tools (Optional — Live Editor Integration)

When the MCP bridge is running (Phase 2), these tools can inspect and modify your project live:

  • script_read — Read C# script files for review
  • script_edit — Update C# scripts directly
  • script_validate — Validate C# scripts for syntax errors
  • scene_tree — Inspect scene hierarchy to verify node composition
  • class_db_info — Query Godot class info for C# interop patterns

Note: MCP tools require the Godot editor to be running with the MCP plugin enabled. Skills work independently without the bridge.

References

  • C# API Reference
  • Performance Guide
  • GDExtension Guide

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.