Menus and pages in the game's style
Script mods can work in the game's own menus, with the game's own look: add buttons to the main menu, MORE, the three
hunt menu pages and the hunt's menu; hide or move the game's buttons; open whole pages drawn with the game's
backgrounds, fonts and buttons; and react to the menu actions before the game does. No ImGui: these are the game's own
GUI controls, so they light up, fade and take touches (Android) exactly like the game's. Everything here works in both
games (API 3, "api": 3 in mod.json). The reference: menu, the gui and menu_* events,
draw (draw.sprite, the game's fonts).
A button and a page
local page = menu.page("mymod:help", {title = "Field guide"})
page:text("Shots to the head and the heart kill at once. Walk, do not run: running is loud.", {width = 320})
page:image("button_dino_active_9")
page:button("GOT IT", function() page:close() end)
menu.button("main", "GUIDE", function() page:open() end)
GUIDE appears in the main menu's grid under the game's buttons, as one more button of the same kind; pressing it
fades to the page like the game changes screens. The page has the MORE screen's background, a red title, your text,
picture and button in one centred column, and a BACK button where the game has its own. BACK, Esc and the Android back
button close it.
Make menus and pages at load time: they stay until the scripts reload (F9, Apply in the Mods screen), when everything a mod made goes away and its new run makes them again.
Where things go
The first argument of menu.button (and menu.text, menu.image, menu.list) says where:
where |
What | Mods' buttons go (without x, y) |
|---|---|---|
main |
the main menu | the grid goes on below the game's rows, two to a row (an odd last one centred), three to a row from five on; a second extra row moves the whole grid up one row. Six fit: more need places of their own (or a page) |
more |
MORE | the column below OPTIONS / SWITCH TO / ABOUT, then a second column to the right |
hunt_area, hunt_species, hunt_weapon |
the hunt menu's pages | the right column, upwards from above the option buttons (species page: from the bottom bar); the small button style |
game |
the hunt's menu (ESCAPE / RESTART / OPTIONS, the MENU tab) | cells of the same kind around the game's, sliding in with them |
| a page id | your page | one centred column from the top (top of the page, 64 by default) |
Positions are game units, the units the game lays out its menus in: the screen is 320 units high (480 wide on a
screen narrower than 3:2, a tablet), x from the left edge, y from the top. menu.screen() gives the size; it is the
same on every screen of that shape, so a layout made on the PC fits the phone. x, y is an item's centre (a text's
y is its top). Give both to place an item yourself; item:move(x, y) moves it later, item:move() puts it back.
on("menu_ready", function(e)
-- a button in the free corner of the species page
menu.button("hunt_species", "TIPS", showTips, {x = e.width - 70, y = 40})
end)
menu_ready comes once the game has made its menus (the mods load before that): measure there (menu.screen,
menu.spriteSize, menu.textSize, menu.buttons).
The game's buttons
| Menu | Names |
|---|---|
main |
hunt, survive (Dinosaur Hunter), trophy, more, mods, quit |
more |
options, switch, about, back |
hunt_area |
back, next, dawn, day, night, observer |
hunt_species |
back, next |
hunt_weapon |
back, hunt, camouflage, cover_scent, tranquilizer, radar |
game |
escape, restart, options (they can be hidden, not moved) |
menu.hide("main", "survive") -- gone, and takes no presses
menu.move("more", "about", 400, 120) -- its new centre (an icon on it moves along)
for _, b in ipairs(menu.buttons("hunt_area")) do print(b.name, b.x, b.y, b.shown) end
menu.reset() -- all of them back
The hunt menu's option buttons are also equipment: a content pack hides them or adds its own
items with "hidden", which is usually the better way.
Events
menu_action comes when the player presses one of the game's buttons, before the game acts on it; set
e.cancelled = true and the game never sees the press. menu_select does the same for the hunt menu's map, species and
weapon cells (e.kind, e.id, e.selected).
on("menu_action", function(e)
if e.menu == "hunt_weapon" and e.button == "hunt" then
local s = menu.selection()
if #s.weapons > 2 then
game.message("Two weapons at most in this mod")
e.cancelled = true -- the hunt does not start
end
end
end)
on("menu_select", function(e)
if e.kind == "species" and e.id == "trex" and e.selected and not unlockedByQuest then
e.cancelled = true -- the T-Rex cell cannot be chosen yet
end
end)
menu.selection() is the hunt menu's choice as it stands: map, time, observer, species, weapons,
equipment (ids) and the prices. menu_open / menu_close tell other mods about pages (e.page); your own page has
onOpen, onClose and onBack (return false from it to stay open).
Pages
local shop = menu.page("trader:main", {
title = "Trader",
background = "menu_hunt_background", -- any sprite; false: the screen below, dimmed
back = "LEAVE", -- the BACK button's label (false: none)
onOpen = function(p) refresh() end,
draw = function(p) -- every frame, with the game's renderer (see below)
draw.text(20, 20, "Credits: " .. player.credits(), "#ffe020", nil, "left", "ccra14")
end,
})
local credits = shop:text("")
shop:list({"Ammunition (20)", "Resupply (50)", "Map of the area (80)"}, function(i, row) buy(i) end, {rows = 3})
shop:button("BUY ALL", buyAll)
- Items:
page:text(text, {width = ..., align = ..., font = ...})wraps atwidth;page:image(sprite);page:list(rows, fn, {rows = 6, width = 240, selected = 1})pages through long lists with small arrows;page:button(label, fn). Every item keeps its handle:item:setText,item:setVisible,item:setEnabled,item:setSprite,item:setColor,item:remove; listsitem:setRows,item:select,item:selected. - Opening:
page:open()(ormenu.open("trader:main")) from a button, a key, an event. A page opened from a page goes on top of it; BACK returns to the one below, then to the menu or hunt it was opened over.page:clear()takes every item off (to build it anew inonOpen),page:remove()the page itself. - In a hunt a page pauses the hunt, as the hunt's own menu does, and frees the mouse in PC control mode. A button in
the hunt's menu (
menu.button("game", ...)) is the natural way there;background = falsekeeps the hunt visible. - Pages lie over everything of the game (its popups too) and under the port's own windows (the mod menu, the console).
Drawing with the game's assets
The gui event runs inside the game's own GUI pass, every frame, in the menus and in hunts. There draw.sprite,
draw.text, draw.textSize, draw.fillRect, draw.rect, draw.line and draw.screen use the game's renderer, its
sprites and its fonts, in game units: what you draw sits among the game's own pictures (over its menus, under its
popups) instead of on top of the screen like the draw event's ImGui drawing. A page's draw function is the same
pass, above the page's background.
on("gui", function(e)
if e.menu == "main" then
draw.sprite("icon_radar", 10, 10, {scale = 0.8}) -- a sprite of the game's ui/*.cfg
draw.text(40, 12, "Modded edition", "#ffe020", nil, "left", "ccra14") -- one of the game's fonts
end
end)
- Sprites are the game's by name (
button_dino_active_9is the T-Rex's menu cell,button_weapon_active_5the rifle's,icon_camouflage,menu_hunt_cell_button,game_menu_cell, the backgroundsmenu_main_background,menu_more_background,menu_hunt_background; the game's files list the sprite files), your own from a sprite file of your mod (menu.loadSprites("mysprites.cfg"), the game's.cfgformat), or a.tgafile of your mod as one whole picture.content.species(id).iconis a species' menu cell. Options:scale,w,h,color,angle,center,flipX,flipY,layer. - Fonts:
ofs15(the cards' text),ofs13,lith18(the buttons' carved capitals; Ice Age's has no digits),ccra14(the hunt's yellow labels).draw.text(x, y, text, color, size, align, font):sizeis the text height in game units (default the font's own). - Layers: the game's menu buttons are at 1000, its popups at 2000, pages at 2900 and up; the
guievent draws at 1500 (layer =for a sprite). draw.spritealso works in thedrawevent: there in its pixels, as large as the game shows the sprite, over everything (a HUD with the game's icons).
Sounds
A button or list option sound = "menugo.wav" plays one of the game's .wav files (or your mod's) when it is pressed
(the game's own menus are silent; menugo.wav, menugo2.wav and menumov.wav are its menu sounds). sound.play
plays any sound at any time.
Phones and tablets
Nothing changes: the mods' buttons are the game's GUI controls, so taps reach them like the game's, the game's tablet layout (480 units wide) has the same names and the automatic places follow it. A three-finger tap still opens the mod menu; a page closes with the back button.
Limits
- The hunt menu's map, species and weapon cells come from the content registry, not from
menu.*: a new species, weapon or map is a content pack entry (orcontent.define), and the menu makes its cell. - The options screen (OPTIONS) and the game's popups take no mod buttons; the hunt's menu cells can be hidden, not moved.
- The game's fonts are Latin-1: other characters show as
?,#as a space (it starts the game's colour codes). - Up to 1024 sprites in all, the game's own included (the sprite table of the game).
The example examples/mods/ice-age-lodge adds an animal and an equipment item to Ice Age's hunt menu and a LODGE page
listing every animal with its picture; examples/tests/test_menu_api uses every function here.