The C you need
Lesson 2.1
You don’t have to be a C expert to write a good mod. Most mods use a small part of the language: functions, a few variables, if and loops, strings in char arrays, and structs. This lesson lists what to learn and where to learn it.
Learn the language on W3Schools
W3Schools has a free C tutorial with short pages and an editor in the browser. Read these pages in this order, and do the little exercises at the end of each one. The first group is enough for your first mod; the second one is needed once your mod starts reacting to events.
The basics (a few evenings):
- Get started and Syntax
- Output and Comments
- Variables and Format specifiers
- Data types and Constants
- Operators and Booleans
- If … else and Switch
- While loop and For loop
- Arrays
- Strings and String functions
- Functions and Function parameters
For mods (the parts the API uses a lot):
- Scope and Storage classes: what
staticmeans - Memory address and Pointers
- Structs and Struct pointers
- Typedef and Enums
- Function pointers and Callback functions
- Memory management, Allocate memory and Deallocate memory
- NULL and Fixed-width integers
- Macros and Organizing code
Later, when you need them: Error handling, Debugging, Random numbers, Math functions and the string.h reference.
The minimum, in one list
Before you write a mod, you should be able to read and write code like this without looking things up:
- declare variables of type
int,float,double,boolandchar, - write
if,else,forandwhile, - write a function that takes parameters and returns a value,
- keep text in a
char name[64]array and format it withsnprintf, - use a struct and set its fields with
.(and with->through a pointer), - pass a function’s name to another function (a callback),
- know what
staticdoes to a variable outside a function.
The last two are the important ones for mods. The game calls your functions when something happens, and those functions keep their state in static variables between calls.
Types you will see everywhere
The API uses the fixed-width types from <stdint.h>: int32_t is a 32-bit whole number, uint32_t the same without a sign, int64_t a 64-bit one. They work exactly like int, but their size is the same everywhere. size_t is used for lengths. bool comes from <stdbool.h>. You don’t need to include these yourself; the SDK’s header does it for you.
C you won’t need
Some things from W3Schools don’t apply in a mod, so don’t worry about them:
- User input (
scanf): a mod gets its input from the game, not from a terminal. - Files (
fopen): a mod may only read its own folder and write its owndata/folder. Memory in the sandbox explains it. main: a mod has nomainfunction. It has an entry function that the game calls once.- Threads: the game calls your code; you never start threads yourself.
Try it
Without a computer: what does this print, and why does it print it?
#include <stdio.h>
static int count_calls(void) {
static int calls = 0;
calls = calls + 1;
return calls;
}
int main(void) {
count_calls();
count_calls();
printf("%d\n", count_calls());
return 0;
}
Show a solution
It prints 3. The static inside the function means calls is created once and keeps its value between calls, instead of starting at 0 every time. Mods use this all the time: a handler that counts joins or remembers the last player keeps its value in a static variable. If this was new to you, read Storage classes on W3Schools.
Recap: learn the basics, pointers, structs and callbacks on W3Schools first; a mod needs functions, static variables, char arrays, structs and callbacks, and no main.