11/20/2010

The Erlang Game Loop

After my last blog post I took the time to signup for Linode and setup my application there.
After running a few tests it was apparent my initial game loop was flawed.
Even with the beefy hardware the erlang beam process was hitting 300% after around 50 areas.

My initial approach to the game loop was a bit naive. 
When loading each area I would spawn an erlang process with a game loop that would grab all the players in that area and process any new move flags, pathfinding, checking collisions, updating character positions, etc.

I figured that even though this was CPU intensive it shouldn't be too much of a problem.  I was wrong.

However, if you pay attention you'll notice I can easily improve this by having a global game loop, and processing all the players regardless of their area.

So, the old was was something like this in pseudo-code:

foreach (areas)
     load_layout_into_memory()
     bootstrap_ets_tables() 
     start_game_loop(area_id)
end

It took me no longer than 30 minutes to update the code.  Now it looks like this:

foreach (areas)
    load_layout_into_memory()
    bootstrap_ets_tables()
end
start_global_game_loop()

I then ran some tests on my Linode, first I had to set my ERL_MAX_ETS_TABLES to 7000.
I was able to load over 3000 areas with the beam process hovering around 10%.  Great!


It now seems like SolomonRPG should handle well for a good while.  There are a few things that need to be smoothed out, but for the most part everything seems to work.
With all these areas the only issue is now running out of memory but Linode has some good plans for adding more memory.  And although I haven't done much testing, thanks to Erlang, I think I can just add more nodes and have them process sets of players.
For example, have node1 process players 1 - 1000, node2 1001 - 2000, node 3....
Or perhaps separate them by area.  Either way should be easy to implement, damn Erlang rocks.

No comments:

Post a Comment