0 votes
by (150 points)


I've got a rather large data structure for my protagonist which I refer to as $hero

I use variables with properties (i.e., separated with dots, sometimes more than once) like $hero.health.current and $hero.health.maximum

For example

<<set $hero to []>>
<<set $hero.health to []>>
<<set $hero.health.current to 0>>
<<set $hero.health.maximum to 30>>

All of this occurs inside my StoryInit passage and everything is okay.

However, when I go to autosave my game progress I've noticed that it seems to be saving only SIMPLE variables, like $hello (ie. those without the dots).

I would like to be able to save my data structure for $hero to include all of the items belonging to it.

How can I do this? I've looked at the docs but nothing obvious jumps out at me that I'm doing wrong.

Does it have anything to do with making these objects serializable?

A code snippet of how I might be able to save the data inside of $hero would be helpful.

Thanks in advance.



2 Answers

+1 vote
by (44.7k points)
selected by
Best answer

All of an object's properties should all get saved as long as they're one of SugarCube's supported types.  However, if you change any variables after the passage is displayed (such as by the player pressing a button), then those changes will not normally get saved unless a passage transition occurs first.

So if, for example, you have a passage that allows the player to buy and sell items without triggering a page transistion, it may make more sense to autosave in the next passage that they go to, if the purpose of the autosave is to make sure they don't have to resell/repurchase everything if they have to reload.

That said, based on your code above, you simply have a problem where you're declaring your variables as arrays, instead of as generic objects.  It should work fine if you do this instead:

<<set $hero to {}>>
<<set $hero.health to {}>>
<<set $hero.health.current to 0>>
<<set $hero.health.maximum to 30>>

Just remember that brackets ("[]") are for declaring arrays, and curly braces ("{}") are for declaring generic objects.

Hope that helps!  :-)

by (150 points)
Thank you!!!! This is exactly the problem. I should have been using curly braces rather than square brackets.

Thanks to both of you who replied. Much appreciated!
–1 vote
by (1.1k points)

I am at the dentist at the moment so I cannot really go too deep on the answer... but Chapel has a great example of that in his Sword Generator, here 

I am certain the answer there will help.  It has certainly helped me!

Good luck. 

by (1.1k points)
Alas... I have been downvoted. But it does make sense... HiEv was like: {} done