Alex Rivera | Logout

Immutable functional objects in highly mutable domain

Asked 2008-10-03T11:16:26.850
25

I'm currently learning functional programming in my spare time with Scala, and I have an idle newbie question.

I can see the elegance of having immutable objects when doing something like calculating a Haar wavelet transform - i.e. when the data itself being represented by the objects doesn't change.

But I saw a blog where someone had a small game as an example when demonstrating immutability. If a creature object recieved damage, it didn't change its state - it returned a new creature object with the new hitpoints and a new "aggro towards X" flag. But if we were to design something like a MMORPG, World of Warcraft say. A hundred players in a battleground... possibly thousands of attacks and buffing/debuffing spell effects affecting them in different ways. Is it still possible to design the system with completely immutable objects? To me it would seem like there would be a ginormous swarm of new instances each 'tick'. And to get the currently valid instance of objects, all clients would constantly have to go through some sort of central "gameworld" object, or?

Does functional programming scale for this, or is this a case of "best tool for best job, probably not immutable here"?

Edit
Report

1 Answer

4

An MMORPG is already an example of immutability. Since the game is distributed across servers and gamers' systems, there is absolutely not a central "gameworld" object. Thus, any object that gets sent over the wire is immutable — because it doesn't get changed by the receiver. Instead, a new object or message gets sent as a response, if there is one.

I've never written a distributed game so I don't know exactly how they're implemented, but I suspect that updates to objects are either computed locally or sent as diffs over the wire.

For example, you're playing Command & Conquer. Your mammoth tank is sitting in ready mode guarding your base. Your opponent approaches with a light tank to explore your base. Your mammoth tank shoots and hits your opponent's tank, causing damage.

This game is pretty simple, so I suspect a lot is computed locally whenever possible. Assume the two players' computers are initially in sync in terms of game state. Then your opponent clicks to move his light tank into your base. A message (immutable) is sent to you over the wire. Since the algorithm to move a tank is (probably) deterministic, your copy of Command & Conquer can move your opponent's tank on your screen, updating your game state (could be immutable or mutable). When the light tank comes in range of your mammoth tank, your tank fires. A random value is generated on the server (in this case, one computer is chosen arbitrarily as the server) to determine whether the shot hits your opponent or not. Assuming the tank was hit and an update to your opponent's tank must be made, only the diff — the fact that the tank's new armor level has decreased to 22% — is sent over the wire to sync the two players' games. This message is immutable.

Whether the object on either player's computer representing the tank is mutable or immutable is irrelevant; it can be implemented either way. Each player does not directly

answered 2008-10-03T12:59:40.610

Your Answer