Lua API Reference
Lua API Reference
Section titled “Lua API Reference”All TalesMUD scripting functions are available under the tales global object. These modules
are injected into every Lua script’s sandbox.
tales.items
Section titled “tales.items”-- Get item by IDlocal item = tales.items.get(itemID)
-- Find items by namelocal items = tales.items.findByName("sword")
-- Get item template by IDlocal template = tales.items.getTemplate(templateID)
-- Find item templates by namelocal templates = tales.items.findTemplates("sword")
-- Create item from templatelocal newItem = tales.items.createFromTemplate(templateID)
-- Delete an itemlocal success = tales.items.delete(itemID)tales.rooms
Section titled “tales.rooms”-- Get room by IDlocal room = tales.rooms.get(roomID)
-- Find rooms by namelocal rooms = tales.rooms.findByName("tavern")
-- Find rooms by arealocal rooms = tales.rooms.findByArea("dungeon")
-- Get all roomslocal allRooms = tales.rooms.getAll()
-- Get characters currently in a roomlocal characters = tales.rooms.getCharacters(roomID)
-- Get NPCs currently in a roomlocal npcs = tales.rooms.getNPCs(roomID)
-- Get items currently in a roomlocal items = tales.rooms.getItems(roomID)tales.characters
Section titled “tales.characters”-- Get character by IDlocal char = tales.characters.get(characterID)
-- Find characters by namelocal chars = tales.characters.findByName("hero")
-- Get all characterslocal allChars = tales.characters.getAll()
-- Get the room a character is currently inlocal room = tales.characters.getRoom(characterID)
-- Damage a character (reduce HP)local success = tales.characters.damage(characterID, amount)
-- Heal a character (increase HP)local success = tales.characters.heal(characterID, amount)
-- Teleport character to a roomlocal success = tales.characters.teleport(characterID, roomID)
-- Give XP to characterlocal success = tales.characters.giveXP(characterID, amount)tales.npcs
Section titled “tales.npcs”-- Get NPC by IDlocal npc = tales.npcs.get(npcID)
-- Find NPCs by namelocal npcs = tales.npcs.findByName("guard")
-- Find NPCs in a roomlocal npcs = tales.npcs.findInRoom(roomID)
-- Get all NPC instanceslocal allNPCs = tales.npcs.getAll()
-- Damage an NPClocal success = tales.npcs.damage(npcID, amount)
-- Heal an NPClocal success = tales.npcs.heal(npcID, amount)
-- Move NPC to a roomlocal success = tales.npcs.moveTo(npcID, roomID)
-- Check if NPC is deadlocal isDead = tales.npcs.isDead(npcID)
-- Check if NPC has the enemy traitlocal isEnemy = tales.npcs.isEnemy(npcID)
-- Check if NPC has the merchant traitlocal isMerchant = tales.npcs.isMerchant(npcID)
-- Delete an NPC instancelocal success = tales.npcs.delete(npcID)tales.dialogs
Section titled “tales.dialogs”-- Get dialog by IDlocal dialog = tales.dialogs.get(dialogID)
-- Find dialogs by namelocal dialogs = tales.dialogs.findByName("greeting")
-- Get all dialogslocal allDialogs = tales.dialogs.getAll()
-- Get the active conversation between a character and NPClocal conv = tales.dialogs.getConversation(characterID, npcID)
-- Set a context variable in a conversationlocal success = tales.dialogs.setContext(conversationID, "key", "value")
-- Get a context variable from a conversationlocal value = tales.dialogs.getContext(conversationID, "key")
-- Check if a dialog node has been visited in this conversationlocal visited = tales.dialogs.hasVisited(conversationID, nodeID)
-- Get how many times a node has been visitedlocal count = tales.dialogs.getVisitCount(conversationID, nodeID)tales.quests
Section titled “tales.quests”-- Get quest definition by IDlocal quest = tales.quests.get(questID)
-- Get all quest definitionslocal allQuests = tales.quests.getAll()
-- Accept a quest for a characterlocal progress = tales.quests.accept(characterID, questID)
-- Force-complete a questlocal progress = tales.quests.complete(characterID, questID)
-- Get quest progress for a characterlocal progress = tales.quests.getProgress(characterID, questID)
-- Set an objective's current progress valuelocal success = tales.quests.setProgress(characterID, questID, objectiveID, amount)
-- Check if a quest is currently activelocal isActive = tales.quests.isActive(characterID, questID)
-- Check if a quest is completedlocal isDone = tales.quests.isCompleted(characterID, questID)
-- Grant a quest to a character (bypasses source type check)local progress = tales.quests.grantQuest(characterID, questID)
-- Abandon an active questlocal success = tales.quests.abandon(characterID, questID)
-- Get all quest progress entries for a characterlocal allProgress = tales.quests.getQuestLog(characterID)tales.game
Section titled “tales.game”-- Send message to all players in a room-- NOTE: roomID must be the room's stored ID, not a display nametales.game.msgToRoom(roomID, "A rumbling sound echoes...")
-- Send message to a specific charactertales.game.msgToCharacter(characterID, "You feel a chill...")
-- Send message to a specific usertales.game.msgToUser(userID, "System message")
-- Broadcast to all connected playerstales.game.broadcast("Server announcement!")
-- Send message to room, excluding one charactertales.game.msgToRoomExcept(roomID, "Others see this.", excludeCharacterID)
-- Log to server logstales.game.log("info", "Something happened")
-- Game flags (per-character persistent key/value)tales.game.setFlag(characterID, "puzzle_solved", true)local val = tales.game.getFlag(characterID, "puzzle_solved")
-- Inventory managementlocal has = tales.game.hasItem(characterID, "ITM0001") -- by ID or TemplateIDtales.game.giveItem(characterID, "ITM0001") -- create from template and give
-- Equipment checkslocal equipped = tales.game.hasEquipped(characterID, "mainHand")
-- Collected item tracking (for CopyOnPickup items)local collected = tales.game.hasCollectedItem(characterID, "ITM0001")tales.game.resetCollectedItem(characterID, "ITM0001")
-- Hidden exit managementtales.game.revealExit(roomID, "north", characterID) -- reveal for one characterlocal revealed = tales.game.hasRevealedExit(characterID, roomID, "north")State Reconciliation Pattern
Section titled “State Reconciliation Pattern”When scripts reveal exits, store a flag so the state survives reconnection:
-- At the top of a room enter script, reconcile statelocal solved = tales.game.getFlag(charID, "puzzle_solved")if solved and not tales.game.hasRevealedExit(charID, roomID, "hidden_passage") then tales.game.revealExit(roomID, "hidden_passage", charID)endrevealExit is idempotent — calling it when already revealed is a no-op.
tales.utils
Section titled “tales.utils”-- Random integer (inclusive on both ends)local num = tales.utils.random(1, 100)
-- Random float between 0.0 and 1.0local f = tales.utils.randomFloat()
-- Generate a UUIDlocal id = tales.utils.uuid()
-- Current Unix timestamp (seconds)local now = tales.utils.now()
-- Current timestamp in millisecondslocal nowMs = tales.utils.nowMs()
-- Format a timestamp to stringlocal str = tales.utils.formatTime(timestamp)
-- Roll dice notation (e.g., "2d6", "1d20+5")local result = tales.utils.roll("2d6+3")
-- Percentage chance — returns true N% of the timelocal success = tales.utils.chance(25) -- 25% chance
-- Pick a random element from an arraylocal picked = tales.utils.pick({"sword", "axe", "spear"})
-- Shuffle an array (returns new array)local shuffled = tales.utils.shuffle(myArray)
-- Clamp a value between min and maxlocal clamped = tales.utils.clamp(value, 0, 100)
-- Linear interpolationlocal lerped = tales.utils.lerp(0, 100, 0.5) -- Returns 50Return Values
Section titled “Return Values”Most write functions return true/false (success/failure).
Read functions return the entity table or nil if not found.
When an item script modifies ctx.item, return the modified item:
local item = ctx.itemitem.minDamage = tales.utils.random(1, 5)item.maxDamage = item.minDamage + tales.utils.random(5, 15)return item -- Return modified entityNext Steps
Section titled “Next Steps”- Event Hooks — Available event types and contexts
- Examples — Complete working script examples
- Custom Mechanics — Patterns for complex systems