2D array? 3D array? What?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Afr0
Prole
Posts: 19
Joined: Mon Feb 22, 2010 9:01 am

2D array? 3D array? What?

Post 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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post 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?
Afr0
Prole
Posts: 19
Joined: Mon Feb 22, 2010 9:01 am

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

Post 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 :\
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post 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
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

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

Post 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.
When I write def I mean function.
Afr0
Prole
Posts: 19
Joined: Mon Feb 22, 2010 9:01 am

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

Post 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!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

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

Post 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.
When I write def I mean function.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post by bartbes »

Yeah and it doesn't work within love, use love.filesystem.load("file")() instead.
Last edited by bartbes on Sat Feb 27, 2010 5:33 pm, edited 1 time in total.
Reason: Missed an entire word :O
Afr0
Prole
Posts: 19
Joined: Mon Feb 22, 2010 9:01 am

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

Post by Afr0 »

Thanks guys! :D
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests