I need a little help. In brief, I need a table that will contain up to about 2100 immobile tiles. This table needs to be in some format to facilitate the following:
- Object lookup by x and y coordinates (with little to no overhead)
- Ability to randomly select an object
Or, to be more specific, this table must firstly give me an efficient way to know if a tile is on the screen or not (so love.draw() can know whether or not to bother drawing it), secondly to tell me if a tile is at a given coordinate, and finally, offer a way to choose a tile at random.
So far I've been using tiles[x][y] = tile. This lets me easily know if a tile exists at a coordinate by saying "if tiles[x][y] then". I can also tell if a tile is on the screen or not in the draw function by doing "for x, row in pairs(tiles) do if x > camera.x and x < camera.x+screen.x then". However, what I CAN'T do this way is choose a tile at random.
So, does anyone have any ideas?
Need help implementing robust tile table
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Need help implementing robust tile table
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Need help implementing robust tile table
Why can't you, just use math.random for both x and y.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Need help implementing robust tile table
What if there's no tile there? My current method of picking a tile at random does just that - chooses random coordinates until it hits a tile. But if a level is 10000x10000, this method gets far too expensive.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Need help implementing robust tile table
Use two tables then. A 2-dimensional table for 2d access and a 1-dimensional one that stores the tiles sequentially.
Code: Select all
-- the tables are called tiles(2d) and existingTiles(1d)
-- a new tile is inserted
tiles[x][y] = tile
table.insert(existingTiles, tile)
-- get the x,y tile
return tiles[x][y]
-- get a random tile
return existingTiles[math.random(#existingTiles)]
When I write def I mean function.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Need help implementing robust tile table
That's a good idea. I tried hacking something like that into my project and it worked quite well. I'll see if I can also use that for the more unusual features of my game.
However, wouldn't this use up a lot of memory?
However, wouldn't this use up a lot of memory?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Need help implementing robust tile table
It would, yes. It'd have to store 1000000 references in the table.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Need help implementing robust tile table
Tiles are not duplicated though.
Help us help you: attach a .love.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Need help implementing robust tile table
Still, it needs to store references, at best these are pointers, which makes them at least 32-bits, 4 bytes per entry, equals 4000000 bytes, including some extra table data that is 4 MB. Not shocking, but not best coding practice either.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Need help implementing robust tile table
Not quite. The area may be 10000x10000, but I'm not going to use more than about 2100 tiles or so (Love starts having issues at that point. Yes, the tile table consists of a lot of empty space). So that'd be more like 8kb?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Need help implementing robust tile table
It still isn't ideal, but manageable.
Who is online
Users browsing this forum: Google [Bot] and 6 guests