Game programming

Friday, October 16, 2009

We’ve just recently completed an online game for a client, and learned a whole lot in the process.

For this I dove into MySQL, which I’d never really had an excuse to get deeply into. I also, finally, got to use session variables extensively.  I’d programmed both session variables and MySQL before, but being a game this really pushed it all together.

I’ll throw a couple simple tips out, nothing Earth shatteringly new, just things that pop into my head.

First off, MySQL doesn’t really store array variables – so how could I store a number of array flags and variables?  My first thought was to (slowly) loop through and make set variables like: variable1, variable2, etc.  Obviously that would be quite painful.   Especially since some of my arrays had over 200 flags and mixed keys!

Then I remembered the handy “serialize” and “unserialize” functions in PHP.

These functions take arrays and convert them to strings, or take a string and convert it to a set of arrays.  It really is quite wonderful!  So you can take:

$my_array=array( 1=>”oopsie”, “key2″=>”oopsie2″);

and through:

$store_this=serialize($my_array);

turn it into a beautiful string which is easily stored, and keeps all the keys too!

Conversely:

$my_array=unserialize($store_this);

brings it all back into array form.  You don’t even need to use the same array name, but all the keys and values will remain.

Categories: Programming
Tags: , ,
Posted by: David

Comments

No Comments

Leave a reply

Name *

Mail *

Website