Archive for August, 2008

Optimal code and Netbeans

Saturday, August 2nd, 2008

So I coded up visibility for my game.  It turned out to be a pretty simple task. As I think my approach is slightly novel I’ve documented it for anyone interested, feel free to comment! Hopefully it’s pretty optimal.

The next thing on my list was some really basic AI, I got a creature to be able to follow me around, I added a simple item class and a weapon class and made it possible to fight. All of which works nicely up to the point of dying – a the moment you can just carry on!

I then worked on some interface for the phones so you could open your inventory. A small learning process but not particularly daunting. The major drawback I can see of my chosen target platform (midp 1.0) is that if you create a list menu with implicit selection there is no way to know when the selected item changes. Which means you can’t create ‘soft button’ menu’s that vary on say the item type in your inventory list, instead you have to create a submenu based on selection. No big deal and I think something fixed in midp 2.0. If someone does know how to do this, please let me know.

Finally I decided that I should refactor my code. Originally the whole thing was based around simply using the mipd TileLayers to specify what things are where. However I wanted all of my game code to be orthogonal to the underlying display SDK it was running on – who knows I might port it to an applet or a standard Java game some time. So I went about building a Level class that represented the game world. In the process of doing this I discovered some interesting things about writing Java. If you create an array in a class as follows

int array[] = { 1, 2, 3, 4, 5, 6, 7, .... };

then that uses up a lot of memory as it has to produce byte code to load each value into each array slot. Then when it’s running you also need the space for the array itself. So it turns out if you want to create a large grid with tile indexes for each element you’re much better off saving out a data file and loading it up when you need it. Especially in the case of the code produced by the Netbeans game designer which just uses this array to initialize a tile layer – once that’s done the original array if effectively finished with and could be removed from memory. So as far as I can see the code the Netbeans game designer produces is almost as bad as it possibly could be when thinking about targeting a mobile device with a small amount of available memory… way to go!

Also of course rather than ints, in my case we’d be better off with bytes or shorts if I end up drawing more than 255 tiles (the zero index is reserved for an empty tile).

I’m now thinking I’ll either

  1. write my own tool to draw tile layers (hmm perhaps integrate it with eclipse as at the moment I still prefer it over Netbeans).
  2. find another tool that’s a bit smarter.
  3. improve the Netbeans game designer.
  4. not worry about it as I’m planning on randomly generating most of my content.

At the moment I’m going with 4!

Next up I think I’m going to look at that random level design.