How to make mods (native modding system)

## Part I. Visual Studio Installation.

1. Download Visual Studio Community 2019.
2. Run downloaded file.
3. After all preparations have done, you need to select .NET desktop development, and select checkboxes in right side menu: .NET Framework 4.6.1 development tools, .NET Framework 4.6.2 development tools, .NET Framework 4.7 development tools, .NET Framework 4.7.1 development tools, .NET Framework 4.8 development tools. Honestly, you dont need them all, but just in case select them all. After all selecting, you need just to click on Install button in the right down corner.
4. Wait until Visual Studio will be installed.
5. Done! Now you have installed Visual Studio Community 2019!

## Part II. Creating new project.

### Creating project:

1. Open Visual Studio.
2. Click on Create a new project button.
3. Now you need to select Class library (.NET Framework). EXACTLY Class library (.NET Framework)!!!
4. Fill Project name field with your mod name. Do not use spaces, use camel case instead.
5. Click Create button in the right down corner.
6. Done! Project created.

### Preparations before modding:

1. After project will be created, you will see something like this:
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
 public class Class1
 {

 }
}

```
2. Now you need to find Solution Explorer in right side, right click on your project name and then click on Open Folder in File Explorer.
3. In opened folder you need to create new folder with name Libs (or something like this).
4. Go to your WorldBox folder (where worldbox.exe is), and then go to /worldbox\_Data/Managed;
5. Copy from Managed folder files: Assembly-CSharp.dll, UnityEngine.dll and UnityEngine.CoreModule.dll. Paste them into yourProject/Libs folder
6. Now go to Visual Studio, in Solution Explorer right click on References > Add Reference...
7. Click Browse, and in opened window select files in yourProject/Libs folder, click Add, click Ok.
8. Now you need to change code of your file. It should looks like it:
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace ExampleMod
{
 public class WorldBoxMod : MonoBehaviour
 {
 static bool initialized = false;

 void Update()
 {
 if (!Config.gameLoaded) return;

 if (!initialized)
 {
 init();
 }

 initialized = true;

 }

 void init()
 {
 //your code here
 }
 }
}

```
There is added using UnityEngine; in the head of file, added initialized boolean variable, which will be true after game will be loaded and all stuff will be initialized, added Update method which calls ~60 times per second, and this is a part of unity game cycle. All your code, or other methods calls must be inside init method. It is not necessary, and depends of what you want to make it can be other, but in most of cases this is default starter code. Namespace must be the same as mod name as result dll file name. Class name must be WorldBoxMod : MonoBehaviour because it is necessary for WorldBox native modding system. Do not change class name!
9. All preparations done! Now you can start create your own mod!
10. To see what you can change, or just to explore game code, you need to disassemble Assembly-CSharp.dll. You can do it using JetBrains dotpeek or dnSpy. You can use google to know how to use them.
11. Also, i recommend to you to learn c sharp, at least basics before you will start modding, because it can close many questions for you.

## Part III. Modding

### Example mod:

Let make new mod, where we will change giant trait, to make creatures that have it much more size and speed then with vanilla giant trait. And we will make that all races will have this trait by default, right from spawning/birth.

As i said before, you can explore game code by disassembling Assembly-CSharp.dll using JetBrains dotpeek or dnSpy.
Example of editing trait:

```
void init()
{
 var giantTrait = AssetManager.traits.get("giant");
 giantTrait.baseStats.scale = 0.50f; // default is 0.05f
}

```
There we:

1. Received trait from asset manager by its id: var giantTrait = AssetManager.traits.get("giant");
2. Changed scale factor from 0.05 to 0.50 (multiplied by ten times!): giantTrait.baseStats.scale = 0.50f;

Yes, we changed our trait, but how we can add it to our creatures(all races at this case)?

```
void init()
 {
 var giantTrait = AssetManager.traits.get("giant");
 giantTrait.baseStats.scale = 0.50f; // default is 0.05f

 var human = AssetManager.unitStats.get("unit\_human");
 human.traits.Add("giant");
 human.traits.Add("slow");

 var elf = AssetManager.unitStats.get("unit\_elf");
 elf.traits.Add("giant");
 elf.traits.Add("slow");

 var orc = AssetManager.unitStats.get("unit\_orc");
 orc.traits.Add("giant");
 orc.traits.Add("slow");

 var dwarf = AssetManager.unitStats.get("unit\_dwarf");
 dwarf.traits.Add("giant");
 dwarf.traits.Add("slow");
 }

```
Right after trait changes we also add changes in creatures assets. There we:

1. Received unit asset using asset manager: var human = AssetManager.unitStats.get("unit\_human");
2. Added giant trait: human.traits.Add("giant");
3. Added slow trait, cuz we want to make out giants slow: human.traits.Add("slow");
4. And we did the same with all four civilized races.

Done! Our code ready, but now we want to build a dll and test our mod.

1. In top panel go to Build > Build Solution (F6 by default).
2. Go to yourProjectName/bin/Debug (ExampleMod/bin/Debug) in our case, find there yourProjectName.dll (ExampleMod.dll in our case)
3. Copy yourProjectName.dll, go to WorldBox folder (where worldbox.exe is), go to path /worldbox\_Data/StreamingAssets/Mods and paste yourProjectName.dll there.
4. Run the game
5. Try to spawn any civilized creatures(humans/elves/orcs/dwarfs);
6. Done! Now you will see that they are too big, and a little bit slow.
7. If you doesnt see any changes, try to run your game as administrator

There is full code:

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace ExampleMod
{
 public class WorldBoxMod : MonoBehaviour
 {
 static bool initialized = false;

 void Update()
 {
 if (!Config.gameLoaded) return;

 if (!initialized)
 {
 init();
 }

 initialized = true;

 }

 void init()
 {
 var giantTrait = AssetManager.traits.get("giant");
 giantTrait.baseStats.scale = 0.50f; // default is 0.05f

 var human = AssetManager.unitStats.get("unit\_human");
 human.traits.Add("giant");
 human.traits.Add("slow");

 var elf = AssetManager.unitStats.get("unit\_elf");
 elf.traits.Add("giant");
 elf.traits.Add("slow");

 var orc = AssetManager.unitStats.get("unit\_orc");
 orc.traits.Add("giant");
 orc.traits.Add("slow");

 var dwarf = AssetManager.unitStats.get("unit\_dwarf");
 dwarf.traits.Add("giant");
 dwarf.traits.Add("slow");
 }
 }
}

```