Question about for loops

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
SquareFish
Prole
Posts: 2
Joined: Tue Jul 04, 2023 5:29 am

Question about for loops

Post by SquareFish »

I have this code here for removing a bunch of zombies in the zombies table, each of the values in zombies is a individual zombie table:
for i,z in ipairs(zombies) do
zombies [ i ] = nil (1)
z = nil (2)
end

My question is, why does line (1) work and line (2) doesn't? When I try line 2 nothing happens. So far I have the understanding that using z is like using a for each loop, and it points to the first value/object in the table, then the next, and so on. I was just wondering why z = nil doesn't work as opposed to zombies = nil. My best guess is the fact that it has to do with z not wanting to be null because it has another index in the loop to go on to or something like that

Thanks a lot guys, I never use for each loops in java so they're pretty confusing to me with lua
Last edited by SquareFish on Tue Jul 04, 2023 7:50 pm, edited 1 time in total.
User avatar
darkfrei
Party member
Posts: 1184
Joined: Sat Feb 08, 2020 11:09 pm

Re: Question about for loops

Post by darkfrei »

Wrong:

Code: Select all

for i,z in ipairs(zombies) do
  zombies = nil -- the table zombies does not exist anymore 
  z = nil -- local / global z does not exist
end
Good:

Code: Select all

for i = #zombies, 1, -1 do -- backwards from last to first
  zombies[i] = nil
end
Also good:

Code: Select all

for i, z in ipairs(zombies) do
  zombies[i] = zombies[#zombies] -- the i-th zombie is gone, now the i-th zombie is exactly-same as last
  zombies[#zombies] = nil -- remove the last
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
SquareFish
Prole
Posts: 2
Joined: Tue Jul 04, 2023 5:29 am

Re: Question about for loops

Post by SquareFish »

Thanks, I realized that the forum text editor removed the [ i ] that I added after the zombies on line one, I edited my post to add it in. Thanks a lot though, both these options make sense. I've had a lot of trouble with removing objects in a list in the past and for some reason it never occurred to me to just iterate backwards.
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Question about for loops

Post by Andlac028 »

SquareFish wrote: Tue Jul 04, 2023 7:56 pm Thanks, I realized that the forum text editor removed the [ i ]
Thats what [ code ] [/ code] tags are for (without spaces in [])
User avatar
darkfrei
Party member
Posts: 1184
Joined: Sat Feb 08, 2020 11:09 pm

Re: Question about for loops

Post by darkfrei »

Extra question: what happens?

Code: Select all

for i = 1, 3 do
  i = 4
  print (i)
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3582
Joined: Sun Oct 18, 2015 2:58 pm

Re: Question about for loops

Post by pgimeno »

Here's the OP's code again surrounded with proper [code]...[/code] tags:

Code: Select all

for i,z in ipairs(zombies) do
        zombies[i] = nil (1)
        z = nil (2)
end
The answer to understand what's going on in numeric loops and predict the behaviour is in the manual's section 2.4.5.
More precisely, a for statement like

Code: Select all

     for v = e1, e2, e3 do block end
is equivalent to the code:

Code: Select all

     do
       local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3)
       if not (var and limit and step) then error() end
       while (step > 0 and var <= limit) or (step <= 0 and var >= limit) do
         local v = var
         block
         var = var + step
       end
     end
[...]
  • var, limit, and step are invisible variables. The names shown here are for explanatory purposes only.
Note that altering v (the loop variable) is not going to influence the stop condition because it's stored in a new local with the same name after being assigned from var.

(Edited after realizing that I misunderstood a few things)
Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot] and 4 guests