love 0.9.0 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.
thewifitree
Prole
Posts: 32
Joined: Sat Dec 21, 2013 5:16 pm

love 0.9.0 tables

Post 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?
Last edited by thewifitree on Sun Jan 05, 2014 5:15 pm, edited 1 time in total.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: love 0.9.0 table creation

Post by DaedalusYoung »

That should still function as normal. What errors do you get? Can you show us some code?
thewifitree
Prole
Posts: 32
Joined: Sat Dec 21, 2013 5:16 pm

Re: love 0.9.0 tables

Post 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
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: love 0.9.0 tables

Post by Ranguna259 »

Code: Select all

for  generator do
I've never seen that in lua :?
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
thewifitree
Prole
Posts: 32
Joined: Sat Dec 21, 2013 5:16 pm

Re: love 0.9.0 tables

Post by thewifitree »

Thank you! But it still creates an error
Last edited by thewifitree on Sun Jan 05, 2014 5:36 pm, edited 1 time in total.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: love 0.9.0 tables

Post by Ranguna259 »

You are welcome but why did you write "generator" in the for loop ?
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
thewifitree
Prole
Posts: 32
Joined: Sat Dec 21, 2013 5:16 pm

Re: love 0.9.0 tables

Post 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 :?
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: love 0.9.0 tables

Post 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.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
CanYouReply
Prole
Posts: 10
Joined: Sat May 16, 2020 7:33 pm

ipairs & pairs display issues in draw function

Post 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 
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love 0.9.0 tables

Post 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
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: Amazon [Bot], Bing [Bot] and 3 guests