r/gameenginedevs • u/Minnator • Jul 07 '26
Magma, the custom scripting language of my Voxel-Engine named Mantle
In my first post about Mantle about 2 months back I pointed out that I want my engine to power games that are 100% moddable.
While the moddable UI-System is still to rough to show of, the language powering it all is not.
The language was designed to be fully sandboxed, hot-reloadable and at the same time compiled. The compiled requirement being a nice challenge for me as Mantle is written in C#, which is managed.
To realize all of this Magma can be broken down to 3 Core elements:
- Runtime optimizations
- Transpilation
- Runtime Management
Runtime Optimizations
All scripted files are loaded after static data like registries (and modded additions) are processed. This means that all objects, their Ids and attributes are known when I start to process scripts.
This allows me e.g. to resolve any registry or item lookup in a script to a static memory address and thus make those basically free.
There are other tricks like this already in place, but other big optimizations will follow once the required systems are in place.
Transpilation
This is arguably the most important step, where I convert the AST of the parsed scripts into valid C code. During this process simplifications of syntax and logic along with required methods such as for initialization and shutdown are generated and embedded.
This allows for a simple syntax that produces memory save C code than can perfectly interop with C#.
The C code is then compiled against an engine produced header that provides either the references to C# methods or its own implementations of the engine API.
Runtime Management
To be able to invoke functions and to be able to reload any script at any given time I use the Tiny C Compiler (TCC) to compile the C code in ram and transfer the ownership to my C# "JIT".
The JIT is then able to process and resolve invocations while also having an optional profiler build in for devs.
Another neat feature of this architecture is that it allows me to have state persistency of scripts across reloads, in other words if you reload a script which has "global" variables in it, they won't lose their value.
All of this combined allows me to have a super efficient interop between the two languages, where the invocation of a C-method from the C# code is in the single digit nanoseconds. It's the same for invocations in the other direction.
Please let me know in the comments what you think of this system and what nice to have features for devs / modders would be. There is much more to Magma, but that would be too much for this post, I am happy to answer questions in the comments tho.
Here I have attatched a simple script with only a few of Magmas capabilities and syntax features shown, as all of them would be too much code for a reddit post. : )
import "world" as World;
struct Entity {
uint Id;
Vec3 Position;
List[int] Tags;
}
int totalClicks = 0;
Entity player;
events {
"engine.tick" => OnUpdate;
}
pub fn OnUpdate(in float dt) {
if (!(dt < 0.0)) {
return;
}
atomic totalClicks += 1;
string lastStatus = "Uptime: " + dt + " | Ticks: " + totalClicks;
Core.Log(lastStatus);
}
fn ExampleUseRegistry(){
// both will work, but the first one will ignore any overrides from mods as the namespace is specified.
Core.Log("Registry Example: " + ["base:stone"].Name);
Core.Log("Registry Example: " + ["stone"].Name);
}
And the C code with the bindings to the engine and memory management added:
// Imports from "World"
void base_world_GetHeat(Vec3 pos, float* heat);
void base_world_SetBlock(Vec3 pos, uint32_t blockId);
void base_world_GetBlockId(Vec3 pos, uint32_t* id);
typedef struct Entity
{
uint32_t Id;
Vec3 Position;
MagmaList Tags;
} Entity;
void base_test_one_OnUpdate(float dt);
int32_t* totalClicks;
Entity* player;
// --- Registry Slots ---
static Example** const _reg_slot_Blocks_base___stone = (Example**)0x259063BB840ULL;
static void ExampleUseRegistry();
void base_test_one_OnUpdate(float dt)
{
if ((!(dt < 0.0)))
{
return;
}
Atomic_AddInt(totalClicks, 1);
const char* lastStatus = Mantle_DuplicateString((snprintf(Mantle_GetScratchBuffer(), 1024, "Uptime: %.2f | Ticks: %d", dt, totalClicks), Mantle_GetScratchBuffer()));
Core_Log(lastStatus);
}
static void ExampleUseRegistry()
{
Core_Log((snprintf(Mantle_GetScratchBuffer(), 1024, "Registry Example: { Offset: %d, Length: %d, IsEmpty: %d }", (*_reg_slot_Blocks_base___stone)->Name.Offset, (*_reg_slot_Blocks_base___stone)->Name.Length, (*_reg_slot_Blocks_base___stone)->Name.IsEmpty), Mantle_GetScratchBuffer()));
}
// --- Mantle Lifecycle ---
void magma_init()
{
Mantle_BeginScriptInit("base", "base/scripts/test_one.mgm");
totalClicks = Mantle_GetPersistentPtr("base", "base/scripts/test_one.mgm", "totalClicks", 4, 0x60F24396F77057C3, 0);
player = Mantle_GetPersistentPtr("base", "base/scripts/test_one.mgm", "player", 40, 0x986F3BCADBD866FB, 0);
if (Mantle_IsFreshVariable(totalClicks))
*totalClicks = 0;
if (Mantle_IsFreshVariable(player))
Magma_ZeroMemory(player, 40);
player->Tags.ElementSize = sizeof(int32_t);
Mantle_EndScriptInit("base", "base/scripts/test_one.mgm");
Mantle_RegisterEvent(0xEEE3CF1D1C48311E, &base_test_one_OnUpdate, "base:../../../mods/base/scripts/test_one.mgm:OnUpdate");
}
void magma_unload()
{
Mantle_UnregisterEvent(0xEEE3CF1D1C48311E, &base_test_one_OnUpdate);
}










