Hello everyone, I'm currently trying to get a really simple game running and would like to use a JS class based approach to encapsulate variables and functions.
This is my barebones javascript code I'm trying to get running before I expand on it. It's the only existing part to keep it simple.
class CPlayer {
constructor() {
this.name= "testplayer";
this.age= "20";
}
test(){
return "random test string";
}
}
setup.CPlayer = CPlayer;
This is the single .tw file I use (I omitted storytitle and storysettings as they're irrelevant for the problem). By invoking it like this, I hope to receive a gobally available $player token I can then use everywhere to set/get it's member variables and to use it's functions.
:: Start
<<if ndef $player>><<set $player = new setup.CPlayer>><</if>>
<<goto TestPage>>
::TestPage
<<-$player.name>>
<<-$player.test()>>
My problem is that, when I simply open the compiled html file, everything works perfectly fine, but once I either do a manual browser refresh (F5 or derivatives) or save and then load the game. I'm greeted with
Error: <<->>: bad evaluation: State.variables.player.test is not a function
I only recently started with twine/tweego/sugarcube and I'm certainly not an expert when using JS. I'm pretty sure I've read something about not all objects being supported by twine and that they need to implement their own .clone() function to be able to be saved/restored, but I'm at a loss at how to achieve that, as the documentation isn't really helping.
I'm not exactly sure if the .clone() approach would be the solution to this problem, or if my problem comes from something else I'm missing.