Skip to main content

Cookbook

Each recipe starts from something you want to do ("run code when an instance is destroyed"), names the API that does it and why, quotes a snippet from a mod that ships in this repository with a link to the exact lines, and lists the mistakes that cost time. Where both the typed form (from the generated interop) and the untyped form work, both are shown. The recipes assume you have read the concepts: the game thread, value lifetime, ownership and fault isolation explain why many of the gotchas exist. If you have not built a mod yet, start with the first mod.

Hooks​

Run your code before or after a game script or object event. See Hooks.

I want to...Recipe
change a script's argument before it runsChange a script's argument
run the original script again (more loot, repeated effect)Run the original script again
cancel a script and return my own valueCancel a script and return your own value
change what a script returnedChange what a script returned
run code when an instance is destroyedInstance destroyed
run code when an instance is createdInstance created
run code every step of an objectEvery step
hook a user eventHook a user event
look up the name of an event (Create_0, Step_1, Draw_64, Other_10)Event names
hook a function I only know the name of at runtime, and unhook itHook by name
run something once inside the next matching call, with a timeoutRun once in the next call
choose between the attribute, the typed ref and a stringAttribute, typed or string

Game state​

Read and change what the game keeps. See Game state.

I want to...Recipe
read or write a global variableGlobals
find a singleton instance and edit its variablesSingleton instance
loop over every instance of an object, children includedIterate instances
create or destroy an instanceSpawn and destroy
read a ds_mapRead a ds_map
edit a ds_list that lives inside a ds_mapEdit a ds_list
read arrays and structsArrays and structs
walk the object table and its parent and child objectsObject table
scale a value without compounding itScale without compounding

Calling the game​

Make the game do something. See Calling the game.

I want to...Recipe
call a script, typed or by nameCall a script
call a builtinCall a builtin
run an object event directlyRun an object event
look up an asset by name and play a soundAssets and sounds
run code inside the game's own eventInside the game's event

Drawing and UI​

Put pixels on screen, in the game or in the overlay. See Drawing and UI.

I want to...Recipe
add a sprite and a sound and draw in the game's GUI layerSprite and sound
reskin one of the game's spritesReskin a sprite
draw text and shapes with draw_*Draw builtins
draw a game sprite at a positiondraw_sprite_ext
draw with the game's own UI scriptsGame UI scripts
build an overlay tab with ImGuiOverlay tab
keep a tab alive when it reads live dataUI.Guarded
give widgets stable idsStable ids
open and close UI scopes correctlyScopes

Input​

Keys, the mouse and clicks. See Input.

I want to...Recipe
detect a key pressKey press
read the mouse in GUI coordinatesMouse
take a click the game never seesTake a click
keep keys from reaching the game while my window is openSwallow the keyboard
understand what the overlay does to my inputThe overlay and your input

Settings and persistence​

Keep things between runs. See Settings and persistence.

I want to...Recipe
save a settingConfig
offer settings in the game's own menuModSettings
read a file from my mod's folderMod folder
keep per-character data inside the saveData in the save
survive a hot reloadHot reload
clean up when the mod unloadsOnShutdown

Robustness and testing​

Fail soft, stay fast, and test without a mouse. See Robustness and testing.

I want to...Recipe
handle a game that is not ready (the title screen)Catch GmlException
keep one bad callback from disabling my modGuard
keep per-frame work cheapThrottle OnUpdate
do slow work in the background and come back to the gameBackground work
see what a compiled script calls, and who calls itInspect compiled code
expose commands to the test hostTest host
write a regression mod for a risky areaRegression mods

Looking for the whole API instead? See the API reference. Looking for which script to hook in a game you do not know? See Finding hooks.