s&box docs

Component Interfaces

There are various interfaces that can be given to components for specific purposes.

There are various interfaces that can be given to components for specific purposes.

ExecuteInEditor

A component marked with ExecuteInEditor will also execute these methods in edit mode:

Sample code

public sealed class ExecuteInEditorSample : Component, Component.ExecuteInEditor
{
	protected override void OnEnabled()
	{
		base.OnEnabled();

		if ( Game.IsEditor )
		{
			Log.Error( "OnEnabled is also executed in editor" );
		}
	}
}

ICollisionListener

A component with this interface can react to physics collisions.

MethodDescription
OnCollisionStartCalled when this collider/rigidbody starts touching another collider.
OnCollisionUpdateCalled once per physics step for every collider being touched.
OnCollisionStopCalled when this collider/rigidbody stops touching another collider.

Sample code

public sealed class CollisionListenerSample : Component, Component.ICollisionListener
{
	public void OnCollisionStart( Collision other )
	{
		Log.Error( "Collision started with: " + other.Other.GameObject );
	}

	public void OnCollisionUpdate( Collision other )
	{
		Log.Error( "Collision continued with: " + other.Other.GameObject );
	}

	public void OnCollisionStop( CollisionStop other )
	{
		Log.Error( "Collision stopped with: " + other.Other.GameObject );
	}
}

ITriggerListener

A component with this interface can react to trigger interactions.

MethodDescription
OnTriggerEnterCalled when a collider enters the trigger.
OnTriggerExitCalled when a collider stops touching the trigger.

Sample code

public sealed class TriggerListenerSample : Component, Component.ITriggerListener
{
	public void OnTriggerEnter( Collider other )
	{
		Log.Error( "Trigger entered with: " + other.GameObject );
	}

	public void OnTriggerExit( Collider other )
	{
		Log.Error( "Trigger exited with: " + other.GameObject );
	}
}

IDamageable

A helper interface to mark components that can be damaged by something.

MethodDescription
OnDamageThe method you invoke when damaging something marked with IDamageable

Sample code

public sealed class SampleDamageable : Component, Component.IDamageable
{
	public void OnDamage( in DamageInfo damage )
	{
		Log.Error( $"I got damaged for {damage.Damage} by {damage.Attacker}" );
	}
}
public sealed class ClickToDamage : Component
{
	protected override void OnUpdate()
	{
		base.OnUpdate();

		if ( Input.Pressed( "attack1" ) )
		{
			var ray = Components.Get<CameraComponent>().ScreenPixelToRay( Mouse.Position );
			var trace = Scene.Trace.Ray( ray, 5000f ).Run();
			if ( trace.Hit )
			{
				var damageable = trace.GameObject.Components.Get<IDamageable>();

				if ( damageable != null )
				{
					damageable.OnDamage( new DamageInfo()
					{
						Damage = 12,
						Attacker = GameObject,
						Position = trace.HitPosition,
					} );
				}
			}
		}
	}
}

INetworkListener

A component with this interface can react to ๐Ÿ“† Network Events.

INetworkSpawn

A component with this interface can react to ๐Ÿ“† Network Events

Referenced API

Canonical API pages mentioned in this guide.

Created at:
Updated at:

On this page