codeAPI Reference
s&box IToolActionEvents: Tool action callbacks
IToolActionEvents Interface API Reference
IToolActionEvents allows listening to tool actions across the scene. Implement this on a Component to receive callbacks before and after any tool action fires. Use this to enforce limits (e.g. max balloons, max constraints) or react to tool usage.
Type Signature
CSHARP
public interface IToolActionEvents : ISceneEvent<IToolActionEvents>
{
public class ActionData
{
public ToolMode Tool { get; init; }
public ToolInput Input { get; init; }
public PlayerData Player { get; init; }
public bool Cancelled { get; set; }
}
public class PostActionData
{
public ToolMode Tool { get; init; }
public ToolInput Input { get; init; }
public PlayerData Player { get; init; }
public List<GameObject> CreatedObjects { get; init; }
}
void OnToolAction(ActionData e);
void OnPostToolAction(PostActionData e);
}Methods
OnToolAction(ActionData e)
Called before a tool action executes. Set ActionData.Cancelled to true to reject the action.OnPostToolAction(PostActionData e)
Called after a tool action has executed successfully.ActionData Properties
- ToolMode Tool - The tool mode that is about to execute. Check e.Tool is Balloon, e.Tool is Weld, etc. for tool-specific logic
- ToolInput Input - Which input triggered this action
- PlayerData Player - The player performing the action
- bool Cancelled { get; set; } - Set to true to cancel the action
PostActionData Properties
- ToolMode Tool - The tool mode that executed the action
- ToolInput Input - Which input triggered this action
- PlayerData Player - The player who performed the action
- List<GameObject> CreatedObjects - GameObjects created by this action, if any
Usage
CSHARP
public class BalloonLimiter : Component, IToolActionEvents
{
private const int MaxBalloons = 50;
void IToolActionEvents.OnToolAction(ActionData e)
{
if (e.Tool is Balloon)
{
var balloonCount = Scene.GetAll<BalloonEntity>().Count();
if (balloonCount >= MaxBalloons)
{
e.Cancelled = true;
}
}
}
void IToolActionEvents.OnPostToolAction(PostActionData e)
{
if (e.Tool is Balloon)
{
// Log balloon creation
Log.Info($"Created {e.CreatedObjects.Count} balloons");
}
}
}Notes
- Implements ISceneEvent for scene-wide broadcasting
- Use for enforcing limits on tool usage
- Use for reacting to tool actions (logging, stats, etc.)
- Cancelled property prevents the action from executing
- PostActionData includes created GameObjects for tracking
Was this helpful?