Skip to content

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):

  1. Get started and Syntax
  2. Output and Comments
  3. Variables and Format specifiers
  4. Data types and Constants
  5. Operators and Booleans
  6. If … else and Switch
  7. While loop and For loop
  8. Arrays
  9. Strings and String functions
  10. Functions and Function parameters

For mods (the parts the API uses a lot):

  1. Scope and Storage classes: what static means
  2. Memory address and Pointers
  3. Structs and Struct pointers
  4. Typedef and Enums
  5. Function pointers and Callback functions
  6. Memory management, Allocate memory and Deallocate memory
  7. NULL and Fixed-width integers
  8. 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, bool and char,
  • write if, else, for and while,
  • write a function that takes parameters and returns a value,
  • keep text in a char name[64] array and format it with snprintf,
  • 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 static does 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 own data/ folder. Memory in the sandbox explains it.
  • main: a mod has no main function. 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.