Difference between revisions of "Modding Tutorials/Writing custom code"

From RimWorld Wiki
Jump to navigation Jump to search
(Added text about the project having to be a "class library" to compile it into a .dll file.)
(Changed from actual numbers to a #numbered list, changed from space before text to <pre> and added 6. which elaborates on writing your class)
Line 1: Line 1:
 
In addition to creating data for the game to use, you can also write code. You could probably write in any .NET language, but I’ve only tested C#.
 
In addition to creating data for the game to use, you can also write code. You could probably write in any .NET language, but I’ve only tested C#.
  
1. Create a new class library project in your code editor of choice. I use Visual Studio, but MonoDevelop also works well.
+
# Create a new class library project in your code editor of choice. I use Visual Studio, but MonoDevelop also works well.
 
+
# In your project, add references to these DLLs:<br/><pre>(RimWorldInstallFolder)/RimWorld_Data/Managed/Assembly-CSharp.dll&#10;(RimWorldInstallFolder)/RimWorld_Data/Managed/UnityEngine.dll</pre>
2. In your project, add references to these DLLs:
+
# In your project properties, change the target framework to .NET 3.5
 
+
# Create a new class in a new code file.
(RimWorldInstallFolder)/RimWorld_Data/Managed/Assembly-CSharp.dll
+
# You’ll want to add these namespace to each of your source files as necessary.<br/><pre>using UnityEngine;  //For all Unity functionality, rendering, resource management&#10;using AI; //RimWorld AI&#10;using Sound;         //RimWorld sound subsystem&#10;using UI; //RimWorld GUI&#10;</pre>
(RimWorldInstallFolder)/RimWorld_Data/Managed/UnityEngine.dll
+
# Write your class. Use [http://ilspy.net/ ILSpy] (Download Binaries) to open Assembly-CSharp.dll, which decompiles the game's source code. Any modding questions can be asked on the [https://ludeon.com/forums/index.php?board=14.0 subforum].
 
+
# Compile your class into a .dll. Make sure your project's output type is "class library".<br/>''Note that by default, Visual Studio will compile all the references of the project as well, so you’ll get a copy of UnityEngine.dll and Assembly-CSharp.dll and some others. You don’t need these. Just take YourModName.dll.''
3. In your project properties, change the target framework to .NET 3.5
+
# Place the .dll in the YourModeName/Assemblies folder of your mod.
 
+
# Reference the classes in your .dll from the xml data in the YourModName/Defs folder. For example, you could create a new ThingDef with a <thingClass> that points to a class in your .dll.
4. Create a new class in a new code file.
+
# The game should load your class now.
 
+
# If you wish, you should also release your source code in the YourModName/Source directory.
5. You’ll want to add these namespace to each of your source files as necessary.
 
 
 
using UnityEngine;  //For all Unity functionality, rendering, resource management
 
using AI; //RimWorld AI
 
using Sound;         //RimWorld sound subsystem
 
using UI; //RimWorld GUI
 
 
 
6. Write your class and compile it into a .dll. Make sure your project's output type is "class library". Note that by default, Visual Studio will compile all the references of the project as well, so you’ll get a copy of UnityEngine.dll and Assembly-CSharp.dll and some others. You don’t need these. Just take YourModName.dll.
 
 
 
7. Place the .dll in the YourModeName/Assemblies folder of your mod.
 
 
 
8. Reference the classes in your .dll from the xml data in the YourModName/Defs folder. For example, you could create a new ThingDef with a <thingClass> that points to a class in your .dll.
 
 
 
9. The game should load your class now.
 
 
 
10. If you wish, you should also release your source code in the YourModName/Source directory.
 
  
  
 
You can find a small tutorial project here: [[Modding Tutorials/Assembly Modding Example]]
 
You can find a small tutorial project here: [[Modding Tutorials/Assembly Modding Example]]

Revision as of 07:53, 15 June 2015

In addition to creating data for the game to use, you can also write code. You could probably write in any .NET language, but I’ve only tested C#.

  1. Create a new class library project in your code editor of choice. I use Visual Studio, but MonoDevelop also works well.
  2. In your project, add references to these DLLs:
    (RimWorldInstallFolder)/RimWorld_Data/Managed/Assembly-CSharp.dll
    (RimWorldInstallFolder)/RimWorld_Data/Managed/UnityEngine.dll
  3. In your project properties, change the target framework to .NET 3.5
  4. Create a new class in a new code file.
  5. You’ll want to add these namespace to each of your source files as necessary.
    using UnityEngine;  	//For all Unity functionality, rendering, resource management
    using AI;		//RimWorld AI
    using Sound;	        //RimWorld sound subsystem
    using UI;		//RimWorld GUI
    
  6. Write your class. Use ILSpy (Download Binaries) to open Assembly-CSharp.dll, which decompiles the game's source code. Any modding questions can be asked on the subforum.
  7. Compile your class into a .dll. Make sure your project's output type is "class library".
    Note that by default, Visual Studio will compile all the references of the project as well, so you’ll get a copy of UnityEngine.dll and Assembly-CSharp.dll and some others. You don’t need these. Just take YourModName.dll.
  8. Place the .dll in the YourModeName/Assemblies folder of your mod.
  9. Reference the classes in your .dll from the xml data in the YourModName/Defs folder. For example, you could create a new ThingDef with a <thingClass> that points to a class in your .dll.
  10. The game should load your class now.
  11. If you wish, you should also release your source code in the YourModName/Source directory.


You can find a small tutorial project here: Modding Tutorials/Assembly Modding Example