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.<br/><br/> |
− | |||
− | |||
=Requirements= | =Requirements= | ||
Line 11: | Line 9: | ||
=Creating a class= | =Creating a class= | ||
− | + | <!-- ### 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 78: | ||
</source><br/> | </source><br/> | ||
− | You can set your project's namespace by | + | 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/> |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
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 101: | ||
! Namespace !! What does it do? | ! Namespace !! What does it do? | ||
|- | |- | ||
− | | RimWorld || Many | + | | RimWorld || Many classes can be found in this namespace. |
|- | |- | ||
− | | | + | | RimWorld.Planet || Everything (almost everything) having to do with the planet savefile is in here. |
|- | |- | ||
− | | RimWorld. | + | | RimWorld.SquadAI || Holds the squad AI, clearly. You'll know it when you need this. |
|- | |- | ||
− | | | + | | Verse || Sometimes more complex classes are found here. |
|- | |- | ||
− | | Verse.AI || | + | | Verse.AI || You might never have to touch this either. |
− | |||
− | |||
|- | |- | ||
| 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 115: | ||
| 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 | + | | UnityEngine || Contains many more methods you most likely won't need. |
|- | |- | ||
|}<br/> | |}<br/> | ||
− | The difference between RimWorld and Verse is | + | 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 156: | Line 163: | ||
## If you get stuck on anything, any modding questions can be asked on the [https://ludeon.com/forums/index.php?board=14.0 subforum], | ## If you get stuck on anything, any modding questions can be asked on the [https://ludeon.com/forums/index.php?board=14.0 subforum], | ||
# Compile your class into a .dll; | # Compile your class into a .dll; | ||
− | ## Most IDE's, integrated development environments, have a button that says "Compile" or | + | ## Most IDE's, integrated development environments, have a button that says "Compile" or you could press F5 or a similar hotkey. Compiling is entirely driven by the software, you only have to supply a location for it to compile to. |
## 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 | + | # Reference the classes in your .dll from the xml data in the MyModName/Defs folder; |
− | + | ## '''Example:''' Create a new ThingDef with a <thingClass> that points to a class in your .dll, | |
− | |||
− | ## '''Example:''' Create a new ThingDef with a <thingClass> that points to a class in your .dll | ||
# The game should load your class now;<br/><br/> | # The game should load your class now;<br/><br/> | ||
− | = | + | =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]] |