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

From RimWorld Wiki
Jump to navigation Jump to search
(Made more in line with other tutorials)
Line 1: Line 1:
 
{{BackToTutorials}}
 
{{BackToTutorials}}
 +
{{Credit|[[User:TheTynan|TheTynan]]}}<br/>
  
''This tutorial requires you to have [[Modding Tutorials/Setting up a solution|set up a solution]].''<br/><br/>
+
This tutorial gives you a broad idea how to work on a C# solution.<br/><br/>
  
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#.<br/><br/>
+
=Requirements=
 +
 
 +
* This tutorial requires you to have [[Modding Tutorials/Setting up a solution|set up a solution]].<br/><br/>
 +
 
 +
=Writing custom code=
 +
<!--  ### THE FOLLOWING TEXT was stripped from the page because it's not in line with the tone of the article
 +
 
 +
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#.<br/><br/>-->
  
 
# Create a new class in a new code file:
 
# Create a new class in a new code file:
Line 20: Line 28:
 
# The game should load your class now;<br/><br/>
 
# The game should load your class now;<br/><br/>
  
''When you are done with all of this, you might want to [[Modding Tutorials/Distribution|release your mod]].''<br/><br/>
+
=See also=
 
 
==See also==
 
  
* You can find a small tutorial project here: [[Modding Tutorials/Assembly Modding Example]]
+
* [[Modding_Tutorials/Distribution|Distribution]] details how to release your mod.
 +
* [[Modding Tutorials/Assembly Modding Example|Assembly modding example]] contains a small modding example.<br/>
  
  
 
[[Category:Modding tutorials]]
 
[[Category:Modding tutorials]]

Revision as of 10:44, 25 August 2015

Modding Tutorials

This page was originally created by TheTynan.

This tutorial gives you a broad idea how to work on a C# solution.

Requirements

Writing custom code

  1. 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,
  2. 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
    
  3. 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,
  4. 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,
  5. Place the .dll in the YourModName/Assemblies folder of your mod;
  6. 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,
  7. The game should load your class now;

See also