What can you do with per-thread JavaScript?
Create custom game mechanics, character stats, inventory systems, lore-based triggers, dynamic events, and interactive storytelling elements.
Advanced Examples:
Character stats system:
window.stats = {
health: 100,
mana: 50,
strength: 10,
takeDamage(amount) {
this.health -= amount;
console.log(`HP: ${this.health}/${100}`);
},
heal(amount) {
this.health = Math.min(100, this.health + amount);
}
};
Inventory system:
window.inventory = {
items: [],
add(item) {
this.items.push(item);
console.log(`Added ${item} to inventory`);
},
has(item) {
return this.items.includes(item);
},
remove(item) {
const index = this.items.indexOf(item);
if (index > -1) this.items.splice(index, 1);
}
};
Keyword-based lore lookups:
window.loreDatabase = {
'ancient artifact': 'A powerful relic from the old kingdom...',
'dragon': 'Fearsome creatures that guard treasure...',
'spell': 'Magic requires years of study...'
};
window.checkLore = (text) => {
for (let keyword in window.loreDatabase) {
if (text.toLowerCase().includes(keyword)) {
console.log(`Lore: ${window.loreDatabase[keyword]}`);
}
}
};
Random encounter system:
window.encounterSystem = {
encounters: [
{ name: 'Dragon', probability: 0.1 },
{ name: 'Merchant', probability: 0.3 },
{ name: 'Treasure', probability: 0.2 },
{ name: 'Nothing', probability: 0.4 }
],
roll() {
const rand = Math.random();
let cumulative = 0;
for (let enc of this.encounters) {
cumulative += enc.probability;
if (rand <= cumulative) {
console.log(`Encounter: ${enc.name}`);
return enc.name;
}
}
}
};
Quest tracking system:
window.quests = {
active: [],
completed: [],
start(questName) {
this.active.push({
name: questName,
objectives: [],
started: new Date()
});
},
complete(questName) {
const quest = this.active.find(q => q.name === questName);
if (quest) {
this.completed.push(quest);
this.active = this.active.filter(q => q.name !== questName);
}
}
};
Time/day cycle:
window.timeSystem = {
hour: 12,
day: 1,
advance(hours) {
this.hour += hours;
while (this.hour >= 24) {
this.hour -= 24;
this.day++;
}
console.log(`Day ${this.day}, ${this.hour}:00`);
},
isNight() {
return this.hour < 6 || this.hour >= 20;
}
};
Integration with Lorebooks:
You can create automatic lore lookups that trigger when specific keywords appear in the conversation:
Auto-lookup character names:
// Define known characters
window.knownCharacters = {
'Aria': 'A skilled mage from the northern kingdom',
'Gareth': 'A legendary warrior who fought in the Great War',
'Elara': 'The current queen of the realm'
};
// Hook to check messages
window.checkCharacterMentions = (message) => {
for (let name in window.knownCharacters) {
if (message.includes(name)) {
console.log(`${name}: ${window.knownCharacters[name]}`);
}
}
};
Important notes:
- Code executes immediately when the thread loads
- Use
window.variableName or window.functionName for global scope
- Access your functions/objects via browser console (F12) or hook them into DOM events
- Works in both regular threads and group chat host threads
- All code and state persists when you save/load the thread
- Use
console.log() to debug your code (check browser console)
- You can access and modify thread data, but be careful with the conversation history
Pro Tips:
- Combine with keyword triggers field for automatic lore/stat checks
- Use localStorage to save data that persists across sessions
- Create helper buttons in your code that modify hidden instruction field
- Build mini-games or decision trees that affect the narrative
- Track conversation metrics like word count, sentiment, or topic shifts