terminalCode Example
Component Event Interfaces for Tool Interactions
Component Event Interfaces (IPhysgunEvent, IToolgunEvent)
Event interfaces that allow components to respond to tool interactions. These use s&box's ISceneEvent<T> pattern for type-safe event routing.
IPhysgunEvent - Physgun Interactions
CSHARP
public interface IPhysgunEvent : ISceneEvent<IPhysgunEvent>
{
public class GrabEvent
{
/// <summary>
/// The connection attempting to grab this object.
/// </summary>
public Connection Grabber { get; init; }
/// <summary>
/// Set to true to cancel the grab.
/// </summary>
public bool Cancelled { get; set; }
}
/// <summary>
/// Called when a player attempts to grab this object with the physgun.
/// Set <see cref="GrabEvent.Cancelled"/> to true to reject the grab.
/// </summary>
void OnPhysgunGrab(GrabEvent e) { }
}IToolgunEvent - Toolgun Interactions
CSHARP
public interface IToolgunEvent : ISceneEvent<IToolgunEvent>
{
public class SelectEvent
{
/// <summary>
/// The connection attempting to use a tool on this object.
/// </summary>
public Connection User { get; init; }
/// <summary>
/// Set to true to reject the toolgun selection.
/// </summary>
public bool Cancelled { get; set; }
}
/// <summary>
/// Called when a player attempts to select this object with the toolgun.
/// Set <see cref="SelectEvent.Cancelled"/> to true to reject the selection.
/// </summary>
void OnToolgunSelect(SelectEvent e) { }
}Implementation Example
CSHARP
public class MyDoor : Component, IPhysgunEvent, IToolgunEvent
{
[Property] public bool CanBePhysgunned { get; set; } = false;
[Property] public bool CanBeTooled { get; set; } = true;
void IPhysgunEvent.OnPhysgunGrab(IPhysgunEvent.GrabEvent e)
{
if (!CanBePhysgunned)
e.Cancelled = true;
}
void IToolgunEvent.OnToolgunSelect(IToolgunEvent.SelectEvent e)
{
if (!CanBeTooled)
e.Cancelled = true;
}
}Firing Events from Tools
CSHARP
// In your physgun/toolgun implementation
var grabEvent = new IPhysgunEvent.GrabEvent
{
Grabber = connection
};
targetGameObject.RunEvent<IPhysgunEvent>(
x => x.OnPhysgunGrab(grabEvent),
FindMode.EverythingInSelfAndDescendants
);
if (grabEvent.Cancelled)
{
// Handle cancellation
return;
}Key Features
- Cancellation Pattern: Event args use Cancelled property for two-phase commit
- ISceneEvent Base: Inherits from ISceneEvent<T> for automatic scene event routing
- Default Implementations: Interface methods have default empty bodies
- Descendant Search: RunEvent with FindMode.EverythingInSelfAndDescendants notifies all components in hierarchy
Was this helpful?