Making your first Mod - HueSamai/CementSource GitHub Wiki

Before reading this page you are going to want to first read the wiki page on setting up a mod project.

If you've already read that, then you're good to go and to begin programming!

NOTE: nothing explained in the wiki page on setting up a modding environment will be explained in full detail on this page. So if you have forgotten how to do something, go back to that wiki page.

Explanation of how code gets loaded into Gang Beasts

The programming language we are going to be using is C# (pronounced CSharp, not C hashtag). C# code is stored in files with the .cs file extension. These files contain human readable versions of our code, but in order for the computer to understand and run the code we need to compile it. Compiling is the process of turning human readable code, into machine code (code that the computer can actually understand). We also want Cement to be able to load our code, so we compile it into a Dynamiced Linked Library or DLL for short. DLL's are just another file type (with the .DLL file extension) which store code, but they can't execute code on their own and need to be loaded in by an executable, such as Gang Beasts, which is where Cement comes in.

Luckily, all of the compiling of our code is handled for us by Visual Studio, using a C# compiler (a program that compiles C# code), and the loading of the code into Gang Beasts, is handled by Cement. So all we have to do, is actually code our mod.

Coding our mod

Opening a C# file in the editor

A C# file (.cs file) is created for us automatically by Visual Studio when we create a new project, called Class1.cs. You probably noticed this C# file while creating all the other files. In order to start coding, we need to open up this file in the editor.

In the solution explorer, find the C# file you want to open (it will be under one of your projects). image

Double click on it to open it in the editor.

Your screen should look something similar to this: image

Explanation of the structure of C#

Before beginning, there are just a few terms I want to explain. The first is a keyword. A keyword is a word reserved by a programming language that has a special meaning.

In C#, we organise code in namespaces. Namespaces contain classes, which we'll get to in a second. We divide classes into namespaces so that we can tell the compiler which bits of libraries or code our program uses to function. This is where the using keyword comes in.

When you create a new C# file you will see at the beginning it will have one or more using statements. Mine has just one:

using System;

Using statements, always come first in our C# file, above anything else.

This just tells the compiler that we want to use classes that are found in the System namespace. If we didn't include this at the top, we wouldn't be able to use classes from there, and if we tried to we would get an error. We refer to this as including a namespace.

We include a namespace by typing using, followed by the name of the namespace we want to include, in this case System, and then we put a semi-colon at the end to let the compiler know this is the end of a statement.

IMPORTANT: ALL statements in C# end with a semi-colon. I will touch on this later again.

We are going to have to tell the compiler we want to include the CementTools namespace, so that we can have access to the classes in Cement, which will allow us to program mods.

First try and do this yourself, and then check back to see if you could figure it out on your own.

You can include the CementTools namespace by typing in the line after the first using statement the following line of code:

using CementTools;

Now after the using statements, comes a namespace definition. This is were we can define our own namespaces which other people can include in their code.

NOTE: You don't have to define classes in namespaces. You can define a class outside of a namespace, and then the class is global, and if people are using a DLL, which contains your class, they can write code using it, without including a namespace (putting a using statement at the top).

You will see that a namespace is defined by typing the keyword namespace, followed by what we want to call our namespace. By default Visual Studio calls the namespace the name of your project, so in my case TutorialMod.

NOTE: Names in C# can't have spaces in them, or any other weird characters. Names can include numbers (although they can't start with them), letters, and underscores. That is why you'll see why people capitalise the start of every word in a name, because they can't use spaces, and this maintains readability. It is good practice for you to use this convention too.

Then after the name you will see an open curly bracket {. This is how we tell the compiler where are namespace definition starts. At the bottom of your C# file, you will see a closed curly bracket }, which tells the compiler this is where our namespace ends. Open and closed curly brackets, are used in C# to tell the compiler where different sections of code begins and ends. For instance, later on when we start coding if statements, where we want to only execute some code if a condition is met, we will use curly brackets to show which code is inside of our if statement (which code we want to execute if our condition is met).

NOTE: whenever there is an open curly bracket, it must always be closed somewhere.

All of class definitions written inside the curly brackets of our namespace will be a part of that namespace.

Then on the inside of the namespace definition, you will see a class definition. It is similar to the namespace definition, except it has the class keyword, followed by the name we want to give the class, and then the curly brackets.

You will also see that the class definition is indented (it is shifted a bit to the right, of where the lines of a file usually start). This helps us see that the class definition is part of our namespace definition. When we start writing code inside this class definition, we will also indent it even more to show that the code we write is part of that class, and not our namespace. This indentation isn't required by C# to compile correctly, but it helps A LOT with readability, and is not just good practice in coding, but is almost essential to writing good code.

We can create an indent by pressing tab, and we can delete an indent by pressing backspace.

Now let's move on to class definitions. Firstly, we need to talk about what an object is. In programming an object stores data in fields or variables and has methods which contain programming instructions. Methods can take inputs and output a value, and just fulfill a small task. Classes definitions are blueprints for objects. When we make an object, we tell the compiler which class we want to use as a blueprint. Class definitions therefore contain the different values we want to store, as well as methods which we code instructions in, to, for instance, spawn a new object, change the colour of a beast, or to do whatever else you can think of.