I've looked around a bunch into finding out exactly what it is that this command does (The only things I found were programs that used it, and explained what it did in the program, but not the command itself), and i've even used it in a few of my files, but never really knew what it did, just how to use it.
But what does it do..?
an ipair is a stateless iterator. ipairs iterates over all elements in an array. http://www.lua.org/pil/7.3.html

Code:

a = {"one", "two", "three"}
    for i, v in ipairs(a) do
      print(i, v)
    end


Are i and v previously declared with a value?

Or is i considered the beginning of the table, and v the end, using ipairs?
i will be the index, and v will be the value. So during that loop i would be 1, 2, 3, and v would be "one","two","three". Basically, each iteration of the loop, i and v get the next value of the index and value in the table, respectively.
Ah. I had guessed that the for loop was like others, and meant "for i to reach v do this."
Lua's awesome :3

Let's say I had:

Code:
local enemy = {}
enemy.x = math.random(8, 800-8)
enemy.y = 8
table.insert(tableofenemies, enemy)


Would:

Code:
for i, v in ipairs(tableofenemies) do
-- Display the enemy here, using love.
love.graphics.draw(enemyimage, enemy.x, enemy.y)
end


Work as displaying the enemy saved into the table?
No, I wouldn't expect so. First of all, you're not using i or v in your loop. You use enemy and enemyimage which aren't defined. If enemyimage is already known, something like this would be better:

Code:
for i, enemy in ipairs(tableofenemies) do
-- Display the enemy here, using love.
love.graphics.draw(enemyimage, enemy.x, enemy.y)
end
A big thanks, shaun & PCB. Soon i'll be doing alot more, I got my strive for programming back! Very Happy
Here's some snow, that I worked a little on, and i'll be playing around with snow and such until I know I can put together a game.

Again, thanks a bunch.
(I did this on a Mac, provided by my school, so to run it, if you want, you'll have to download love2d. http://www.love2d.org)

https://www.dropbox.com/s/jqqeqsakme1kkzz/Snow.love
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement