[squeak-dev] #saveOnFile won't do

Matthew Fulmer tapplek at gmail.com
Fri Dec 12 01:35:52 UTC 2008

On Fri, Dec 12, 2008 at 02:16:13PM +1300, Aidan Gauland wrote:
> Hello,
>   I'm working on a game engine, and I found the #saveOnFile method in the 
> Object class, which I can use for saving the game state, but I also want to do 
> exactly the same thing for storing the initial game state (loaded when the 
> user starts a new game) with one exception: instead of writing to a file, I 
> want to store it internally (in the image).  I can't set up the game 
> programatically, because I use the Object Explorer for building the game 
> (which works very well for this purpose, I might add; you don't get that with 
> other programming languages).
>   Is there a way I can do this?

If you look at the implementation of saveOnFile:, most of the
work is done by ReadWriteStream >> fileOutClass:andObject:

so, you can use that method to write your object out to a
memory-backed ReadWriteStream rather than a disk-backed
FileStream. This will give you a ByteArray of the object, which
you can store as a class variable of your game or something.

If you go a little deeper, you will discover SmartRefStream,
which is the serializer responsible for this feature.

Other solutions:

you could create your game state, then send it the message #copy
to get another copy of the game state. You may have to customize
what copy does when sent to your game state instances, though.

you could just write out the initial game state to a file and
not worry about it.

If you want to distribute your game as a .mcz, you may want to
check out a brand new feature of monticello 1.5: the ability to
save resource files (like initial game state) inside .mcz

Or, you can just distribute the image with its initial state
inside, or in a bundled file

Matthew Fulmer -- http://mtfulmer.wordpress.com/

More information about the Squeak-dev mailing list