I'm working on a game that utilizes tiles in a weird way. It's not your average kind of game where I could just make a nice default texture and focus 'the camera' on a certain section.Think of the game sort of like minecraft-- the terrain at the start is randomly generated and it's absolutely massive in terms of size. For example, I started getting "out of memory" errors when I wanted to make a 2D int array that had 10000 x 10000 indexes [is that the proper plural?]... I tried storing the tile number in an int[][] where the arrays were an int(x)(y) . Thus, I can't store all the tiles I want in a memory array. How would I go about storing the tiles in a file?Thus far they only have to be numbers, very basic. I could even write them as bytes at this point, but I wanted to keep integers to allow for more stuff later on.

Basically my game works like this-- or rather this is how I want it to work (if you know a better way I'll change it guaranteed, remember I am a noob ):- Interface draws tiles from top left to bottom right (2D flat game, no isometrics or anything). It draws 16 x 16 tiles on the screen.- Lets say it wants to draw from (x, y) down to (x + 16, y + 16), the code would go like:

Actually I probably did the height last and the width first, but either way: that the only way I can think of rendering a dynamic world.- NPCs and stuff would be in a different file or loaded into the game as an object directly instead and are drawn on the tiles (should I also store these in a file somewhere?)

I don't know how to go about properly formatting the tiles for storage. I heard XML files are great, but I don't know how feasible it is for it to read 16x16 tiles every 20ish milliseconds.

Are there any better ways to go about reading/writing/printing 2D tiles on an interface than how I've proposed? Please note that I'm still learning java, though I've done 2-3 other languages so I understand how syntax works... hope that helps!

XML files are not very good for massive amount of data because of the large markup overhead. Just use an RandomAccessFile, seek to the desired position and read bytes. If you need more than 256 possible values, read two bytes in a sequence, shift the first byte 8 bits and add the second to get an 16bit integer (65536 values).

Keep in mind that you get an OutOfMemory error because of the default max heap setting, not because of running out of "real" memory. See the -Xmx option of the java launcher.

Also if you really want to make a huge random level, you should think about splitting it in chunks, generate them on the fly and only store chunks on disk, that are already visited (thats afaik how minecraft does it)

16x16 for x/y and 256 for height/zBut if you are keeping it in x/y only then id personally recommend something a little larger than 16x16, perhaps 256, 1024, or 2048 per chunk. You can go higher, but just experiment. Once its written for dynamic chunks, changing chunk size is easy

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org