Page 1 of 2

love 0.9.0 tables

Posted: Sun Jan 05, 2014 5:12 pm
by thewifitree
I just got love 0.9.0 and it is amazing! But, I can't use the for i, v in pairs line anymore. Has it been replaced?

Re: love 0.9.0 table creation

Posted: Sun Jan 05, 2014 5:15 pm
by DaedalusYoung
That should still function as normal. What errors do you get? Can you show us some code?

Re: love 0.9.0 tables

Posted: Sun Jan 05, 2014 5:16 pm
by thewifitree
this is my code,

Code: Select all

function love.load()
	text = "COMMAND:"
	block = {}
	block.x = 20
	block.y = 20
end

function love.textinput(t)
	text = text .. t
end

function love.draw()
	love.graphics.printf(text, 0, 0, love.graphics.getWidth())
	for  generator do
		love.graphics.rectangle("fill", block[i].x, block[i].y, 10, 10)
	end
end

function love.update(dt)
	x, y = love.mouse.getPosition()
	table.insert(block,{x = x, y = y, dir = dir})
end

Re: love 0.9.0 tables

Posted: Sun Jan 05, 2014 5:27 pm
by Ranguna259

Code: Select all

for  generator do
I've never seen that in lua :?

Re: love 0.9.0 tables

Posted: Sun Jan 05, 2014 5:30 pm
by thewifitree
Thank you! But it still creates an error

Re: love 0.9.0 tables

Posted: Sun Jan 05, 2014 5:34 pm
by Ranguna259
You are welcome but why did you write "generator" in the for loop ?

Re: love 0.9.0 tables

Posted: Sun Jan 05, 2014 5:38 pm
by thewifitree
The error message made it sound like i, v in pairs was replaced with for generator by the error messege when i ran the code but I now noticed i neede to put bloc in parenthesis. derp :?

Re: love 0.9.0 tables

Posted: Sun Jan 05, 2014 9:35 pm
by Ranguna259
Here's the for loop you want:
ipairs:

Code: Select all

for i,v in ipairs(table) do
 -- code
end
pairs:

Code: Select all

for i,v in pairs(table) do
  -- code
end
Click this link to see the difference between an "ipairs" a a "pairs" for loop.

ipairs & pairs display issues in draw function

Posted: Sat May 30, 2020 7:24 pm
by CanYouReply
When I try to get the table content with 'pairs' as below :

Code: Select all

temptable = {}
temptable.x = 10
temptable.y = 20
temptable.width = 40
temptable.height = 30
	

function love.draw()
	local coXX = 200
	for i,v in pairs(temptable) do
		love.graphics.print("value of i : ", 120, 220)		
		love.graphics.print("value of v : ", 120, 240)
		love.graphics.print(i, coXX, 220)		
		love.graphics.print(v, coXX, 240)
		coXX = coXX + 50
	end
end 
I get an output in the display as:
Image

However, in the same code when I use 'ipairs' as below it gives no display in love2d. Why ? This question may be very basic but I am new to Love2d. Thanks in advance.

Code: Select all

temptable = {}
temptable.x = 10
temptable.y = 20
temptable.width = 40
temptable.height = 30
	

function love.draw()
	local coXX = 200
	for i,v in ipairs(temptable) do
		love.graphics.print("value of i : ", 120, 220)		
		love.graphics.print("value of v : ", 120, 240)
		love.graphics.print(i, coXX, 220)		
		love.graphics.print(v, coXX, 240)
		coXX = coXX + 50
	end
end 

Re: love 0.9.0 tables

Posted: Sat May 30, 2020 7:27 pm
by zorg
because ipairs works on integer keys and pairs barfs out all keys with an undefined order.

your table has fields called x, y, width and height; pretty sure those aren't numbers, now are they? :3

Code: Select all

local tbl = {'these', 'would', 'all', 'be'}
table[5] = 'shown'
table[7] = 'order'
table[6] = 'in'

function love.draw()
  for i,v in ipairs(tbl) do
    love.graphics.print(v, 0, (i-1)*12)
  end
end
That should print one word per line: "these would all be shown in order".

by the way, you messed something up with the image, it doesn't show. :v