a couple questions about tables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
RandomGameFun
Prole
Posts: 1
Joined: Sun Apr 30, 2023 5:35 am

a couple questions about tables

Post by RandomGameFun »

I have this from the tutorial section here.
https://love2d.org/wiki/Tutorial:Gridlocked_Player

What I'm trying to do is randomize this bit of code, it don't have to be in any specific order at all.

Code: Select all

	map = {
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } 
	}
I can't seem to grasp tables and maybe loops, I have read and watched videos on the two subjects but I still can't seem to figure out where the maker of the tutorial is getting the x and y in this line.
(It's in the draw function)

Code: Select all

	if map[y][x] == 1 then
as for my problem with loops, here is where I got my loop definition
https://www.tutorialspoint.com/lua/lua_for_loop.htm
https://www.tutorialspoint.com/lua/lua_tables.htm

Code: Select all

	for init,max/min value, increment
	do
   		statement(s)
	end
I don't know that init would be i equals what ever i want the max increment, what i don't know is max/min value?

What i had in mind was taking the large map table and randomly generating each section, the same was it's displayed above, so something like

Code: Select all

--for loop is creating however many sections i need inside the map table to make a map
for i = 10, 1, -1 
do
	mapSection = {}
	mapSection.i = math.random(0,1)
	table.insert (map, i, mapSection)
end

Here is my full code I'm running, I took out all the player code, all i needed was to just draw the squares for debugging.

Code: Select all

function love.load()
	-- we need to create tables like this in a loop
	--then inject them into a map table.
	map = {}
	for i = 10, 1, -1 
	do
		mapSection = {}
		mapSection.i = math.random(0,1)
		table.insert (map, i, mapSection)
	end

	world = map

end
function love.draw()
	for y=1, #world do
		for x=1, #world[y] do -- 1-wall, 2-tree, 3-water, 4-rock   r,g,b
			if world[y][x] == 1 then
				love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
			end
		end
	end 
end	
If your interested here is what I have done with this example so far, I'm hoping to only have one map table and be able to generate a new one when i hit the escape key.

Code: Select all

function love.load()
		allMaps = {
			map1 = { -- 1-wall, 2-tree, 3-water, 4-rock, 5-door
				{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1 }, -- this one will always be a wall just about
				{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1 }, -- only the first and last one will be a wall, everything in between will be random?
				{ 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1 },
				{ 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 1 },
				{ 1, 0, 0, 0, 2, 0, 0, 4, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5 },
				{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 4, 0, 3, 3, 0, 0, 0, 0, 4, 0, 1 },
				{ 1, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 5 },
				{ 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1 }  -- this one will just about always  be 1 maybe unless a door?
			},
			map2 = {
				{ 1, 1, 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, -- this one will always be a wall just about
				{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },-- only the first and last one will be a wall, everything in between will be random?
				{ 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
				{ 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
				{ 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1 },
				{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1 },
				{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
				{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }  -- this one will just about always  be 1 maybe unless a door?
			},
			map3 = {
				{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, -- this one will always be a wall just about
				{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 }, -- only the first and last one will be a wall, everything in between will be random?
				{ 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5 },
				{ 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }  -- this one will just about always  be 1 maybe unless a door?
			},
			map4 = {
				{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 5, 1, 1, 1 }, -- this one will always be a wall just about
				{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, -- only the first and last one will be a wall, everything in between will be random?
				{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }  -- this one will just about always  be 1 maybe unless a door?
			},
			map5 = { -- 1-wall, 2-tree, 3-water, 4-rock
				{ 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 3, 3, 3, 1 }, -- this one will always be a wall just about
				{ 3, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1 }, -- only the first and last one will be a wall, everything in between will be random?
				{ 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1 },
				{ 3, 3, 3, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 3, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 1 },
				{ 3, 0, 0, 0, 2, 0, 0, 4, 0, 0, 0, 0, 0, 1 },
				{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
				{ 3, 0, 0, 4, 0, 3, 3, 0, 0, 0, 0, 4, 0, 1 },
				{ 3, 3, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 1 },
				{ 3, 3, 3, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1 }  -- this one will just about always  be 1 maybe unless a door?
			}
		}
		math.randomseed(os.time()*360)
		mapSelect = math.random(1,5) ------------------make sure to put the last map# here!
		if mapSelect == 1 then
			world = allMaps.map1
		elseif mapSelect == 2 then
			world = allMaps.map2
		elseif mapSelect == 3 then
			world = allMaps.map3
		elseif mapSelect == 4 then
			world = allMaps.map4
		end
end
	player = {
		x = 50,
		y = 50,
		width = 32,
		height = 32,
		grid_x = 256,
		grid_y = 256,
		act_x = 200,
		act_y = 200,
		speed = 32
	}
function love.update(dt)
	player.act_y = player.act_y - (player.act_y - player.grid_y) * player.speed * dt
	player.act_x = player.act_x - (player.act_x - player.grid_x) * player.speed * dt
	
end

function love.draw()
	love.graphics.rectangle("fill", player.act_x, player.act_y, player.width, player.height)
	for y=1, #world do
		for x=1, #world[y] do -- 1-wall, 2-tree, 3-water, 4-rock   r,g,b
			if world[y][x] == 1 then
				love.graphics.setColor(1,1,1,1)
				love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
				love.graphics.setColor(1,1,1,1)
			elseif world[y][x] == 2 then
				love.graphics.setColor(0,1,0,1)
				love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
				love.graphics.setColor(1,1,1,1)		
			elseif world[y][x] == 3 then
				love.graphics.setColor(0,0,1,1)
				love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
				love.graphics.setColor(1,1,1,1)	
			elseif world[y][x] == 4 then
				love.graphics.setColor(.5,.5,.5,1)
				love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
				love.graphics.setColor(1,1,1,1)			
			elseif world[y][x] == 5 then
				love.graphics.setColor(.04,.03,0,1)
				love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
				love.graphics.setColor(.9,.9,0,1)
				love.graphics.circle("fill", x * 32+5, y * 32+16, 5)
				love.graphics.setColor(1,1,1,1)			
			end
		end
	end
	--reset the color
	love.graphics.setColor( 1, 1, 1, 1 )	 
end
function testMap(x, y)
	--0 false, 1 true, 2 door
	--if tile is 1-4
	if world[(player.grid_y / 32) + y][(player.grid_x / 32) + x] >= 1 and world[(player.grid_y / 32) + y][(player.grid_x / 32) + x] < 5 then
		return 0
	--else if tile is 5
	elseif world[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 5 then
		return 2
	end
	return 1
end

function love.keypressed(key)
	if key == "up" then
		if testMap(0, -1) == 1 then
			player.grid_y = player.grid_y - 32
		end
	elseif key == "down" then
		if testMap(0, 1) == 1 then
			player.grid_y = player.grid_y + 32
		end
	elseif key == "left" then
		if testMap(-1, 0) == 1 then
			player.grid_x = player.grid_x - 32
		end
	elseif key == "right" then
		if testMap(1, 0) == 1 then
			player.grid_x = player.grid_x + 32
		end
	elseif key == "escape" then
		love.load()
	end
end
User avatar
darkfrei
Party member
Posts: 1196
Joined: Sat Feb 08, 2020 11:09 pm

Re: a couple questions about tables

Post by darkfrei »

1. Define each map
2. Define allMaps as list: {map1, map2, map3, map3, map5}
3. Amount of maps is #allMaps
4. world = allMaps[mapSelect]

Also do the iterations as for i = 1, 10 do; for i = 18, 4, -1 do; for i = 1, #list-1, 2 do;
:x but never for i = 1, 2, 0.1 do, the float error makes bad things.
Be sure that the tileX, tileY is rounded as math.floor (floatX+0.5), math.floor (floatY+0.5)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: a couple questions about tables

Post by zorg »

RandomGameFun wrote: Sun Apr 30, 2023 5:55 am I can't seem to grasp tables and maybe loops, I have read and watched videos on the two subjects but I still can't seem to figure out where the maker of the tutorial is getting the x and y in this line.
(It's in the draw function)

Code: Select all

	if map[y][x] == 1 then
Those are the loop variables, in the two for loops:

Code: Select all

function love.draw()
	love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)
	for y=1, #map do -- y is defined here, goes from map[1] to map[#map], or the map's length.
		for x=1, #map[y] do -- x is defined here, goes from 1 to #map[y], or the current map table's length depending on the above loop.
			if map[y][x] == 1 then
				love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
			end
		end
	end
end
The map table itself is a two-dimensional table in that it's multiple tables (representing either columns or rows) with values (representing either rows or columns); one loop goes through the tables, the other goes through the values in the specific tables.
I don't know that init would be i equals what ever i want the max increment, what i don't know is max/min value?
max/min is essentially the limit where you want the loop to stop; it would be the minimum value if your increment is negative, or maximum if your increment is positive... this should be logical, like, if you think about it, "for i=0,10,-1" would not run at all, since you can't add minus one to zero and expect it to ever equal ten.
What i had in mind was taking the large map table and randomly generating each section, the same was it's displayed above, so something like <code>
You can't use the dot syntax to access a table field if the field's name (key) is not a string that starts with the characters a-zA-Z or _; you need to use the [] syntax for that.

Code: Select all

for i = 10, 1, -1 -- also why are you iterating backwards? just do: for i=1, 10 and you can lose the ,1 since that's default behaviour.
do
	mapSection = {}
	mapSection[i] = love.math.random(0,1) -- use love's method; this does give you a fractional value between 0 inclusive and 1 exclusive.
	table.insert (map, i, mapSection) -- or just do: map[i] = mapSection; here, it's the same.
end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests