## Watch the stars fly!

Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

### Re: Watch the stars fly!

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.

bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Watch the stars fly!

Also, note that ipairs might get deprecated in lua 5.2.

Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

### Re: Watch the stars fly!

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.

thelinx
The Strongest
Posts: 848
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

### Re: Watch the stars fly!

bartbes wrote:Also, note that ipairs might get deprecated in lua 5.2.
Actually, they changed their minds. ipairs is staying.

Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

### Re: Watch the stars fly!

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.

thelinx
The Strongest
Posts: 848
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

### Re: Watch the stars fly!

There's nothing wrong with using for 1,#table do instead of ipairs(table).

arquivista
No longer with us
Posts: 266
Joined: Tue Jul 06, 2010 8:39 am
Location: Insert Geolocation tag here
Contact:

### Re: Watch the stars fly!

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.
--------------------------------------------------------
To Do: Insert Signature Here
--------------------------------------------------------

zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

### Re: Watch the stars fly!

I'm not sure if anyone noticed this, but HOURS? Minutes seems to better fit there.

Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

### Re: Watch the stars fly!

zac352 wrote:I'm not sure if anyone noticed this, but HOURS? Minutes seems to better fit there.
Explain further.

TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

### Re: Watch the stars fly!

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.

### Who is online

Users browsing this forum: No registered users and 25 guests