Guide: Creating Modules

Creating Modules :package:

WIP - not done yet

Modules are a very barebones way to extend game functionality. Modules can be used to create plugin frameworks (i.e. RocketMod and OpenMod), add new tools and features (i.e. commands, UIs, custom gamemodes), and more.

1. Installing Necessary Tools

To make modules for Unturned, you’ll need the following tools:

2. Setting Up Visual Studio for .NET Framework

While in the Visual Studio installer, select the ‘.NET desktop development’ workload and press ‘Install/Modify.’

3. Creating a New Project

Open Visual Studio 2022 after properly installing it and select ‘Create a new project’, then create a new ‘Class Library (.NET Framework).’

In the project configuration menu, set your framework version to 4.8. Any framework version should be acceptable, but I recommend 4.8. Alongside that, for this tutorial, I’ll be naming the module ‘UnturnedModule.’

4. Setting Up Library References using NuGet

NuGet is a package manager created by Microsoft, it is a standardized way to manage and distribute libraries and dependencies for your projects. For Unturned, you can use it to easily install the proper redistributables required for modules.

In Visual Studio, open the ‘Package Manager Console’ as shown below.

In that console, input NuGet\Install-Package RocketModFix.Unturned.Redist and
NuGet\Install-Package RocketModFix.UnityEngine.Redist, this will install the dependencies required for creating modules.

Note: I use the NuGet packages provided by RocketModFix for my plugins. OpenMod has their own packages for Unturned and UnityEngine redistributables.

5. Changing Copy Settings for References

Since the game already loads the required libraries, there is no reason for the libraries to be copied to the output folder of the project. Under ‘References’ in Solution Explorer, select all of the items and in the Properties window set ‘Copy Local’ to false.

6. Adding Required Module Files

Add two new files to your project. Right-click your project in the Solution Explorer and select ‘Add’ → ‘New Item’ and select ‘Text File.’ Add a file named English.dat and then UnturnedModule.module. Keep note that ‘UnturnedModule’ in the .module file should be changed to your library’s name.

Select the newly created files in the Solution Explorer and set ‘Copy to Output Directory’ to ‘Copy always.’ This will copy them to the output directory when the library is compiled so you can use them later.

You can now edit the ‘English.dat’ file and add the lines shown below. Keep in mind that ‘<project_name>’ and ‘<project_description>’ can be replaced with your own custom text.

Name <project_name>
Description <project_description>

Also edit the .module file and add the text shown below. The .module file uses JSON formatting. The ‘Version’ property can be changed freely when you wish to increment your module’s versioning. ‘<project_name>’ should be replaced with your C# project’s name, which in this tutorial is ‘UnturnedModule.’

{
    "IsEnabled": true,
    "Name": "<project_name>",
    "Version": "1.0.0.0",
    "Assemblies":
    [
        {
            "Path": "/<project_name>.dll",
            "Role": "Both_Optional"
        }
    ]
}

7. Writing Proper Code

In Visual Studio, C# projects normally come with a default .cs file. In this case, it should in theory be named ‘Class1.cs.’ Modify the name of this file and rename it to whatever you want. For this I’ll be naming it ‘Main.’

Somewhere at the top of the project add using SDG.Framework.Modules; and have the class inherit IModuleNexus.

using System;
using SDG.Framework.Modules;

namespace UnturnedModule
{
    public class Main : IModuleNexus
    {
    }
}

After properly inheriting the IModuleNexus interface, implement the interface fully by adding the initialize and shutdown methods to the class.

using System;
using SDG.Framework.Modules;

namespace UnturnedModule
{
    public class Main : IModuleNexus
    {
        public void initialize()
        {

        }

        public void shutdown()
        {

        }
    }
}

Add Console.WriteLine("Tutorial code was loaded, yay."); to the initialize method.

using System;
using SDG.Framework.Modules;

namespace UnturnedModule
{
    public class Main : IModuleNexus
    {
        public void initialize()
        {
             Console.WriteLine("Tutorial code was loaded, yay.");
        }

        public void shutdown()
        {

        }
    }
}

8. Testing Your Module

In Visual Studio, navigate to ‘Build’ and click ‘Build Solution.’

Access your project folder and open the ‘bin/Debug’ folder. Copy all the file within that folder (except the .pdb) to a new folder named after your project in the Unturned/Modules folder of your Unturned server app (U3DS).

Launch Unturned and your console line should be printed.

image

5 Likes

Modules are just super duper turbo modding :terry: wow great tutorial!

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.