Page 2 of 3

Re: Watch the stars fly!

Posted: Mon Nov 22, 2010 9:34 pm
by Kingdaro
Mud wrote:
Kingdaro wrote:I'm much more comfortable with using for i=1, #table [..] I never really bothered to get the concept of pairs/ipairs
Iterating arrays that way can be faster (eliminating iterator function call overhead), but only if you index the table you're iterating no more than ~1-2 times in the loop body, and it only works for arrays. For instance:

Code: Select all

for i=1, #stars do
   stars[i].y = stars[i].y+(speed*dt*(stars[i].size/2))
end

for i,star in ipairs(stars) do
   star.y = star.y+(speed*dt*(star.size/2))
end
The second loop is faster, because we eliminate 3 table look-ups, which saves more time than we lose from the iterator call (I also find it easier to read).

Code: Select all

for i=1, #stars do
   local star = stars[i]
   star.y = star.y+(speed*dt*(star.size/2))
end
That would work for me.
Mud wrote:
Kingdaro wrote:here's the demo in all it's beautified and updated glory. (See first post.)
The line "star.x = math.random(-640,1280)" is generating ~65% of your stars off-screen where they will never be seen.
That's part of the new feature I plan to add where you can move your mouse around the starfield to move around the starfield, per-say.

Re: Watch the stars fly!

Posted: Mon Nov 22, 2010 9:54 pm
by bartbes
Also, note that ipairs might get deprecated in lua 5.2.

Re: Watch the stars fly!

Posted: Mon Nov 22, 2010 10:02 pm
by Kingdaro
bartbes wrote:Also, note that ipairs might get deprecated in lua 5.2.
inb4peoplegoingape<censored>

It's kind of funny how this tiny little gadget got more attention than my other much bigger project.

Re: Watch the stars fly!

Posted: Mon Nov 22, 2010 10:26 pm
by thelinx
bartbes wrote:Also, note that ipairs might get deprecated in lua 5.2.
Actually, they changed their minds. ipairs is staying.

Re: Watch the stars fly!

Posted: Mon Nov 22, 2010 10:28 pm
by Kingdaro
thelinx wrote:
bartbes wrote:Also, note that ipairs might get deprecated in lua 5.2.
Actually, they changed their minds. ipairs is staying.
Okay that's good. I might consider using it one day, maybe not today. :)

Re: Watch the stars fly!

Posted: Mon Nov 22, 2010 10:31 pm
by thelinx
There's nothing wrong with using for 1,#table do instead of ipairs(table).

Re: Watch the stars fly!

Posted: Mon Nov 22, 2010 10:31 pm
by arquivista
thelinx wrote:
bartbes wrote:Also, note that ipairs might get deprecated in lua 5.2.
Actually, they changed their minds. ipairs is staying.
It's a good call, Love people use so much ipairs than a lot of programs had to be revised (or use old Loves) just to use new Love versions with new Lua.

Re: Watch the stars fly!

Posted: Mon Nov 22, 2010 10:56 pm
by zac352
I'm not sure if anyone noticed this, but HOURS? Minutes seems to better fit there. :?

Re: Watch the stars fly!

Posted: Mon Nov 22, 2010 11:00 pm
by Kingdaro
zac352 wrote:I'm not sure if anyone noticed this, but HOURS? Minutes seems to better fit there. :?
Explain further.

Re: Watch the stars fly!

Posted: Mon Nov 22, 2010 11:33 pm
by TechnoCat
Kingdaro wrote:
zac352 wrote:I'm not sure if anyone noticed this, but HOURS? Minutes seems to better fit there. :?
Explain further.
He is just being an arrogant fool.