Getting started with Development
Write your first Alt:SA gamemode in C# with Godfather and load it on your server.
Introduction#
We are going to show you how to write your own gamemode: a small C# library the dedicated server loads from its plugins/ folder. By the end, players who join your server get a welcome panel and spawn when they press Spawn.
Requirements#
- A server build. Read Getting started with Server first.
- Basic C# knowledge.
- The .NET SDK, and an editor such as Visual Studio or Visual Studio Code.
How a server is scripted#
Everything a gamemode does runs on the server:
- Server-side is your gamemode: it reacts to events, spawns players, stores data and decides the rules.
- The client draws what the server describes, such as a glass UI panel or a toast. There is no client scripting; read Getting started with Client-side for what that means.
Create your first gamemode#
Create the project#
- Create a class library:
dotnet new classlib -n HelloGodfather -f netstandard2.0. - Reference
Godfather.dll. A server build has it inAlt-SA-Server_Data/Managed/Godfather.dll; add it to the.csprojwithPrivateset to false, so it isn't copied next to your DLL:
1<ItemGroup>2 <Reference Include="Godfather">3 <HintPath>C:\altsa-server\Alt-SA-Server_Data\Managed\Godfather.dll</HintPath>4 <Private>false</Private>5 </Reference>6</ItemGroup>Keep the target at netstandard2.0: that's what the engine's runtime loads.
Write the gamemode#
Replace the generated class with this:
1using Godfather;2 3[Gamemode("hello-godfather", Version = "0.1.0")]4public class HelloGodfather : Gamemode5{6 // Players stay unspawned until they press "Spawn".7 public override bool SpawnOnJoin => false;8 9 public override void OnPlayerJoined(Player player)10 {11 player.Freeze();12 13 var panel = UI.Panel(player, "Welcome", "My first gamemode");14 panel.Text($"Hi {player.Name}, ready?");15 panel.Button("Spawn", () =>16 {17 panel.Close();18 player.Unfreeze();19 player.Spawn(Spawn.Default);20 player.Toast("Good luck out there.", ToastKind.Success);21 });22 }23}The [Gamemode] attribute names your gamemode; that id is what server.json uses. SpawnOnJoin => false keeps players out of the world until you spawn them. OnPlayerJoined runs once the player has connected and sent their name, which is the moment to show a welcome screen.
Load it on your server#
- Build:
dotnet build -c Release. - Copy
bin/Release/netstandard2.0/HelloGodfather.dllinto your server'splugins/folder. - Set
"gameMode": "hello-godfather"inserver.jsonand restart the server. The log saysfound gamemode 'hello-godfather'and thenactivated. - Join with
127.0.0.1:7777. You should see the welcome panel, and spawn when you press Spawn.
A complete example with a visit counter in storage is examples/HelloGodfather in the Godfather-API repository.
Next step#
Continue with Getting started with Events to learn every event a gamemode can handle.