Hello everyone, I'm new in the community, Love2D and Lua (and programming), and as the title suggests, I have a question related to Lua's generic for. I'm having trouble "visualizing" it's aplication, I would like to help to put this subject into context, like, under what circumcstance will this be of use to me in game dev.
Cheers from Portugal.
Programming in Lua page > http://www.lua.org/pil/4.3.5.html
Lua Generic for
- Jasoco
- Inner party member
- Posts: 3725
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Lua Generic for
Every circumstance.
Pretty much every one. It will be used all the time.
Pretty much every one. It will be used all the time.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Lua Generic for
Generic for/pairs is used for iterating through any table which doesn't have sequential, integer keys (that is, the kind of table you'd use ipairs with). The place I think I've used it most is for handling game content (I put my content in tables, for cleanliness / iterating / etc.), but I've also used it for saving/loading data, combining/copying tables (example: The gene-merging function in my plant game), and component-oriented programming.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Lua Generic for
I'm not sure about what you are asking. It could be two things.
Are you asking "Why should I use a generic for instead of a numeric for?" or are you asking "Why should I use a for?".
Are you asking "Why should I use a generic for instead of a numeric for?" or are you asking "Why should I use a for?".
When I write def I mean function.
Re: Lua Generic for
Can I ask "Why should I use a generic for instead of a numeric one" and "How does generic for work"? I didn't read that part of your tutorial 'cause numbers don't confuse me and I'm fine with numeric ones, so I don't understand how do they work... (I tried to read it later but I don't understand it even after readin' it)
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: Lua Generic for
@kikito The question is actually "Why should I use the generic for?"
As I read the Lua book I'm writting it onto Love just to see it in action, like simple operations, etc etc, and now control structures, but I have not yet done anything complex so I'm not seeing the "big picture", I have compiled the example given in the book, it works fine but I didn't really understood its use based solely on that page.
Thanks for your responses so far.
As I read the Lua book I'm writting it onto Love just to see it in action, like simple operations, etc etc, and now control structures, but I have not yet done anything complex so I'm not seeing the "big picture", I have compiled the example given in the book, it works fine but I didn't really understood its use based solely on that page.
Thanks for your responses so far.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Lua Generic for
Ok.
Let me begin by saying that you almost never will need ipairs. Numeric fors are just more efficient.
If all tables where like this one:
... then you would only need numeric fors.
The problem is that not all tables are like letters. In fact lots of them are not.
The reason why it doesn't print anything is because #tableWithHoles is considered 0. Lua has no (predefined) way of calculating the length of a table with holes in it. With a generic for and pairs, you would have got only one line: 1000 => I'm in the 1000th position.
And of course, there is the case in which tables are not indexed by integers at all. For example, they can have strings as keys:
Generic fors are the only ones that will allow you to print them:
Going back to the begining: I said you almost never need ipairs. I can imagine one case in which it makes sense that it exists; when creating libraries that use iterators as a parameters to functions. But that's not something regular people need (I have done plenty of stuff with Lua and still have not needed it).
Regards!
Let me begin by saying that you almost never will need ipairs. Numeric fors are just more efficient.
If all tables where like this one:
Code: Select all
letters = { "a", "b", "c", "d" }
The problem is that not all tables are like letters. In fact lots of them are not.
- Tables indexed by numbers but with "holes" in them
- Tables indexed with negative numbers, or 0
- Tables indexed with non-nombers
Code: Select all
tableWithHoles = {}
tableWithHoles[1000] = "I'm in the 1000th position"
for i=1,#tableWithHoles do
print(i, "=>", tableWithHoles[i])
end
And of course, there is the case in which tables are not indexed by integers at all. For example, they can have strings as keys:
Code: Select all
person = { name = "peter", age = 21, job = "code monkey", likes = "cheetos & montain dew" }
Code: Select all
for k,v in pairs(person) do
print(k, " => ", value)
end
Regards!
When I write def I mean function.
Re: Lua Generic for
Thank you, you just told me that, if I want to solve an annoying bug from my game (if you shoot for more than 5 seconds on a netbook it will start to get slower), I've to use something that I hate... ![Sad :(](./images/smilies/ms-sad.png)
Since I still don't understand it, I think I'll wait before solving that bug
![Sad :(](./images/smilies/ms-sad.png)
Since I still don't understand it, I think I'll wait before solving that bug
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Lua Generic for
Have you tried uploading your game here? People are very helpful (I'd recommend replacing/removing long songs to make it small)
When I write def I mean function.
Re: Lua Generic for
Thank you very much for the explanation Kikito, It is all much clearer now. ![Smile :)](./images/smilies/ms-smile.png)
![Smile :)](./images/smilies/ms-smile.png)
Who is online
Users browsing this forum: Ahrefs [Bot] and 2 guests