A live dungeon panel
You build a small panel that sits beside the game window while you are in a dungeon. It shows a line of text and a progress bar, "Defeated: 0/5". A chat command moves the bar one step, and the bar redraws on your client at once, with no reload of the page.
Two files make it: an HTML template that lives in the client pack, and a Forge that opens the panel and sets the counters the template reads. The shipped progress.html on the right is the macro library the template uses; read it first.
Before you start
- The finished module of Your first Forge module, and its compile and restart loop.
- The UCHTML reference open beside you, and UCHTML for why a page is a template and not a hand-built window.
- A debug client. A release client still opens the
.htmlfile but renders from the compiled module, so your new page shows nothing there until you compile it with the Compiler tab of the Wiki Manager. In a debug client the.htmlis read as it is. - A test character that can use GM commands and can stand inside any dungeon. The panel is bound to a dungeon instance, so outside one the Forge refuses.
Read the shipped macros
The panel does not draw the bar itself. progress.html defines three macros. progress(text, count, max) draws a label and a <progress> element beside it. progress_block puts the bar under the label. rprogress(done_var, need_var, ...) wraps whatever you hand it in a <div> with an id and a reactive attribute that names the variables it reads.
The reactive attribute is the whole trick. When a counter named in it changes, the client re-renders only that zone and patches it into the window. The rest of the page stays as it is.
Check. You can name the two counters the zone below will watch: done and need.
Step 1: write the template
A page is a Jinja template. It imports the macros with with context, so the macros see the counters, and it calls rprogress with a call block. The block receives the current done and need as arguments, and everything inside it is the part that redraws.
Put the file in the client pack under locale/universal/dungeon_states/, next to the folder templates/. The import path is relative to that folder.
Check. Every tag you used is a tag of the template engine: main, spacer, progress, div. The page has one reactive zone, progress-done-need.
Step 2: open the panel from a Forge
The shipped dungeon state module exposes open(map_index, template, title) and set_counters(map_index, counters). open binds a state to the dungeon on that map and sends the template name to every player inside. set_counters stores each value as a string and sends only the keys you pass. A key set to nil is removed.
Create the Forge and register a /panel_demo open command with it.
Add ../system/tutorial/panel_demo.forge to share/locale/italy/quest/qc/quest_list and compile it:
./qc.sh --f ../system/tutorial/panel_demo.forge
Check. The compiler prints no error and share/locale/italy/quest/forge/panel_demo.game exists. Restart the game cores.
Step 3: move the bar
A counter that changes is the only thing that has to cross the wire. Add step and close commands. step reads the stored done back as a string, adds one, caps it at NEED, and sends only done.
Compile and restart again.
Check. The file has three routes, open, step and close, and returns a table at the end of the environment, because a forge that returns nothing fails to load.
Run it
Log in to a debug client, step into any dungeon and type /panel_demo open. A window titled "Yard drill" appears with the line "Defeat the dummies in the yard." and the bar "Defeated: 0/5". Type /panel_demo step five times: the label and the bar advance each time and stop at 5/5. /panel_demo close closes the window.
If the window appears with nothing in it, the client could not render the template: you are on a release client without a compiled page, or the file is not under locale/universal/dungeon_states/. If the chat says "Stand inside a dungeon first.", the map you are on is not a dungeon instance.
Where to go next
- UCHTML: the pipeline from template to native widgets, and the reactive zones.
- Wiki Manager and data generation: the Compiler tab.
- Luna Forge: the Forge module format used here.