Twine Version: 2.3.16
Story Format: Sugarcube
Hi there!
I have some complex game on hands, so I isolated my problem in a much simplier example following
In example game we have two JS files:
Gladiator.js
window.Gladiator = class Gladiator {
constructor(name) {
this.name = name;
this.hp = 10;
this.skill = 0;
}
}
and Ludus.js
window.Ludus = class Ludus {
constructor() {
this.name = 'My Ludus';
this.gold = 0;
this.jobs = {
list: ['fight', 'train'],
options: {
'train': {
jobName: 'Make gladiators train',
execute() {
variables().gladiator.hp--
variables().gladiator.skill++
},
},
'fight': {
jobName: 'Make gladiators fight',
execute() {
variables().gladiator.hp--
this.ludus.gold += variables().gladiator.skill
},
},
},
};
}
}
and three Passages in Twine .xml
Start
<<script>>
importScripts(
["C:/dev/arena/Gladiator.js",
"C:/dev/arena/Ludus.js",
]
);
<</script>>
[[makeGladiators]]
makeGladiators
<<script>>
variables().gladiator = new Gladiator('Alice');
<</script>>
<<script>>
variables().ludus = new Ludus();
<</script>>
<<goto 'Ludus'>>
and Ludus
Ludus gold: $ludus.gold
$gladiator.name
HP: $gladiator.hp
Skill: $gladiator.skill
<<link '$ludus.jobs.options.train.jobName' 'Ludus'>>
<<= $ludus.jobs.options.train.execute()>>
<</link>>
<<link '$ludus.jobs.options.fight.jobName' 'Ludus'>>
<<= $ludus.jobs.options.fight.execute()>>
<</link>>
I got error when trying to exeute() job. “variables not defined”. As far as I know this is due to JS modules have no information about Twine variables.
Yes, I know that this code is excesive to make such a simple task, but as I said before it’s just an example and in my actual game I really need multiple JS modules work tougether. So, how can I make State.variables to be visible to JS modules here? Or maybe get another walkaround this problem.
I allso would be glad to use Twine either() function inside my JS modules (not to write it myself from scratch in a module file). And this also can be solved if JS modules could see Twine variables and methods.