Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization of closures in groovy

I'm developing a game in Groovy and I'm thinking of using closures extensively to make architecture cleaner. For example, to implemented status effects (such as poisoning) the Player object will have a list of closures to execute each game turn. These will have to be serialized when saving the game.

Is it generally a good idea to store closures in objects that will need serialization? Or should I opt for a more traditional architecture (e.g. storing a list of StatusEffect objects)?

like image 875
ramirami Avatar asked Oct 09 '22 10:10

ramirami


1 Answers

Having a list of closures to execute each game turn sounds like a really nice idea :-)

Serialising Closures is perfectly possible. Since Groovy 1.8.5, it has been made easier as the two methods dehydrate and rehydrate were added to Closures (so that the owner, thisObject and delegate can be stripped before serialisation)

But I have issues with native java Serialisation for the saving of data. For sending short-lived data between systems, it can be great (but even then I would look at protocol buffers or thrift)

Consider what happens if you need to update your game? If there is a bug in the poisoned affect, then every user that has saved with the buggy poisoned closure in their save file will keep that bug until it wears off. In a multiplayer game, it would also be possible for people to manipulate their save game files to give themselves unexpected or unwanted powers (as the functionality for the powers themselves would be stored in the file). I could see manipulating a poison affect so it adds HP instead of removing them could be beneficial ;-)

In short, I guess what I'm saying is that I would write out a character sheet, with IDs for things that are affecting the user, inventory, score, etc, and then check and apply the closures as the file is read in.

like image 148
tim_yates Avatar answered Oct 12 '22 11:10

tim_yates