0 votes
by (590 points)

I searched everywhere for an answer to this, but I failed to find anybody attempting the same thing. I want to *set* a variable through the *loadgame* command, such as, 

<<link "Load">><<save.get()>><<set $var to 1>><</save>><</link>>

I don't think I'm using the Save API correctly but I'm not sure how to use it in Sugarcube, I had been trying to do it in Harlowe but I couldn't make the *set variable* work there through a load either. Well, in Harlowe it at least *set* the variable, but then I couldn't *call* it later because it disappeared. 

Thank you for any help you can offer. Sorry, I'm a n00b at coding... 

1 Answer

+1 vote
by (44.7k points)

I'm not entirely clear on what you're asking for, so if this doesn't answer your question, please let me know.

If you want to set a variable upon game load then you should create a load function for Config.saves.onLoad in your JavaScript section like this:

Config.saves.onLoad = function (save) {
	save.state.history[save.state.index].variables.var = 1;

That would set $var to 1 whenever a game is loaded.  See the save object definition for details.

Also, if you do want to use the Save.get() function in your passage, you can do that like this:

<<set saveObject = Save.get()>>

(The example code in this part of the documentation is for use in JavaScript, so you have to do something like the above if you want to use it in Twine/SugarCube code.)

Note that the Save.get() function doesn't load a game, it just gets you a copy of the save data object.  If you want to load a particular save game, then you'd do this:

<<run Save.slots.load(1)>>

and that would load the game from slot #1.

Hope that helps!  :-)

by (590 points)
Awesome, thank you, that was exactly what I was looking for!! :D