Skip to content

ON events

These events are used in set_callback to call your callback function before or after something happens in the game. The named events related to game screens generally run on the first engine update when changing to said screen. Coming back from the options screen does not fire the parent screen event though (OPTIONS to LEVEL for example). If you need more fine-grained control over the screen events, use SCREEN, PRE_LOAD_SCREEN and POST_LOAD_SCREEN with the help of the state.screen variables.

ON.ARENA_INTRO

Runs when entering the arena VS intro screen.

function callback()

ON.ARENA_ITEMS

Runs when entering the arena item config screen.

function callback()

ON.ARENA_MATCH

Runs when entering the arena level screen, after all entities have spawned, on the first level frame, before the get ready go scene.

function callback()

ON.ARENA_MENU

Runs when entering the main arena rules menu screen.

function callback()

ON.ARENA_SCORE

Runs when entering the arena scores screen.

function callback()

ON.ARENA_SELECT

function callback()

ON.ARENA_STAGES

Runs when entering the arena stage selection screen.

function callback()

ON.BLOCKED_GAME_LOOP

Runs instead of POST_GAME_LOOP when anything blocks a PRE_GAME_LOOP. Even runs in Playlunky when Overlunky blocks a PRE_GAME_LOOP.

function callback()

ON.BLOCKED_PROCESS_INPUT

Runs instead of POST_PROCESS_INPUT when anything blocks a PRE_PROCESS_INPUT. Even runs in Playlunky when Overlunky blocks a PRE_PROCESS_INPUT.

function callback()

ON.BLOCKED_UPDATE

Runs instead of POST_UPDATE when anything blocks a PRE_UPDATE. Even runs in Playlunky when Overlunky blocks a PRE_UPDATE.

function callback()

ON.CAMP

Runs when entering the camp, after all entities have spawned, on the first level frame.

function callback()

ON.CHARACTER_SELECT

Runs when entering the character select screen.

function callback()

ON.CONSTELLATION

Runs when entering the turning into constellation cutscene after cosmic ocean.

function callback()

ON.CREDITS

Runs when entering the credits screen.

function callback()

ON.DEATH

Runs when entering the death screen.

function callback()

ON.DEATH_MESSAGE

Params: STRINGID id Runs once after death when the death message journal page is shown. The parameter is the STRINGID of the title, like 1221 for BLOWN UP.

function callback(stringid: STRINGID)
stringid: STRINGID

ON.FRAME

Runs while playing the game while the player is controllable, not in the base camp or the arena mode

function callback()

ON.GAMEFRAME

Runs whenever the game engine is actively running. This includes base camp, arena, level transition and death screen

function callback()

ON.GUIFRAME

Params: GuiDrawContext draw_ctx Runs every frame the game is rendered, thus runs at selected framerate. Drawing functions are only available during this callback through a GuiDrawContext

function callback(draw_ctx: GuiDrawContext)
draw_ctx: GuiDrawContext

ON.INTRO

Runs when entering the intro cutscene.

function callback()

ON.LEADERBOARD

Runs when entering the leaderboard screen.

function callback()

ON.LEVEL

Runs when entering any level, after all entities have spawned, on the first level frame.

function callback()

ON.LOAD

Params: LoadContext load_ctx Runs as soon as your script is loaded, including reloads, then never again

function callback(p1: LoadContext)
p1: LoadContext

ON.LOADING

Runs whenever state.loading changes and is > 0. Prefer PRE/POST_LOAD_SCREEN instead though.

function callback()

Runs when entering the the mossmouth logo screen.

function callback()

Runs when entering the main menu.

function callback()

ON.ONLINE_LOADING

Runs when entering the online loading screen.

function callback()

ON.ONLINE_LOBBY

Runs when entering the online lobby screen.

function callback()

ON.OPTIONS

Runs when entering the options menu.

function callback()

ON.PLAYER_PROFILE

Runs when entering the player profile screen.

function callback()

ON.POST_GAME_LOOP

Runs right after the main engine loop.

function callback()

ON.POST_LAYER_CREATION

Params: LAYER layer Runs right after a layer has been created and you can start spawning entities in it. Runs in all screens that usually have entities, or when creating a layer manually.

function callback(layer: LAYER)
layer: LAYER

ON.POST_LAYER_DESTRUCTION

Params: LAYER layer Runs right after a layer has been unloaded and any entities there destroyed. Runs in pretty much all screens, even ones without entities. The screen has already changed at this point, meaning the screen being destroyed is in state.screen_last.

function callback(layer: LAYER)
layer: LAYER

ON.POST_LEVEL_CREATION

Runs right after the back layer has been created and you can start spawning entities in it. Runs in all screens that usually have entities, or when creating a layer manually.

function callback()

ON.POST_LEVEL_DESTRUCTION

Runs right after the current level has been unloaded and all entities destroyed. Runs in pretty much all screens, even ones without entities. The screen has already changed at this point, meaning the screen being destroyed is in state.screen_last.

function callback()

ON.POST_LEVEL_GENERATION

Runs right after level generation is done, i.e. after all level gen entities are spawned, before any entities are updated. You can spawn your own entities here, like extra enemies, give items to players etc.

function callback()

ON.POST_LOAD_JOURNAL_CHAPTER

Params: JOURNALUI_PAGE_SHOWN chapter, array:int pages Runs after the pages for the journal are prepared, but not yet displayed, pages is a list of page numbers that the game loaded, if you want to change it, do the changes (remove pages, add new ones, change order) and return it All new pages will be created as JournalPageStory, any custom with page number above 9 will be empty, I recommend using above 99 to be sure not to get the game page, you can later use this to recognise and render your own stuff on that page in the RENDER_POST_JOURNAL_PAGE Return: return new page array to modify the journal, returning empty array or not returning anything will load the journal normally, any page number that was already loaded will result in the standard game page When changing the order of game pages make sure that the page that normally is rendered on the left side is on the left in the new order, otherwise you get some messed up result, custom pages don't have this problem. The order is: left, right, left, right ...

function callback(chapter: integer, p2: integer[]): any
chapter: integer
p2: integer[]

Returns any (may be nil)

Returning nil (or nothing): treated as {}.

ON.POST_LOAD_SCREEN

Runs right after a screen is loaded, before rendering anything

function callback()

ON.POST_LOAD_STATE

Runs right after the main StateMemory is manually loaded from a slot or a custom SaveState. Slot is 1..4 or -1 on custom SaveState. Params: int slot, StateMemory loaded

function callback(slot: integer, loaded: StateMemory?)
slot: integer
loaded: StateMemory?

ON.POST_PROCESS_INPUT

Runs right after the game gets input from various devices and writes to a bunch of buttons-variables. Probably the first chance you have to capture or edit buttons_gameplay or buttons_menu sort of things.

function callback()

ON.POST_ROOM_GENERATION

Params: PostRoomGenerationContext room_gen_ctx Runs right after all rooms are generated before entities are spawned

function callback(p1: PostRoomGenerationContext)
p1: PostRoomGenerationContext

ON.POST_SAVE_STATE

Runs right after the main StateMemory is manually saved to a slot or a custom SaveState. Slot is 1..4 or -1 on custom SaveState. Params: int slot, StateMemory saved

function callback(slot: integer, saved: StateMemory?)
slot: integer
saved: StateMemory?

ON.POST_UPDATE

Runs right after the State is updated, runs always (menu, settings, camp, game, arena, online etc.) with the game engine, typically 60FPS

function callback()

ON.PRE_GAME_LOOP

Runs right before the main engine loop. Return true to block state updates and menu updates, i.e. to pause inside menus.

function callback(): boolean

Returns boolean

Returning nil (or nothing): treated as false.

ON.PRE_GET_FEAT

Runs before getting performed status for a FEAT when rendering the Feats page in journal. Return: true to override the vanilla feat with your own. Defaults to Steam GetAchievement.

function callback(feat: FEAT): boolean
feat: FEAT

Returns boolean

Returning nil (or nothing): no override: the C++ side keeps its default.

ON.PRE_GET_RANDOM_ROOM

Params: int x, int y, LAYER layer, ROOM_TEMPLATE room_template Return: string room_data Called when the game wants to get a random room for a given template. Return a string that represents a room template to make the game use that. If the size of the string returned does not match with the room templates expected size the room is discarded. White spaces at the beginning and end of the string are stripped, not at the beginning and end of each line.

function callback(x: integer, y: integer, layer: integer, room_template: integer): string
x: integer
y: integer
layer: integer
room_template: integer

Returns string

Returning nil (or nothing): treated as std::string{}.

ON.PRE_HANDLE_ROOM_TILES

Params: int x, int y, ROOM_TEMPLATE room_template, PreHandleRoomTilesContext room_ctx Return: bool last_callback to determine whether callbacks of the same type should be executed after this Runs after a random room was selected and right before it would spawn entities for each tile code Allows you to modify the rooms content in the front and back layer as well as add a back layer if not yet existant

function callback(x: integer, y: integer, room_template: integer, ctx: PreHandleRoomTilesContext): boolean
x: integer
y: integer
room_template: integer
ctx: PreHandleRoomTilesContext

Returns boolean

Returning nil (or nothing): treated as false.

ON.PRE_LAYER_CREATION

Params: LAYER layer Runs right before a layer is created. Runs in all screens that usually have entities, or when creating a layer manually.

function callback(layer: LAYER): boolean
layer: LAYER

Returns boolean

Returning nil (or nothing): treated as false.

ON.PRE_LAYER_DESTRUCTION

Params: LAYER layer Runs right before a layer is unloaded and any entities there destroyed. Runs in pretty much all screens, even ones without entities. The screen has already changed at this point, meaning the screen being destroyed is in state.screen_last.

function callback(layer: LAYER): boolean
layer: LAYER

Returns boolean

Returning nil (or nothing): treated as false.

ON.PRE_LEVEL_CREATION

Runs right before the front layer is created. Runs in all screens that usually have entities, or when creating a layer manually.

function callback(): boolean

Returns boolean

Returning nil (or nothing): treated as false.

ON.PRE_LEVEL_DESTRUCTION

Runs right before the current level is unloaded and any entities destroyed. Runs in pretty much all screens, even ones without entities. The screen has already changed at this point, meaning the screen being destroyed is in state.screen_last.

function callback(): boolean

Returns boolean

Returning nil (or nothing): treated as false.

ON.PRE_LEVEL_GENERATION

Runs before any level generation, no entities exist at this point. Runs in most screens that have entities. Return true to block normal level generation, i.e. stop any entities from being spawned by ThemeInfo functions. Does not block other ThemeInfo functions, like spawn_effects though. POST_LEVEL_GENERATION will still run if this callback is blocked.

function callback(): boolean

Returns boolean

Returning nil (or nothing): treated as false.

ON.PRE_LOAD_JOURNAL_CHAPTER

Params: JOURNALUI_PAGE_SHOWN chapter Runs before the journal or any of it's chapter is opened Return: return true to not load the chapter (or journal as a whole)

function callback(chapter: integer): boolean
chapter: integer

Returns boolean

Returning nil (or nothing): no override: the C++ side keeps its default.

ON.PRE_LOAD_LEVEL_FILES

Params: PreLoadLevelFilesContext load_level_ctx Runs right before level files would be loaded

function callback(p1: PreLoadLevelFilesContext)
p1: PreLoadLevelFilesContext

ON.PRE_LOAD_SCREEN

Runs right before loading a new screen based on screen_next. Return true from callback to block the screen from loading.

function callback(): boolean

Returns boolean

Returning nil (or nothing): treated as false.

ON.PRE_LOAD_STATE

Runs right before the main StateMemory is manually loaded from a slot or a custom SaveState. Slot is 1..4 or -1 on custom SaveState. Return true to block load. Params: int slot, StateMemory loaded

function callback(slot: integer, loaded: StateMemory?): boolean
slot: integer
loaded: StateMemory?

Returns boolean

Returning nil (or nothing): treated as false.

ON.PRE_PROCESS_INPUT

Runs right before the game gets input from various devices and writes to a bunch of buttons-variables. Return true to disable all game input completely.

function callback(): boolean

Returns boolean

Returning nil (or nothing): treated as false.

ON.PRE_SAVE_STATE

Runs right before the main StateMemory is manually saved to a slot or a custom SaveState. Slot is 1..4 or -1 on custom SaveState. Return true to block save. Params: int slot, StateMemory saved

function callback(slot: integer, saved: StateMemory?): boolean
slot: integer
saved: StateMemory?

Returns boolean

Returning nil (or nothing): treated as false.

ON.PRE_SET_FEAT

Runs before the game sets a vanilla feat performed. Return: true to block the default behaviour of calling Steam SetAchievement.

function callback(feat: FEAT): boolean
feat: FEAT

Returns boolean

Returning nil (or nothing): no override: the C++ side keeps its default.

ON.PRE_UPDATE

Runs before the State is updated, runs always (menu, settings, camp, game, arena, online etc.) with the game engine, typically 60FPS Return behavior: return true to stop further PRE_UPDATE callbacks from executing and don't update the state (this will essentially freeze the game engine)

function callback(): boolean

Returns boolean

Returning nil (or nothing): treated as false.

ON.PROLOGUE

Runs when entering the prologue / poem.

function callback()

ON.RECAP

Runs when entering the Dear Journal screen after final scores.

function callback()

ON.RENDER_POST_BLURRED_BACKGROUND

-- replace journal book background with a piece of paper, drawn on top of the blurred bg
set_callback(function(ctx, blur)
local src = Quad:new(AABB:new(0.535, 0.21, 0.85, 0.93))
local dest = Quad:new(AABB:new(-1, 1, 1, -1))
local col = Color:white()
col.a = game_manager.journal_ui.opacity --or simply 'blur' to draw behind pause menu too
ctx:draw_screen_texture(TEXTURE.DATA_TEXTURES_JOURNAL_TOP_MAIN_0, src, dest, col)
-- hide real book offscreen, only drawing pages
game_manager.journal_ui.book_background.y = 2
end, ON.RENDER_POST_BLURRED_BACKGROUND)
-- the previous cb is not called for death screen, default to normal book bg there
set_pre_render_screen(SCREEN.DEATH, function(ctx)
game_manager.journal_ui.book_background.y = 0
end)

Params: VanillaRenderContext render_ctx, float blur Runs after the blurred background is drawn on screen, behind pause menu or journal book. In this event, you can draw textures with the draw_screen_texture function of the render_ctx. (blur amount is probably the same as journal opacity)

function callback(render_ctx: VanillaRenderContext, blur_amount: number): boolean
render_ctx: VanillaRenderContext
blur_amount: number

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_POST_DRAW_DEPTH

Params: VanillaRenderContext render_ctx, int draw_depth Runs right after the entities of the specified draw_depth are drawn on screen. In this event, you can draw textures with the draw_world_texture function of the render_ctx

function callback(render_ctx: VanillaRenderContext, draw_depth: integer): boolean
render_ctx: VanillaRenderContext
draw_depth: integer

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_POST_GAME

Params: VanillaRenderContext render_ctx Runs after the level and HUD are rendered, before pause menus and blur effects

function callback(render_ctx: VanillaRenderContext): boolean
render_ctx: VanillaRenderContext

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_POST_HUD

Params: VanillaRenderContext render_ctx, Hud hud Runs after the HUD is drawn on screen. In this event, you can draw textures with the draw_screen_texture function of the render_ctx

function callback(render_ctx: VanillaRenderContext, hud: Hud?): boolean
render_ctx: VanillaRenderContext
hud: Hud?

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_POST_JOURNAL_PAGE

Params: VanillaRenderContext render_ctx, JOURNAL_PAGE_TYPE page_type, JournalPage page Runs after the journal page is drawn on screen. In this event, you can draw textures with the draw_screen_texture function of the VanillaRenderContext The JournalPage parameter gives you access to the specific fields of the page. Be sure to cast it to the correct type, the following functions are available to do that: page:as_journal_page_progress() page:as_journal_page_journalmenu() page:as_journal_page_places() page:as_journal_page_people() page:as_journal_page_bestiary() page:as_journal_page_items() page:as_journal_page_traps() page:as_journal_page_feats() page:as_journal_page_deathcause() page:as_journal_page_deathmenu() page:as_journal_page_playerprofile() page:as_journal_page_lastgameplayed()

function callback(render_ctx: VanillaRenderContext, page_type: integer, page: JournalPage?): boolean
render_ctx: VanillaRenderContext
page_type: integer
page: JournalPage?

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_POST_LAYER

Params: VanillaRenderContext render_ctx, int rendered_layer Runs after a layer is rendered, runs for both layers during layer door transitions. Things drawn here will be part of the layer transition animation

function callback(render_ctx: VanillaRenderContext, layer: integer): boolean
render_ctx: VanillaRenderContext
layer: integer

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_POST_LEVEL

Params: VanillaRenderContext render_ctx, int camera_layer Runs after the level is rendered, before hud

function callback(render_ctx: VanillaRenderContext, layer: integer): boolean
render_ctx: VanillaRenderContext
layer: integer

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_POST_PAUSE_MENU

Params: VanillaRenderContext render_ctx Runs after the pause menu is drawn on screen. In this event, you can draw textures with the draw_screen_texture function of the render_ctx

function callback(render_ctx: VanillaRenderContext): boolean
render_ctx: VanillaRenderContext

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_PRE_BLURRED_BACKGROUND

Params: VanillaRenderContext render_ctx, float blur Runs before the blurred background is drawn on screen, behind pause menu or journal book. In this event, you can't really draw textures, because the blurred background is drawn on top of them. Return true to skip rendering.

function callback(render_ctx: VanillaRenderContext, blur_amount: number): boolean
render_ctx: VanillaRenderContext
blur_amount: number

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_PRE_DRAW_DEPTH

Params: VanillaRenderContext render_ctx, int draw_depth Runs before the entities of the specified draw_depth are drawn on screen. In this event, you can draw textures with the draw_world_texture function of the render_ctx. Return true to skip rendering.

function callback(render_ctx: VanillaRenderContext, draw_depth: integer): boolean
render_ctx: VanillaRenderContext
draw_depth: integer

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_PRE_GAME

Params: VanillaRenderContext render_ctx Runs before the in-game part of the game is rendered. Return true to skip rendering.

function callback(render_ctx: VanillaRenderContext): boolean
render_ctx: VanillaRenderContext

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_PRE_HUD

Params: VanillaRenderContext render_ctx, Hud hud Runs before the HUD is drawn on screen. In this event, you can draw textures with the draw_screen_texture function of the render_ctx or edit the Hud values. Return true to skip rendering.

function callback(render_ctx: VanillaRenderContext, hud: Hud?): boolean
render_ctx: VanillaRenderContext
hud: Hud?

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_PRE_JOURNAL_PAGE

Params: VanillaRenderContext render_ctx, JOURNAL_PAGE_TYPE page_type, JournalPage page Runs before the journal page is drawn on screen. Return true to skip rendering.

function callback(render_ctx: VanillaRenderContext, page_type: integer, page: JournalPage?): boolean
render_ctx: VanillaRenderContext
page_type: integer
page: JournalPage?

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_PRE_LAYER

Params: VanillaRenderContext render_ctx, int rendered_layer Runs before a layer is rendered, runs for both layers during layer door transitions. Return true to skip rendering.

function callback(render_ctx: VanillaRenderContext, layer: integer): boolean
render_ctx: VanillaRenderContext
layer: integer

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_PRE_LEVEL

Params: VanillaRenderContext render_ctx, int camera_layer Runs before the level is rendered. Return true to skip rendering.

function callback(render_ctx: VanillaRenderContext, layer: integer): boolean
render_ctx: VanillaRenderContext
layer: integer

Returns boolean

Returning nil (or nothing): treated as false.

ON.RENDER_PRE_PAUSE_MENU

Params: VanillaRenderContext render_ctx Runs before the pause menu is drawn on screen. In this event, you can't really draw textures, because the blurred background is drawn on top of them. Return true to skip rendering.

function callback(render_ctx: VanillaRenderContext): boolean
render_ctx: VanillaRenderContext

Returns boolean

Returning nil (or nothing): treated as false.

ON.RESET

Runs when resetting a run

function callback()

ON.SAVE

Params: SaveContext save_ctx Runs at the same times as ON.SCREEN, but receives the save_ctx

function callback(p1: SaveContext)
p1: SaveContext

ON.SCORES

Runs when entering the final score celebration screen of a normal or hard ending.

function callback()

ON.SCREEN

Runs whenever state.screen changes

function callback()

ON.SCRIPT_DISABLE

Runs when the script is disabled from the UI and also right before unloading/reloading.

function callback()

ON.SCRIPT_ENABLE

Runs when the script is enabled from the UI or when imported by another script while disabled, but not on load.

function callback()

ON.SEED_INPUT

Runs when entering the seed input screen of a seeded run.

function callback()

ON.SPACESHIP

Runs when entering the olmecship cutscene after Tiamat.

function callback()

ON.SPEECH_BUBBLE

Params: Entity speaking_entity, string text Runs before any speech bubble is created, even the one using say function Return: if you don't return anything it will execute the speech bubble function normally with default message if you return empty string, it will not create the speech bubble at all, if you return string, it will use that instead of the original The first script to return string (empty or not) will take priority, the rest will receive callback call but the return behavior won't matter

function callback(entity: Entity?, buffer: string): string
entity: Entity?
buffer: string

Returns string

Returning nil (or nothing): no override: the C++ side keeps its default.

ON.START

Runs on the first ON.SCREEN of a run

function callback()

ON.TEAM_SELECT

Runs when entering the team select screen of arena mode.

function callback()

ON.TITLE

Runs when entering the title screen.

function callback()

ON.TOAST

Params: string text Runs before any toast is created, even the one using toast function Return: if you don't return anything it will execute the toast function normally with default message if you return empty string, it will not create the toast at all, if you return string, it will use that instead of the original message The first script to return string (empty or not) will take priority, the rest will receive callback call but the return behavior won't matter

function callback(buffer: string): string
buffer: string

Returns string

Returning nil (or nothing): no override: the C++ side keeps its default.

ON.TRANSITION

Runs when entering the transition screen, after all entities have spawned.

function callback()

ON.USER_DATA

Params: Entity ent Runs on all changes to Entity.user_data, including after loading saved user_data in the next level and transition. Also runs the first time user_data is set back to nil, but nil won't be saved to bother you on future levels.

function callback(ent: Entity?)
ent: Entity?

ON.WIN

Runs when entering any winning cutscene, including the constellation.

function callback()