Page 1 of 1

2D array? 3D array? What?

Posted: Sat Feb 27, 2010 1:53 pm
by Afr0
Alright, I needed a way to fill out my map array, but I couldn't understand the way the Löve filesystem worked, so I sort of hacked together my own file loading, which seems to work;

Code: Select all

--Must be called before TileMap:Draw()!
function TileMap:LoadMap(Path)
	local File = assert(io.open(Path, "r"))
	local Lines = {}
	
	for Line in File:lines() do
		Lines[Line] = File:read()
	end
	
	for i = 1,table.getn(Lines) do
		for j = 1,strlen(Lines[i]) do
		end
	end
end]
Now what I need to know is; How do I fill in the Map array from the Tilemap tutorial?

That array looks like this:

Code: Select all

map={
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
   { 0, 1, 0, 0, 2, 2, 2, 0, 3, 0, 3, 0, 1, 1, 1, 0, 0, 0, 0, 0},
   { 0, 1, 0, 0, 2, 0, 2, 0, 3, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0},
   { 0, 1, 1, 0, 2, 2, 2, 0, 0, 3, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
   { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0},
   { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 2, 2, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0, 0},
   { 0, 2, 0, 0, 0, 3, 0, 3, 0, 1, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0},
   { 0, 2, 0, 0, 0, 3, 0, 3, 0, 1, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0},
   { 0, 2, 2, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 2, 2, 2, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}
I've never seen an array declared this way before. How many dimentions does it have, and how do I fill it in?

My current map file looks like:

Code: Select all

11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111

Re: 2D array? 3D array? What?

Posted: Sat Feb 27, 2010 2:02 pm
by bartbes
Afr0 wrote:but I couldn't understand the way the Löve filesystem worked, so I sort of hacked together my own file loading
WTF
Anyway, let's take care of that first:

Code: Select all

--Must be called before TileMap:Draw()!
function TileMap:LoadMap(Path)
	local File = love.filesystem.newFile(Path)
        File:open("r")
The rest should be the same.

Then
Afr0 wrote:How many dimentions does it have
Just.. count.. you know, the structure is like this:
{ { } }, so 2 dimensions, wasn't that hard after all..
Afr0 wrote:and how do I fill it in?
Fill it in?

Re: 2D array? 3D array? What?

Posted: Sat Feb 27, 2010 2:11 pm
by Afr0
Yeah, how do I put the values into the array once I extract them from a line in the file with strsub()?

edit: My current Map array looks like:

Code: Select all

TileMap.Map = {}
Thinking about it, I'm guessing I'd have to pluck out two values at once, and then put them each into the X and Y dimension of the array?
I'm not sure how the code would look though :\

Re: 2D array? 3D array? What?

Posted: Sat Feb 27, 2010 2:17 pm
by bartbes

Code: Select all

        for y, t in ipairs(Lines) do --table.getn is deprecated, at least use the replacement (#)
                TileMap.Map[y] = {}
                for x, v in ipairs(t) do
                        TileMap.Map[y][x] = v
                end
        end
Probably

Re: 2D array? 3D array? What?

Posted: Sat Feb 27, 2010 2:24 pm
by kikito
I guess that what you are trying to do is this:

Code: Select all

TileMap.Map = {
  {0,0,0,1,1,0,1,0},
  {0,0,1,0,0,1,1,0},
  {0,1,0,0,0,0,1,0},
  {1,1,1,1,1,1,1,1},
  {0,1,0,0,0,0,1,0},
  {0,1,0,0,0,0,1,0},
  {0,1,0,1,1,0,1,0},
  {0,1,1,1,1,1,1,0},
}
This will give you a 2D (8x8) level with tiles resembling a house with a chimney.

You can make the level bigger than 8x8, and also use different numbers to code different tiles.

Re: 2D array? 3D array? What?

Posted: Sat Feb 27, 2010 2:28 pm
by Afr0
Thanks!

How come you're not using strsub() at all here?
ipairs() is kind of confusing to me, but the first loop seems to be iterating through all the lines, correct?
And then the second call, ipairs(t), iterates through every letter in the current line?

kikito: Yes I'm trying to do that, but I'm trying to load the values for the array from an external file!

Re: 2D array? 3D array? What?

Posted: Sat Feb 27, 2010 3:58 pm
by kikito
Afr0 wrote:kikito: Yes I'm trying to do that, but I'm trying to load the values for the array from an external file!
Have you considered using a lua file, then?

I mean, this could be your directory structure:

Code: Select all

[root]
  |
  +-- main.lua
  |
  `-- [levels]
      |
      +-- level1.lua
      |
      `-- level2.lua
In that case, if level1 had the code I just put on my previous post, you could do just this on main.lua:

Code: Select all

  dofile('levels.level1')
And then you would have the level loaded on the variable TileMap.Map.

Then if you want to load level two, you can do:

Code: Select all

  dofile('levels.level2')
Now the TileMap.Map variable will have level two.

Note: I use dofile instead of require just in case you want to load the same level more than once.

Re: 2D array? 3D array? What?

Posted: Sat Feb 27, 2010 3:59 pm
by bartbes
Yeah and it doesn't work within love, use love.filesystem.load("file")() instead.

Re: 2D array? 3D array? What?

Posted: Sat Feb 27, 2010 5:29 pm
by Afr0
Thanks guys! :D