Mod support paths

  • sbox.game/ugc resource publishing
  • scripting v0.1 publishing with steam ugc api

Resource publishing

most powerful, can publish code and assets bundled in a resource for others to download at runtime.

Scripting v0.1

Less powerful broadly but allows you to run client scripts (like lua). You get control over what users can touch, unlike resource publishing

Resource Publishing

s&box makes supporting mods in your game very easy in a few ways. The most powerful being GameResource publishing.

A GameResouce is just an asset like a texture or a sound. But it allows you to customize what data is stored. They function similar to DataAssets in unreal, ScriptableObjects in Unity and Resources in Godot.

The most basic GameResource is a class that inherits from GameResouce and defines properties.

using Sandbox;
[AssetType( Category = "Mod", Name = "ModResource", Extension = "mod")]
public class ModResource : GameResource
{
	[Property] public GameObject Prefab { get; set; }
	
	public override void ConfigurePublishing( ResourcePublishContext context )
	{
		context.IncludeCode = true;
	}
 
}

The AssetType attribute on the class allows you do create a resource from the right click menu in your assets panel.

overriding Configure Publishing and enabling Include Code. This will include any custom components attached to the Prefab referenced above.

For clients to download this resource, use Cloud.Load with the type and ident.

		Cloud.Load<ModResource>( "derrikcreates/test_mod" );

Hard coding the ident for this mod will work but doesnt give players the ability to select what mods they want to install.

Using Package.Fetch you can search for all the resources of your type. Then pass the ident from Fetch to Cloud.Load to download.

You should build out a UI from the data you receive from Package.Fetch to give playeres an option to enable mods.

Once the mod is downloaded and loaded, you can freely use that resource. Like cloning the prefab we included, the prefab that contains our custom component. This component can fully access the scene like any other.

This is almost exactly how the sandbox gamemode handles custom entities.

If a player want to create a mod for your game, Create a new addon project and target the base game.

Then create a game resource and fill in all the required information. The publish that resource to the workshop.

Using GameResources, Package.Fetch, Cloud.Load and a Custom UI, gives you the tools to build your own modding API for your game.

TODO

test reflection to see if a mod can call another mods code or if its restricted for some reaons ( i dont think so)