Editing Modding Tutorials/Writing custom code

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
 
{{BackToTutorials}}
 
{{BackToTutorials}}
<br/>
+
{{Credit|[[User:TheTynan|TheTynan]]|extra=1}}<br/>
  
 
This tutorial gives you a broad idea how to work on a C# solution.
 
This tutorial gives you a broad idea how to work on a C# solution.
Line 11: Line 11:
  
 
=Creating a class=
 
=Creating a class=
Once you've [[Modding Tutorials/Setting up a solution|set up your solution]] correctly you'll usually be greeted with a class similar like the template below:
+
<!--  ### 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/>-->
 +
 
 +
===Sharpdevelop===
 +
 
 +
When you start your software you'll see a '''Projects''' file explorer to the left, a '''Properties''' explorer to the right and currently opened files in the middle.
 +
 
 +
* With your solution open, create a new class inside your '''Projects''' explorer;
 +
*# If you don't have [[Modding Tutorials/Setting up a solution|your solution]] open you can do so by clicking File -> Open -> Project/Solution.. or by choosing a recent project on the '''Start page''' in the middle;
 +
*# If it's not expanded, click the + next to "Solution ''MySolutionName''". Right-click ''MyProjectName'' (this could be the same as ''MySolutionName'' or different, depending on how you set up the solution);
 +
*# Select Add -> New Item.. -> '''Categories:''' C# -> '''Templates:''' Class -> '''File Name:''' ''MyClassName''.cs,
 +
 
 
====Template====
 
====Template====
  
Line 68: Line 80:
 
</source><br/>
 
</source><br/>
  
You can set your project's namespace by going to visual studio and right-clicking MyProjectName -> Properties -> Application -> Root namespace(Visual Studio 2019 it is Default namespace). Please take some time to make it unique so no compatibility issues will arise with other mods, for your and everyone else's sanity.<br/><br/>
+
You can set your project's namespace by right-clicking MyProjectName -> Properties -> Application -> Root namespace. Please take some time to make it unique so no compatibility issues will arise with other mods, for your and everyone else's sanity.<br/><br/>
 
 
====Summary====
 
 
 
This summary is part of the template but it's not required. It can help other people understand your code better when you provide the source but besides that you really don't need it.<br/><br/>
 
 
 
====Class and Constructor====
 
 
 
This is your class definition. To access anything inside of a ''public class'' you will need a reference to an instance of that class:<br/>
 
 
 
<source lang="csharp">
 
MyClassNamefoo = new MyClassName();
 
</source><br/>
 
 
 
The brackets at ''new MyClassName()'' indicate you're creating this instance without ''parameters'' which is possible because your class contains a constructor (anything inside of ''class MyClassName'' which has parameters and isn't a method, often abbreviated '''.ctor''') which accepts a call without parameters:<br/>
 
 
 
<source lang="csharp">
 
public class MyClassName
 
{
 
public MyClassName() /* No parameters */
 
{
 
}
 
 
 
public MyClassName(string foo, int bar) /* Required parameters of datatypes string and int */
 
{
 
}
 
 
 
public MyClassName(string foo = "none supplied", int bar = 42) /* Default parameters " " " */
 
{
 
}
 
}</source><br/>
 
 
 
The last implementation of the constructor (''MyNameSpace.MyClassName.MyClassName'' inside of ''MyNameSpace.MyClassName'') is capable of accepting zero to two parameters while the second implementation of the constructor will only accept calls with exactly two parameters.<br/><br/>
 
  
=Namespaces=
 
 
Some very useful namespaces in general are the following:<br/><br/>
 
Some very useful namespaces in general are the following:<br/><br/>
  
Line 124: Line 103:
 
! Namespace !! What does it do?
 
! Namespace !! What does it do?
 
|-
 
|-
| RimWorld || Many RimWorld specific classes can be found in this namespace. This and the Verse namespace will be most used.
+
| RimWorld || Many classes can be found in this namespace.
 
|-
 
|-
| Verse || Many general use classes can be found in this namespace. This and the RimWorld namespace will be most used.
+
| RimWorld.Planet || Everything (almost everything) having to do with the planet savefile is in here.
 
|-
 
|-
| RimWorld.BaseGen || Everything that creates the layout and contents of the Outposts, Bases and Settlements is in here.
+
| RimWorld.SquadAI || Holds the squad AI, clearly. You'll know it when you need this.
 
|-
 
|-
| RimWorld.Planet || Everything (almost everything) having to do with the planet is in here.
+
| Verse || Sometimes more complex classes are found here.
 
|-
 
|-
| Verse.AI || Holds the Jobs and general AI. Broadly speaking, what pawns do.
+
| Verse.AI || You might never have to touch this either.
|-
 
| Verse.AI.Group || Holds the squad AI. This is useful when you want 2 or more pawns to work together.
 
 
|-
 
|-
 
| Verse.Grammar || Contains functionality for the Languages folder and other things having to do with text operations such as the art flavour text generator.
 
| Verse.Grammar || Contains functionality for the Languages folder and other things having to do with text operations such as the art flavour text generator.
Line 140: Line 117:
 
| Verse.Noise || Something something you'll never need this.
 
| Verse.Noise || Something something you'll never need this.
 
|-
 
|-
| Verse.Sound || Much like Verse.Noise.
+
| Verse.Sound || Much like Verse.Noise except it's required to play sounds directly with your code.
 
|-
 
|-
| Verse.Steam || Steam integration by Tynan - you won't need it.
+
| Verse.Steam || Steam integration by Tynan - it's not active so you won't need it.
 
|-
 
|-
| UnityEngine || Contains many more methods you most likely won't actively use. RimWorld mostly uses it for the GUI, Rect and Color methods.
+
| UnityEngine || Contains many more methods you most likely won't need.
 
|-
 
|-
 
|}<br/>
 
|}<br/>
  
The difference between RimWorld and Verse is sometimes blurred. "RimWorld" contains things which are reasonably specific to RimWorld functionality. Classes in Verse are more oriented towards general game functionality. Since it can be difficult to guess which namespace holds a class, you might as well add both using statements whenever you want.<br/>
+
The difference between RimWorld and Verse is not very clear and you might as well add both using statements whenever you want.<br/>
 
If you want to use the exact functionality of a base game class you're best off copying all its ''using'' statements, its ''namespace'' and the namespace of its parent.<br/><br/>
 
If you want to use the exact functionality of a base game class you're best off copying all its ''using'' statements, its ''namespace'' and the namespace of its parent.<br/><br/>
 +
 +
====Summary====
 +
 +
This summary is part of the template but it's not required. It can help other people understand your code better when you provide the source but besides that you really don't need it.<br/><br/>
 +
 +
====Class and Constructor====
 +
 +
This is your class definition. To access anything inside of a ''public class'' you will need a reference to an instance of that class:<br/>
 +
 +
<source lang="csharp">
 +
MyClassNamefoo = new MyClassName();
 +
</source><br/>
 +
 +
The brackets at ''new MyClassName()'' indicate you're creating this instance without ''parameters'' which is possible because your class contains a constructor (anything inside of ''class MyClassName'' which has parameters and isn't a method, often abbreviated '''.ctor''') which accepts a call without parameters:<br/>
 +
 +
<source lang="csharp">
 +
public class MyClassName
 +
{
 +
public MyClassName() /* No parameters */
 +
{
 +
}
 +
 +
public MyClassName(string foo, int bar) /* Required parameters of datatypes string and int */
 +
{
 +
}
 +
 +
public MyClassName(string foo = "none supplied", int bar = 42) /* Default parameters " " " */
 +
{
 +
}
 +
}</source><br/>
 +
 +
The last implementation of the constructor (''MyNameSpace.MyClassName.MyClassName'' inside of ''MyNameSpace.MyClassName'') is capable of accepting zero to two parameters while the second implementation of the constructor will only accept calls with exactly two parameters.<br/><br/>
  
 
=Writing code=
 
=Writing code=
Line 159: Line 168:
 
## 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. Just take ''MyModName.dll'' and place it in the ''MyModName/Assemblies'' folder. If you have [[Modding Tutorials/Setting up a solution|set up a solution]] according to the tutorial you don't have this problem,
 
## '' '''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. Just take ''MyModName.dll'' and place it in the ''MyModName/Assemblies'' folder. If you have [[Modding Tutorials/Setting up a solution|set up a solution]] according to the tutorial you don't have this problem,
# Reference the classes in your .dll (optionally: from the xml data in the MyModName/Defs folder);
+
# Reference the classes in your .dll from the xml data in the MyModName/Defs folder;
#* {{Main|Modding Tutorials/Linking XML and C#}}
+
## '''Example:''' Create a new ThingDef with a <thingClass> that points to a class in your .dll (see [[Modding_Tutorials/Def classes|Modding_Tutorials/Def classes]] and [[Modding_Tutorials/Modifying defs|Modding_Tutorials/Modifying defs]])
## '''Example:''' Write a [[Modding_Tutorials/Hello_World|Hello World]] mod
 
## '''Example:''' Create a new ThingDef with a <thingClass> that points to a class in your .dll (see, for example, [[Modding_Tutorials/Def classes|Def classes]] and [[Modding_Tutorials/Modifying defs|Modifying defs]])
 
 
# The game should load your class now;<br/><br/>
 
# The game should load your class now;<br/><br/>
  
=Examples=
+
=See also=
* Write a [[Modding_Tutorials/Hello_World|Hello World]] mod: the simplest C# project.
 
* [[Modding Tutorials/Assembly Modding Example|Assembly modding example]] contains a small modding example.
 
* [[Plague_Gun_(1.1)|Plague Gun]] How to Make a RimWorld Mod - step by step guide by Jecrell.
 
  
=See also=
 
 
* [[Modding_Tutorials/Distribution|Distribution]] details how to release your mod.
 
* [[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]]

Please note that all contributions to RimWorld Wiki are considered to be released under the CC BY-SA 3.0 (see RimWorld Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)

This page is a member of 1 hidden category: