Skip to content

Script engine rules

The script engine runs most of C#, with a few differences. The error messages tell you what to change; these are the ones you will meet first:

  • Write RustLegacy.DamageInfo, not DamageInfo: s&box has a type with the same name, and the hook would not match (“No approved virtual base method matches”).
  • Test objects with x != null. x.IsValid() fails when x is null.
  • No static fields or properties that change, and no static computed properties (static long Now => ...): keep state in fields of your plugin class and write a method (long Now() => ...).
  • Callbacks take one parameter at most (Action, Action<T>, Func<T>, Func<T, TResult>): that is why commands receive one cmd.
  • LINQ lambdas sometimes need a plain loop instead (“Cannot infer an unambiguous compatible delegate signature”), for instance over a Dictionary.
  • No switch expressions, lock or goto. Use if / else.
  • List.RemoveAll( x => ... ) takes a Predicate, which the script engine does not pass: remove in a backwards for loop.
  • Helper classes in your file are shared by every plugin: give them your plugin’s name as a prefix (ArenaConfig, not Config).
  • Async: only named async Task methods, resumed each tick; Task.Delay is not available. Use After instead.
  • TimeSpan is not available: for seconds, divide DateTime.UtcNow.Ticks by 10_000_000 (or use RealTime.Now within a run).
  • out var sometimes fails (“needs value arguments whose types are known”): declare the variable first (int n = 0; int.TryParse( s, out n )).

Facepunch’s documentation of the script engine: https://sbox.game/dev/doc/code/scripting/.

Arena.cs is a complete example: chat commands, a menu and a button, config, saved data, inventories, damage rules, deaths and respawns. Creative.cs and Give.cs are short ones, Airdrop.cs shows HUD texts, markers and per-player settings, NoMobs.cs blocks spawns and ServerInfo.cs shows images Presents.cs uses models and sounds from the cloud, Legendary.cs defines custom items and AlphaWolf.cs a custom mob. Chat.cs uses OnPlayerChat and chat tags, Remover.cs the building helpers and CanDamageBuilding, Teleporter.cs makes a new object (Model, UseLabel, OnDeployableUse, CanBuild), Rally.cs a custom car (DefineVehicle, SpawnVehicle).