Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Deloading data  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a 2d tile based (pokemon) game and I want to split the map up in to chunks. Let's say my player is in the middle of a 9x9 chunk and I only want to load the chunks around the player (so 9 chunks). If I step in to a chunk that is around the player, I want one chunck to unload ofcourse. Problem is I have no idea how I would unload that chunk. I load it using serialisation, but how would I erase all data of that chunk as if it were never loaded?
 
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is rather difficult to follow, but:-
If you have an object and allow it to go out of scope, so there are no more references to it, the garbage collection system will take care of it and delete it in due course.
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that would mean if I just do something like

it will eventually be deleted by the garbage machine?
 
Campbell Ritchie
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, as long as you have no other references to that chunk elsewhere.
Why are you using arrays like that? I can't see how you are going to match an array to a game board.
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an object Level, which contains a 2d array of Chunck objects. Chunk will have an array of 9x9 which will contain Tile objects. Is this not a good approach?
 
Campbell Ritchie
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know. Please explain how you are moving across the chunks. It isn't obvious how an array allows you to move. Also if you have indices like [5][2], how large is your array?
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This array will have a size of about [9[9], so 100 chunks. Each Chunk contains an array of [9][9] tiles and the player is in one of those tiles. When the player goes to a tile that is outside of it's chunk, it needs to load the next chunk and delete the previous Chunks.
 
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why use "chunks" when you could just have an array of size 81x81?  Then you wouldn't have to deal with any "loading" logic.
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that (actually tested it on a bigger level of 400x400 tiles) but I got an error about heap space. I thought this was because it would be to much to have 160.000 tiles in space.
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
160.000 doesn't seem too big. How much data does each Tile hold? Is this on an Android OS?
 
Campbell Ritchie
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Luc Drenth wrote:This array will have a size of about [9[9], so 100 chunks.  . . . .

You mean its largest indices will be [9][9], otherwise its size would be 81.
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Luc Drenth wrote:This array will have a size of about [9[9], so 100 chunks.  . . . .

You mean its largest indices will be [9][9], otherwise its size would be 81.


You're right, thanks for correcting me on that.

And no, it is a desktop application. Each tile holds an image of 64x64 pixels, 5 boolean values and 2 int values.
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that most of the images are not unique, meaning, the Tile would hold a reference to an image but many tiles could be sharing the same image. Do you have a guesstimate of how many unique images you would be dealing with?
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't know, I would think 1/7 but that would be a wild guess. (for reference, it's the pokemon Kanto region I tried copying it from).
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Luc Drenth wrote:Each tile holds an image of 64x64 pixels, 5 boolean values and 2 int values.


So, if an image is actually a reference to one of 7 images, and, assuming for the moment that booleans are stored as an int, then we have 7 ints, or roughly a total all together of 40 bytes per Tile (not counting overhead). Times 160.000 gives us approx. 6.4 mega-bytes which is not very large and shouldn't be causing out of memory errors.

I suspect that something else is going on in your implementation to suck up enough memory to cause an out of memory error.
 
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this array form the basis for drawing an image onto the display?
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll look into that then, since solving that possible problem would be much easier. Thanks a lot for your help! I really appriciate it.
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it does. I loop trough all the tiles and draw them to the screen.
 
Dave Tolls
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Luc Drenth wrote:Yes it does. I loop trough all the tiles and draw them to the screen.



Just wondering if that might be why your earlier version, with the array containing all 160,000 tiles, hit a memory issue.
Could you have been trying to draw the whole thing, rather than just a suitable window onto it?
 
Luc Drenth
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program crashed before even drawing it. I loaded in all default tiles and eventually it gave me the error.
The code I used for loading in the default tiles;
 
Campbell Ritchie
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than creating a new Image for each tile, try creating one Image and passing that to the Tile constructor.
 
Dave Tolls
Rancher
Posts: 3989
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah!

What you want to do in that case is load each file into a collection of images (a Map would be good, with a suitable enum key you can use later) then the Tile class either just holds a reference to the image, or the enum key (I prefer the latter as it reads a little better to me).  That way you only have a handful of images, and not 160,000 of them.

ETA: This is what Carey was talking about earlier.
 
Campbell Ritchie
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't notice that Carey mentioned that before me. Sorry.
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Luc Drenth wrote:The code I used for loading in the default tiles;


It looks like you are storing the image's width and height in two ints in your Tile objects. If all tile images are the same size then this shouldn't be necessary, a constant should suffice. Alternatively, an image object (e.g. BufferedImage) already stores this and can be retrieved with getWidth() and getHeight().
 
Hug your destiny! And hug this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!