Using the Puck API
Last updated
Last updated
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 libs
folder 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_Data
folder and within it, open the Managed
folder 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 Managed
folder to your project's libs
folder.
In order for our project to be aware of the added libraries, let's also add the the following to our MyPuckMod.csproj
file:
<ItemGroup>
<Libs Include="libs\*.dll" Exclude="libs\System.*.dll" />
<Reference Include="@(Libs)">
<HintPath>%(Libs.FullPath)</HintPath>
<Private>false</Private>
</Reference>
</ItemGroup>
This ItemGroup
gathers all .dll files in our libs
folder and references them in our project. The Private
flag ensures the referenced libraries are not included in our final build.
The resulting MyPuckMod.csproj
should 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.cs
file to make use of the IPuckMod
interface:
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 IPuckMod
interface is what Puck looks for when loading your .dll. Make sure your mod also provides an OnEnable
and OnDisable
functions. 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 both OnEnable
and OnDisable
return true
. This is a status indicator for Puck to know that your mod initialized or shutdown successfully. For any failures, make sure to return false
, 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.