Index of every page on this site: llms.txt
All skills
jame581 avatar

/csharp-signals

@426e8ab
by Jan Mesarčjame581/godotprompter979 stars
63
Run it yourself

Use when implementing signals in C# — [Signal] delegates, EmitSignal patterns, and async signal awaiting. Triggers on declaring, emitting, connecting, or awaiting a signal from a C# script, and on a signal crossing between C# and GDScript in either direction.

Use this Skill: https://skilld.dev/gh/jame581/godotprompter/csharp-signals

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesdisconnecting.md

≈806 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Disconnecting Signals (C#)

Reference for skills/csharp-signals/SKILL.md — what Godot disconnects for you, the two cases it does not, -= operator + _ExitTree, SafeDisconnect helper.

← Back to SKILL.md


4. Disconnecting Signals

The -= operator and _ExitTree() cleanup

Player's signals below are custom [Signal]s, one of the two cases Godot does not disconnect when the receiver is freed (see the last section). Without the -=, the next emission after the HUD is freed reaches a disposed object and throws ObjectDisposedException.

using Godot;

public partial class HudLayer : CanvasLayer
{
    private Player _player;

    public override void _Ready()
    {
        _player = GetNode<Player>("../Player");
        _player.HealthChanged += OnHealthChanged;
        _player.Died          += OnDied;
        _player.ItemCollected += OnItemCollected;
    }

    // _ExitTree is called when the node leaves the tree, including when it is freed.
    // If this node can leave and re-enter the tree, connect in _EnterTree instead:
    // _Ready runs only once, so the connections would not come back.
    public override void _ExitTree()
    {
        // Mirror every += from _Ready() with a -=.
        _player.HealthChanged -= OnHealthChanged;
        _player.Died          -= OnDied;
        _player.ItemCollected -= OnItemCollected;
    }

    private void OnHealthChanged(int current, int maximum) { /* ... */ }
    private void OnDied() { /* ... */ }
    private void OnItemCollected(string itemName) { /* ... */ }
}

SafeDisconnect pattern

When the signal source may have already been freed (e.g., it lives in a different scene), guard the disconnection with IsInstanceValid.

public override void _ExitTree()
{
    if (IsInstanceValid(_player))
    {
        _player.HealthChanged -= OnHealthChanged;
        _player.Died          -= OnDied;
    }
}

What Godot disconnects for you, and what it does not

When any GodotObject is freed, Godot disconnects every connection associated with it, for signal emitters and for signal receivers. A built-in signal connected to a method of the receiver (button.Pressed += OnPressed) needs no manual cleanup.

Automatic disconnection does not happen when the receiver is freed while the emitter is still alive, in two cases:

  • The signal is a custom [Signal] connected with +=.
  • The handler is a lambda that captures a variable. Godot uses Delegate.Target to tell which instance a delegate belongs to; a capturing lambda's target is a compiler-generated closure object, not the node. Callable.From with Connect has the same limit.

In both cases the next emission calls into a disposed object and throws ObjectDisposedException. Keep a reference to the delegate and pass it to -=.

Automatic disconnection is fully reliable when the emitter is freed before its receivers, so a project that always frees emitters first never meets either case.

Rule: every += on a custom [Signal], and every capturing lambda, gets a matching -= in _ExitTree(). Connect in _Ready(), or in _EnterTree() for a node that can leave and re-enter the tree.


Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts7d3 checks · Risk SAFE
  • Gen Agent Trust Hub7d

    The skill provides safe documentation and code templates for implementing Godot 4.x signals in C#. No malicious patterns, obfuscation, or data exfiltration risks were detected.

  • Socket7d

    No alerts

  • Snyk7d

    Risk: LOW · No issues

Signed by skilld at 426e8ab. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 6 days ago.

Activeupdated last week

README badge

README badge for jame581/godotprompter/csharp-signals