RAGE Against the Virtual Machine
In most games you have two different types of code, the engine code and the game code. The engine code is the heart of the game. This does all of the dirty work such as rendering, threading, I/O, memory management, calling the OS and third party libraries. You generally write this is a native compiled language and want this to be as fast as possible. In most game engines this is where you'll find weird hacks and ancient dark code written under a red moon that nobody today understands.
The "game code" is the logic of the actual game. What happens when you press this lever? How do the NPCs behave? If I jump off a cliff how much damage do I take? The priority with game code is flexibility. The logic that runs when you throw a rock at a pig doesn't need to be the most bare-metal optimized code, it's not running 10,000 times a second, it's just running every time someone throws a rock at a pig. For this, engines often sacrifice a bit of performance to make developer's lives easier.
This often takes the form of a scripting language, one of the more common being Lua. The idea behind using a scripting language is you can hot-reload them without recompiling the engine, they're a lot easier to write without worrying about lower level internals which in turn opens them up for more people on a team to work on without needing specialized engine knowledge. Scripting interacts with the game through an API layer, which reduces complexity further and also reduces the chance the game will just implode from a bad script. Instead of handling pointers or memory management, you get a nice SetHealth(50) function to call.
I Hardly Lua
Every iteration of my game until this year has included Lua as the scripting language. It always felt good enough, and I didn't really have any better ideas, but I was never married to it. It worked, I didn't really have a big problem I could point to, it just felt I was spending more time writing glue and working around some of the quirks, which I'd outsource to more engine code.
As an experiment, I decided to just delete Lua completely and shift to writing game code natively in C3. To start with, this felt good. Shackles free, I could just do whatever I wanted. The problem with game code is you end up laying a lot of it, and I started to miss some of that flexibility quickly. One of the most annoying things was waiting. If you interacted with a lever and something needed to happen after 5 seconds, that would look like this...
fn bool on_interact(uint db_id) {
if (db_id != g_lever_id)
return false;
pull_lever(db_id);
sequence::start(&lever_seq_tick);
return true;
}
fn void lever_seq_tick(Sequence* s, float dt) {
switch (s.step) {
case 0: if (!sequence::@wait(s, 5.0, dt)) return;
case 1: sequence::@action(s) { sfx::play_sound("file:data/sfx/quack.flac"); };
case 2: sequence::stop(s);
}
}
That's right, a good old-fashioned state machine. Does this work? Yes. But look at what we have to do for playing a sound 5 seconds after a lever is pulled, now imagine the spaghetti monster that turns into if we have a fully scripted sequence with hundreds of variables and events. The mental overhead grows exponentially. To be fair, doing this in Lua using coroutines didn't make me that much happier.
Flower in a Hurricane
In November, a small indie game called Grand Theft Auto 6 came out and it was all the RAGE (pun). Leading up and immediately after the launch my YouTube recommendations were solidly GTA related. Flicking through them one lunch time I saw a video about how NPC scripting works in RAGE (Rockstar's engine), so I thought that would be a fun watch.
The video touched a bit on how the scripting works, and while it didn't get super technical, it was enough to tickle my curiosity. Naturally, I dug in and started researching the topic.
RAGE (Rockstar Advanced Game Engine) uses a custom scripting language called SanScript (similar to BASIC). They're compiled to bytecode with static variables, a string heap and a table of engine functions stored as 64-bit hashes. The engine links the hashes to C++ function pointers when the script loads. To run the scripts, the engine uses a stack machine with opcodes. Each script is ran on it's own thread with it's own stack. No pointers and no memory allocation.
I didn't need a lot of convincing. This direction felt like the kind of solution I was looking for, and more than anything it stoked that fire that drives most of my decisions: "can I do that?"
Making Bacon
My goal with BACON (the engine is called HOG, so the scripts are called BACON) was to take that stack-based approach and bring it down to a level fitting my little game engine. The syntax is intentionally basic. In addition to the basics like INT, FLOAT, TRUE/FALSE and strings, it also integrates the custom mechanics of the engine such as file, animation and data table references as first class features without the dirty string-matching transpile step I had before.
VAR quack FILE "file:data/sfx/quack.flac" EVENT LOAD SET_PRESENCE "DEV_TINKERING" END EVENT INTERACT @MapObject$TestPathLever PLAY_ANIM @MapObject$TestPathLever "anim:data/models/lever.hmod/PullAndRelease" IF NPC_MOVE_TO @MapObject$TestTamara1 @NavNode$TestPath2 WAIT_UNTIL NOT NPC_IS_MOVING @MapObject$TestTamara1 LOG "tamara arrived at TestPath2" PLAY_SOUND quack END END EVENT INTERACT @MapObject$KruVonKronksLever PLAY_ANIM @MapObject$KruVonKronksLever "anim:data/models/lever.hmod/PullAndRelease" PLAY_SOUND quack IF DIALOG_START @DialogTree$lever_test WAIT_DIALOG LOG "lever dialog ended" END END EVENT DIALOG_HOOK @DialogNode$lever_test_quack PLAY_SOUND quack END EVENT TRIGGER_ENTER @WorldTrigger$shake_test SCREEN_SHAKE 0.15 20 2000 PLAY_SOUND quack WAIT 1000 PLAY_SOUND quack WAIT 1000 PLAY_SOUND quack END
Keeping the syntax super basic means the compilation is easy as well. It compiles with a single-pass lexer with just a single pre-pass to collect PROC signatures. There's no AST (Abstract Syntax Tree) step, it produces bytecode directly. There's a few compile-time checks to catch argument types, duplicate events and blocking natives.
For running it, it uses a stack-based VM with (currently) 50 opcodes. This covers push/load/store, int/float arithmetic, comparisons, jumps, calls, SPAWN/WAIT/YIELD/EXIT. One of the key benefits is that each script runs in it's own thread with it's own stack. This means any runtime faults only stops that thread, rather than crashing the engine. The biggest advantage by far is just how much cleaner waiting in a script works...
EVENT INTERACT @MapObject$Lever PLAY_ANIM @MapObject$Lever "anim:data/models/lever.hmod/PullAndRelease" WAIT 5000 PLAY_SOUND "file:data/sfx/quack.flac" END
On the game code, a simple WAIT 5000 is so simple it's stupid. On the engine side, because each script is running with it's own stack, this means locals, call depth and arguments all survive the wait without any extra work at all. This wasn't possible with the native sequencer without storing things in global state. Here, after the wait, the context picks up exactly where it left off.
EXIT_BLOG_POST
In summary, did I need to do any of this? No. For my little indie game, using Lua was far more than acceptable and definitely the sensible thing to do. But we've established multiple times over that this project is not built with the goal of shipping a game as quickly as possible, but instead for me to learn, push myself and have a lot of fun in the process.
I'm really happy with my shiny reinvented wheel, and that's the most important part. There are many wheels like it, but this one is mine.
Stay In The Loop
Get notified when new posts are published.

Comments
No comments yet. Be the first to comment!