Your first Forge module
You build a Forge module named login_counter, one piece at a time. At the end, every login is counted, the player gets a chat line and the game core log gets one too, and a logout drops the reference to the character. The count itself stays in the module until the core stops.
The tutorial has six steps. Each one changes one file, ends with a check you make in the compiler output, the log or the game, and has its own solution. If a step does not behave, press Check in the workbench to compare your files with the solution of the current step, or Solve to see it. The Start step button at the top of each step sets the workbench to what the previous step left, so you can begin from a known state at any point. It overwrites your edits.
| Step | You add | You see |
|---|---|---|
| 1 | the module file | the compiler writes login_counter.game |
| 2 | the quest_list line | [Forge-login_counter]: loaded in the log |
| 3 | the counter and the when login hook | a login line in the log |
| 4 | the chat line | Login 1 in the game |
| 5 | the when logout hook | a logout line that still says online 1 |
| 6 | the cleanup | the logout line says online 0 |
Before you start
- A runtime you can compile and restart: the folder that holds
share/,qc.sh,run.shandshutdown.sh, and a game core you can log in to. About twenty minutes. - A rough idea of what a Forge is: the Forge API reference lists the API, and How Forge modules work explains what happens underneath.
- A text editor. The workbench on the right only shows the files; you create the real ones in
share/locale/italy/system/tutorial/andshare/locale/italy/quest/qc/. Thequest_listfile in the workbench is the one the game ships, shown as it is.
Step 1 of 6: an empty module
A forge file is a forge <name> begin block. Inside it, environment game begin holds Lua that runs once, when the game core boots, and it must return a table or an object. That return value is what the rest of the server sees as forge.game.login_counter. The module here is a small class with two tables keyed by player id: logins for the counts and online for the live characters. Nothing uses them yet.
Create share/locale/italy/system/tutorial/login_counter.forge with exactly this content. Then compile it alone, from the runtime root:
./qc.sh --f ../system/tutorial/login_counter.forge
The path is relative to the quest folder, the same form quest_list uses.
Check. The compiler prints no error, and share/locale/italy/quest/forge/login_counter.game exists. There is nothing under object/ for this forge, because the file has no state block.
Step 2 of 6: register it and see it load
Without an entry in quest_list, --f compiles the file, but the next full compile deletes the result and does not rebuild it. The workbench shows the shipped quest_list; it ends with the forges of the game. Add one line at the end:
Compile again, then restart the game cores so the forge loads (see Run it below).
Check. Search the log:
grep login_counter logs/syslog
You should see:
[Forge-login_counter]: loaded
If the line is missing, logs/syserr tells you why; the most likely cause is the missing quest_list line or a path that is not relative to the quest folder.
Step 3 of 6: count logins from a when login hook
A hook is a when block in a state block. It runs as a quest, with the player who triggered it as the current player, so pc.get_player_id() works there. Keep the hook short: read the player id, find the character, call into the module, return. The module keeps the state, the hook does not.
Replace the whole file with this version. Three things to notice:
Counter:on_loginis called with:, because the environment returned an instance and the method needsself.- The module logs with
forge.sys.log(0, ...). Level 0 always prints, and the line always carries the[Forge-login_counter]:prefix. - The hook never waits. A
when loginblock must run to the end.
Check. Compile again: share/locale/italy/quest/object/notarget/login/login_counter.start and object/state/login_counter now exist. Restart, log in, and the log shows a line like this one (your name will differ):
[Forge-login_counter]: Alice login 1, online 1
Step 4 of 6: tell the player
The count is only in the log so far. One more line in on_login writes it to the player.
chat_packet takes a chat type from forge.sys.game.CHAT_TYPE and the text.
Check. Compile, restart, log in. The chat shows:
Login 1 on this core since it started
Log out and in again without restarting: the chat says Login 2. The count survives a logout because it lives in the module, not in the character.
Step 5 of 6: log the logout and see the stale entry
A when logout hook mirrors the login one. Pass the player id, not the character: the character may already be on its way out, and a plain number needs no nil check. This version only logs, on purpose.
Check. Compile, restart, log in and out. The log shows:
[Forge-login_counter]: pid 1234 logout, online 1
[QUEST_LOGOUT][INFO][1234] Logout PC OK!
online 1 after a logout is the problem. The entry for your player id is still in the table and points at a character the engine has already destroyed. Log in again and the entry is only replaced because it has the same player id; for the whole time you were out it pointed at freed memory. It is harmless here because count_online never touches the stored character, but any code that used it would be reading freed memory.
Step 6 of 6: drop the reference on logout
A character pointer that outlives the character is the classic trap in a forge. Remove the entry in on_logout. Leave logins alone, so the count survives a logout.
Check. Compile, restart, log in and out. The log shows:
[Forge-login_counter]: pid 1234 logout, online 0
[QUEST_LOGOUT][INFO][1234] Logout PC OK!
Log in again: the chat says Login 2 and the log says login 2, online 1. The module name appears in three roles in this file: the quest name behind object/state/login_counter, the module you reach as forge.game.login_counter, and the output file name. The quest table and the module are different tables.
You have built the whole module: a loaded environment, a counter fed by a hook, a message to the player and a clean logout. Press Solve to compare it with the final file.
Run it
The loop is the same after every change:
- Compile the file:
./qc.sh --f ../system/tutorial/login_counter.forge. A syntax error aborts with<file>:<line>:syntax error : ...on stderr. For an error inside theenvironmentbody the reported line is where the environment ends, not where the mistake is. - Restart the game cores:
sh shutdown.sh, thensh run.sh. The db does not need a restart for a game-only forge. In the local docker runtime, restart the containers. - Read
logs/syslogof a game core. At boot the forge prints[Forge-login_counter]: loaded; at login and logout it prints the lines above.logs/syserrshould have nothing aboutlogin_counter.
There is no hot reload for forges. A changed forge is picked up only at the next core start, and a core restarts with its counts back at zero. A player who warps to another core starts at Login 1 there, because each game core has its own copy of the module.
If the module returns nothing, the file compiles, and the core refuses to start. The error is in logs/syserr:
Forge::ExcecuteSources result is nil for forge[login_counter]. Each environment should return a table or a class instance
could not load Forge: [./locale/italy/quest/forge]
Every forge after it in the load order is not loaded either. Put return Counter:new() back.
Pitfalls
stateandwithare reserved words in.forgefiles, as arequest,when,defineandbegin. The compiler rejects them as names, even after a dot.local state = {}orself.withfails the compile withsyntax error :and the word. Rename the field, or writet["state"]for a field you do not own.- Global
sys_log(1, ...)prints nothing on a live server: any level of 1 or more is dropped unless the server is a test server. Useforge.sys.log(0, ...)in a module. - Do not wait inside a hook.
wait(),select(...)orinput()inwhen loginends withQUEST NOT END RUNNING on Login/Logoutinlogs/syserr, and the block is closed. A module that has to wait starts a coroutine owned by the player. - A typo in
forge.game.login_countr:on_login(ch)compiles, because the compiler does not check calls. It fails at the first login withattempt to index a nil value (field 'login_countr'), and the server keeps running. forge.game.login_counter.on_login(ch)with a dot instead of a colon passes the character asself, and the first access toself.loginsfails.- Do not read another forge's module in the body of
environment game. Forges load in directory order, which is not guaranteed, so it works on one machine and kills the core on another.
Where to go next
- Forge overview and setup: files, environments, boot order and the full surface.
- How Forge modules work: how a forge is loaded, why state lives in the environment and how it differs from a quest.
- Make the counter survive a restart: it needs a db or a quest flag, which this module deliberately avoids.