love 0.9.0 tables
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 32
- Joined: Sat Dec 21, 2013 5:16 pm
love 0.9.0 tables
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.
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: love 0.9.0 table creation
That should still function as normal. What errors do you get? Can you show us some code?
-
- Prole
- Posts: 32
- Joined: Sat Dec 21, 2013 5:16 pm
Re: love 0.9.0 tables
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
- 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
Code: Select all
for generator do
-
- Prole
- Posts: 32
- Joined: Sat Dec 21, 2013 5:16 pm
Re: love 0.9.0 tables
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.
- 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
You are welcome but why did you write "generator" in the for loop ?
-
- Prole
- Posts: 32
- Joined: Sat Dec 21, 2013 5:16 pm
Re: love 0.9.0 tables
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
- 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
Here's the for loop you want:
ipairs:
pairs:
Click this link to see the difference between an "ipairs" a a "pairs" for loop.
ipairs:
Code: Select all
for i,v in ipairs(table) do
-- code
end
Code: Select all
for i,v in pairs(table) do
-- code
end
- CanYouReply
- Prole
- Posts: 10
- Joined: Sat May 16, 2020 7:33 pm
ipairs & pairs display issues in draw function
When I try to get the table content with 'pairs' as below :
I get an output in the display as:
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 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
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
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: love 0.9.0 tables
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?
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
your table has fields called x, y, width and height; pretty sure those aren't numbers, now are they?
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
by the way, you messed something up with the image, it doesn't show. :v
Me and my stuff True 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.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests