terminalCode Example

DynamiteEntity Explosive Entity Pattern

calendar_today May 3, 2026 schedule ~1 min read person patrickjr verified 50

DynamiteEntity - Simple Explosive Entity

A simple explosive entity that can be triggered via damage or player input. Demonstrates IPlayerControllable, Component.IDamageable, and prefab cloning patterns.

Implementation

CSHARP
[Alias("dynamite")]
public class DynamiteEntity : Component, IPlayerControllable, Component.IDamageable
{
    [Property, Range(1, 500), Step(1), ClientEditable]
    public float Damage { get; set; } = 128;

    [Property, Range(16, 4096), Step(16), ClientEditable]
    public float Radius { get; set; } = 1024f;

    [Property, Range(1, 100), Step(1), ClientEditable]
    public float Force { get; set; } = 1;

    [Property, Sync, ClientEditable]
    public ClientInput Activate { get; set; }

    bool _isDead = false;

    [Rpc.Host]
    public void Explode()
    {
        _isDead = true;

        var explosionPrefab = ResourceLibrary.Get<PrefabFile>("/prefabs/engine/explosion_med.prefab");
        if (explosionPrefab == null)
        {
            Log.Warning("Can't find /prefabs/engine/explosion_med.prefab");
            return;
        }

        var go = GameObject.Clone(explosionPrefab, new CloneConfig 
        { 
            Transform = WorldTransform.WithScale(1), 
            StartEnabled = false 
        });
        
        if (!go.IsValid()) return;

        // Configure the explosion
        go.RunEvent<RadiusDamage>(x =>
        {
            x.Radius = Radius;
            x.PhysicsForceScale = Force;
            x.DamageAmount = Damage;
            x.Attacker = go;
        }, FindMode.EverythingInSelfAndDescendants);

        go.Enabled = true;
        go.NetworkSpawn(true, null);

        GameObject.Destroy();
    }

    void IDamageable.OnDamage(in DamageInfo damage)
    {
        if (_isDead) return;
        if (IsProxy) return;

        Explode();
    }

    void IPlayerControllable.OnControl()
    {
        if (Activate.Pressed())
        {
            Explode();
        }
    }

    void IPlayerControllable.OnEndControl() { }
    void IPlayerControllable.OnStartControl() { }
}

Key Patterns

Prefab Cloning

CSHARP
// Load a prefab resource
var prefab = ResourceLibrary.Get<PrefabFile>("/path/to/prefab.prefab");

// Clone with configuration
var instance = GameObject.Clone(prefab, new CloneConfig 
{ 
    Transform = WorldTransform.WithScale(1),
    StartEnabled = false  // Spawn disabled, configure first
});

// Configure via events
instance.RunEvent<SomeComponent>(x => x.Property = value, FindMode.EverythingInSelfAndDescendants);

// Enable and network spawn
instance.Enabled = true;
instance.NetworkSpawn(true, null);

Host-Only RPC

CSHARP
[Rpc.Host]
public void Explode()
{
    // This only executes on the host
    // Safe to modify scene state
}

Input Binding

CSHARP
[Property, Sync, ClientEditable]
public ClientInput Activate { get; set; }

// In the editor, this creates an input selector
// In code, check with: Activate.Pressed()

Damageable Interface

CSHARP
void IDamageable.OnDamage(in DamageInfo damage)
{
    // Called when the entity takes damage
    // Use 'in' parameter modifier for efficiency
}

Alias Attribute

The [Alias("dynamite")] attribute allows the entity to be referenced by name in console commands and other string-based lookups.

ClientEditable Properties

Properties marked with [ClientEditable] can be modified in the editor by clients (when host allows), enabling gameplay tuning without code changes.

Was this helpful?