Thank you this is exactly what I was wondering, but what are j and i? I still have a very poor understanding of Lua tables and I have read the tutorial on the wiki a couple times nothing has really helped me understand what exactly all the table commands are and what they do.MarekkPie wrote:Well, if you just want to build a table of size WIDTH, HEIGHT, just:
At that point, then follow one of the algorithms in that wikipedia article. If that still isn't what you need, then I have no idea what you're asking.Code: Select all
map = {} for j, HEIGHT do for i, WIDTH do table.insert(map, {x = i, y = j, visited = false}) end end
building a map from mapsize variable.
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- forestwolf42
- Prole
- Posts: 24
- Joined: Tue Dec 20, 2011 5:18 pm
Re: building a map from mapsize variable.
Re: building a map from mapsize variable.
My bad. They are just iterators. I forgot to put j = 1 and i = 1.
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: building a map from mapsize variable.
I don't see how that grid creation mode is going to actually work vs. mine that puts each grid square in its own table cell with the X and Y being their indices. i.e. map[x][y].
Re: building a map from mapsize variable.
I already said yours was better
You can still do it...but it would require the modulus operator, and the modulus operator make me cry inside.
You can still do it...but it would require the modulus operator, and the modulus operator make me cry inside.
- forestwolf42
- Prole
- Posts: 24
- Joined: Tue Dec 20, 2011 5:18 pm
Re: building a map from mapsize variable.
Although this will probably be very useful later, I think I phrased my question poorly, I'm not trying to fill a grid with anything right now, I'm trying to create the grid. So in pseudo-codeJasoco wrote:As I said, you start with a grid of a certain size:
Code: Select all
sizeY = 5
sizeX = 5
map = magicfunction
map is
{
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1}
}
Code: Select all
map = {
{1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1}
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: building a map from mapsize variable.
Jasoco's code does this..
Re: building a map from mapsize variable.
Both of our methods do that. If you want to fill it with 1's, then simply make the node equal to 1.
Jasoco's:
A visual representation of Jasoco's:
Jasoco's:
Code: Select all
map = {}
for j = 1, HEIGHT do
map[j] = {}
for i = 1, WIDTH do
map[j][i] = 1
end
end
Code: Select all
[1][2][3]...[WIDTH]
[2]
[3]
.
.
.
[HEIGHT]
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: building a map from mapsize variable.
And I already told you exactly how to do that.forestwolf42 wrote:Although this will probably be very useful later, I think I phrased my question poorly, I'm not trying to fill a grid with anything right now, I'm trying to create the grid.Jasoco wrote:As I said, you start with a grid of a certain size:
Code: Select all
map = {}
for x = 0, mapWidth - 1 do
map[x] = {}
for y = 0, mapHeight - 1 do
map[x][y] = { marked = false }
end
end
It will be complicated. But in the end, worth it. My example .love was created as a Link's Awakening style dungeon engine with each room having 1 to 4 exits. North, south, east and west. And each is marked with a door or as solid and the door can be opened or closed. Etc... It's crappy code though and will probably be confusing.
It's how I create all my grids. And pretty much every game I've started making uses a grid. I've typed that same code sooooo many times it's gotten to the point I no longer hate doing it.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests