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

From RimWorld Wiki
Jump to navigation Jump to search
m (, to ;)
(Added more in-depth info on each step)
Line 8: Line 8:
 
## Enter a project name (solution name automatically updated);
 
## Enter a project name (solution name automatically updated);
 
## Choose a location, preferably:<br/><pre>(RimWorldInstallFolder)/Mods/(YourModName)/Source</pre>
 
## Choose a location, preferably:<br/><pre>(RimWorldInstallFolder)/Mods/(YourModName)/Source</pre>
## Untick "Create a directory for solution"/"Create a project within the solution directory".
+
## Untick "Create a directory for solution"/"Create a project within the solution directory",
# 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>
+
# In your project, add references to Assembly-CSharp.dll and UnityEngine.dll:
# In your project properties, change the target framework to .NET 3.5;
+
## Copy these files:<br/><pre>(RimWorldInstallFolder)/RimWorld_Data/Managed/Assembly-CSharp.dll&#10;(RimWorldInstallFolder)/RimWorld_Data/Managed/UnityEngine.dll</pre>
# Create a new class in a new code file;
+
## Place them in your project:<br/><pre>(RimWorldInstallFolder)/Mods/(YourModName)/Source/(YourSolutionName)/Source-DLLs</pre>
# 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>
+
## In your IDE project file browser, right-click the "References" folder and "Add reference";
 +
## Choose the ".NET Assembly Browser" tab and "Browse", go to the folder you placed the copied DLLs;
 +
## Choose both DLLs and click OK,
 +
# In your project properties, change the target framework to .NET 3.5:
 +
## In your IDE project file browser, right-click Solution (YourSolutionName);
 +
## Choose Properties;
 +
## Go to the "Compiling" tab, "Output", "Target framework", "Change" and choose ".NET Framework 3.5",
 +
# Create a new class in a new code file:
 +
## In your IDE project file browser, right-click (YourProjectName), Add -> New item -> C# or .NET -> Class;
 +
## Rename the class to what you want the class name to be, e.g DamageWorker_FlameExtension,
 +
# You’ll want to add these namespace to the top of each of your .cs 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>
 
# Write your class;
 
# Write your class;
 
## [[Modding Tutorials/Decompiling source code|Decompile source code]] to take a look at the game's existing code;
 
## [[Modding Tutorials/Decompiling source code|Decompile source code]] to take a look at the game's existing code;
Line 18: Line 28:
 
# Compile your class into a .dll;
 
# Compile your class into a .dll;
 
## Make sure your project's output type is "class library";
 
## Make sure your project's output type is "class library";
## '' '''Note:''' 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,''
+
## '' '''Note:''' 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'',
 
# Place the .dll in the YourModName/Assemblies folder of your mod;
 
# Place the .dll in the YourModName/Assemblies folder of your mod;
 
# Reference the classes in your .dll from the xml data in the YourModName/Defs folder;
 
# Reference the classes in your .dll from the xml data in the YourModName/Defs folder;

Revision as of 08:58, 22 August 2015

Modding Tutorials

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 IDE of choice;
    1. Go to File -> New -> Solution;
    2. Go to C# or .NET -> Library or Class Library (NOT portable);
    3. Enter a project name (solution name automatically updated);
    4. Choose a location, preferably:
      (RimWorldInstallFolder)/Mods/(YourModName)/Source
    5. Untick "Create a directory for solution"/"Create a project within the solution directory",
  2. In your project, add references to Assembly-CSharp.dll and UnityEngine.dll:
    1. Copy these files:
      (RimWorldInstallFolder)/RimWorld_Data/Managed/Assembly-CSharp.dll
      (RimWorldInstallFolder)/RimWorld_Data/Managed/UnityEngine.dll
    2. Place them in your project:
      (RimWorldInstallFolder)/Mods/(YourModName)/Source/(YourSolutionName)/Source-DLLs
    3. In your IDE project file browser, right-click the "References" folder and "Add reference";
    4. Choose the ".NET Assembly Browser" tab and "Browse", go to the folder you placed the copied DLLs;
    5. Choose both DLLs and click OK,
  3. In your project properties, change the target framework to .NET 3.5:
    1. In your IDE project file browser, right-click Solution (YourSolutionName);
    2. Choose Properties;
    3. Go to the "Compiling" tab, "Output", "Target framework", "Change" and choose ".NET Framework 3.5",
  4. Create a new class in a new code file:
    1. In your IDE project file browser, right-click (YourProjectName), Add -> New item -> C# or .NET -> Class;
    2. Rename the class to what you want the class name to be, e.g DamageWorker_FlameExtension,
  5. You’ll want to add these namespace to the top of each of your .cs 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;
    1. Decompile source code to take a look at the game's existing code;
    2. If you still get stuck on anything, any modding questions can be asked on the subforum,
  7. Compile your class into a .dll;
    1. Make sure your project's output type is "class library";
    2. Note: 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 YourModName/Assemblies folder of your mod;
  9. Reference the classes in your .dll from the xml data in the YourModName/Defs folder;
    1. Example: Create a new ThingDef with a <thingClass> that points to a class in your .dll,
  10. The game should load your class now;
  11. Optional: Release your source code.
    1. Most mods include a YourModName/Source folder with the full C# project in it for other modders to check out;
    2. Some mods include a Dropbox download link to their source code in the mod's topic. This is also a possibility.

See also