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, notDamageInfo: 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 whenxis null. - No
staticfields or properties that change, and nostaticcomputed 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 onecmd. - LINQ lambdas sometimes need a plain loop instead (“Cannot infer an unambiguous compatible delegate signature”), for instance over a
Dictionary. - No
switchexpressions,lockorgoto. Useif/else. List.RemoveAll( x => ... )takes aPredicate, which the script engine does not pass: remove in a backwardsforloop.- Helper classes in your file are shared by every plugin: give them your plugin’s name as a prefix (
ArenaConfig, notConfig). - Async: only named
async Taskmethods, resumed each tick;Task.Delayis not available. UseAfterinstead. TimeSpanis not available: for seconds, divideDateTime.UtcNow.Ticksby10_000_000(or useRealTime.Nowwithin a run).out varsometimes 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).