menu_bookDocumentation

Serverside Code: #if SERVER Blocks and .Server.cs Files for Hidden Logic

calendar_today May 20, 2026 schedule ~1 min read person PatrickJr verified 50

s&box supports serverside-only code that is stripped from client builds. This only works when running local projects on dedicated servers.

#if SERVER Blocks

Code inside #if SERVER blocks only runs on dedicated servers:
CSHARP
protected override void OnUpdate()
{
#if SERVER
    Log.Info( "This is a server update!" );
#else
    Log.Info( "This is a client update!" );
#endif
}

Useful for sensitive code like API calls, database access, or anti-cheat logic that clients should never see.

.Server.cs Files

For cleaner code, create a companion file like GameManager.Server.cs alongside GameManager.cs. The .Server.cs file wraps everything in #if SERVER automatically — no preprocessor blocks needed in your main code. The class must be partial for this to work.

Publishing

All serverside code is stripped from published games by default. Combined with running your own dedicated server using a local project, this gives you full control over hidden server logic.
Was this helpful?