Using the markup engine to run some JavaScript is a fairly heavyweight way to initialize an object. If you’re set on using passages to hold your initialization code, then I’d probably suggest simply executing the the JavaScript directly.
Example getEntity
function: (goes in Story JavaScript)
window.getEntity = function getEntity(entityName) {
// Complain if the entity passage does not exist.
if (!Story.has(entityName)) {
throw new Error('no such entity: "' + entityName + '"');
}
try {
// Execute the entity passage and return the result.
return evalJavaScript('(' + Story.get(entityName).text + ')');
}
catch (ex) {
throw new Error('error initializing entity ("' + entityName + '"):' + ex.message);
}
};
Example foe_goblin
passage:
{
name: "A common goblin",
hp: 3
}
Example usage:
/* Init two gobbos. */
<<set
$gob1 to getEntity("foe_goblin"),
$gob2 to getEntity("foe_goblin")
>>
/* Init a wooden sword. */
<<set $sword to getEntity("item_wooden_sword")>>
You should get the idea.