Using the Puck API
Now that we have our development environment set up, we can start adding dependency libraries to our project in order to interact with the Puck API.
Let's create a new
libsfolder within our project in which we will be placing our libraries.
Now we need to navigate to the directory where Puck is installed. This can be quickly done via Steam's
Browse local files.
Open the
Puck_Datafolder and within it, open theManagedfolder as well. The absolute path where the game's libraries are stores will look as follows:C:\Program Files (x86)\Steam\steamapps\common\Puck\Puck_Data\Managed.
Copy all of the contents from the
Managedfolder to your project'slibsfolder.
In order for our project to be aware of the added libraries, let's also add the the following to our
MyPuckMod.csprojfile:<ItemGroup> <Libs Include="libs\*.dll" Exclude="libs\System.*.dll" /> <Reference Include="@(Libs)"> <HintPath>%(Libs.FullPath)</HintPath> <Private>false</Private> </Reference> </ItemGroup>This
ItemGroupgathers all .dll files in ourlibsfolder and references them in our project. ThePrivateflag ensures the referenced libraries are not included in our final build. The resultingMyPuckMod.csprojshould now look as follows:<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net4.8</TargetFramework> </PropertyGroup> <ItemGroup> <Libs Include="libs\*.dll" Exclude="libs\System.*.dll" /> <Reference Include="@(Libs)"> <HintPath>%(Libs.FullPath)</HintPath> <Private>false</Private> </Reference> </ItemGroup> </Project>Let's update our
Class1.csfile to make use of theIPuckModinterface:using UnityEngine; namespace MyPuckMod { public class Class1 : IPuckMod { public bool OnEnable() { Debug.Log("Hello world from MyPuckMod!"); return true; } public bool OnDisable() { return true; } } }The
IPuckModinterface is what Puck looks for when loading your .dll. Make sure your mod also provides anOnEnableandOnDisablefunctions. Puck will call those functions internally when enabling/disabling your mod in the UI, when joining/disconnecting from a modded server and so on. Note that bothOnEnableandOnDisablereturntrue. This is a status indicator for Puck to know that your mod initialized or shutdown successfully. For any failures, make sure to returnfalse, as other system rely on this response to operate correctly! For the sake of keeping this guide simple, let's print out a simple "Hello world!" to the console.Build the project with
dotnet build:
Great! Your mod now has access to the Puck API along with any libraries Puck itself depends on by default. Head over to the next section to see how we can actually run your mod within the game.
Last updated