Game Saving/Loading

Started by
14 comments, last by ToohrVyk 16 years, 6 months ago
Quote:Original post by ToohrVyk
However, must the controller objects necessarily interact with the model through polling? Is there any reason to forbid the Observer design pattern from being used? Listeners? Callbacks?


Not at all. Think of separate GameModel and GameModelData objects. The GameModel object applies changes to the data, updates observers, etc. It's only the data itself that gets saved.

Say you have AI methods in Character objects. Whatever is calling those methods (eg, GameModel::run) should be a publisher, not the Character itself (except perhaps in an intra-model context). This makes sense for a number of reasons, including efficiency, lack of extra complexity, and decoupling.

And just to be clear, pickle handles this kind of pattern without issue:

import pickleclass Observer:	def __init__(self, name):		self.name = name	def update(self, data):		print "%s: %s" % (self.name, data)class Publisher:	def __init__(self):		self.observers = []	def attach(self, o):		self.observers.append(o)	def notify(self, data):		for o in self.observers:			o.update(data)class World:	def init(self):		self.pub = Publisher()		self.ob1 = Observer('One')		self.ob2 = Observer('Two')		self.ob3 = Observer('Three')		self.pub.attach(self.ob1)		self.pub.attach(self.ob2)		self.pub.attach(self.ob3)	def do_something(self):		self.pub.notify('foo')def test_pickle():	w = World()	w.init()	fout = file('test.pkl', 'wb')	pickle.dump(w, fout)	fout.close()def test_unpickle():	fin = open('test.pkl', 'rb')	w = pickle.load(fin)	w.do_something()test_pickle()test_unpickle()


(edit) Sorry for mixing my terms...Observer/Subject, Publisher/Subscriber, it's all the same anyway.
Advertisement
Quote:Original post by drakostar


You seem to have misunderstood me. I never said anything about using callbacks/observers/publishers within the scope of the model—this is something which every single serialization package should get right, or burn a fiery death at my hands.

The problem comes when using callbacks as a lazy means of communication between a model object and a controller object. That is, instead of the controller polling a grenade object every update tick to see if an "explosion" sound needs to be played, he registers an "OnExplode" callback with the the grenade object that plays the sound. This kind of design (event-based) is both easier to understand and faster. An O'Caml example:
grenade # on_explode ++  (fun () -> sound # play "explosion.wav")


However, as soon as model objects start holding callbacks which refer to things outside the model, boom. This leaves you with three options:
  • Configure or alter the serialization system to ignore handlers somehow marked as "external". This is the approach I use.
  • Use some kind of indirect addressing-and-messaging system to handle callbacks.
  • Use only polling from controller to model.
Quote:Use some kind of indirect addressing-and-messaging system to handle callbacks.

That's more or less what follows from any kind of multi-threaded engine. To oversimplify a bit, the model thread would push a GrenadeExplode event to the input queues of any interested parties (renderer, sound, and physics).

Even in a single-threaded engine, it shouldn't be the Grenade object that's telling the sound manager it exploded...otherwise you're talking about each individual object knowing what kind of data format the sound manager expects. The Model would inform it of the event, perhaps after being notified by the Grenade object.
hi ViperG...
Why you don't save the values in file type a INI?
this is simple.
You don´t have problem with this, so you have be careful with the values you pass to this file...Ok?and you have interpreted this values where the exe is access again.
Ok?

good luck.
have a nice day.
hey jack,

thanks for the advice. I'm still researching the best way to do this (serialization vs standard text)

I can still save everything as a text file, it will just take longer me to to code that part, as i have to manually go through each struct and save all the variable types.

but with serialization, i have to set the serialization behavior anyways, so it would probably be more difficult to get working, but probably in less time.
Black Sky A Star Control 2/Elite like game
Quote:Original post by drakostar
Even in a single-threaded engine, it shouldn't be the Grenade object that's telling the sound manager it exploded...otherwise you're talking about each individual object knowing what kind of data format the sound manager expects.


You're confusing dynamic and static coupling. The Grenade object isn't telling the sound manager it exploded, it tells "whomever might care about it" that it exploded (just like any other event-based object), which creates absolutely zero coupling between grenade and sound manager.

This topic is closed to new replies.

Advertisement