Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is this code not making use of the variable in the for loop?

Asked by 8 years ago

I'm quit new to Lua Coding and just started on following the scripting tutorial made in the wiki. I'm a curious learner and like things to specific so everything would make sense. This is just a simple newbie question, so it would be really great if you can help me out :D

In the script tutorial: http://wiki.roblox.com/index.php?title=Making_an_Explosion_Course It gave this as it's finished code:

children = game.Workspace:GetChildren()
while true do
    for _, child in ipairs(children) do
        if child.Name == "ExplosionPart" then
            explosion = Instance.new("Explosion", game.Workspace)
            explosion.Position = child.Position
        end
    end
    wait(1)
end

On line 3 of this code, the author gave it 2 variables, which is a "" and "child", however he doesn't use the "" variable at all instead only uses 1 variable. Why is that?

Also, I would like to clarify what the variable is there for. What value is being given to the two variables, without knowing the content of the variable I can't really make use of what it is.

Sorry for poorly explaining it, my English is not great and I'm still new. I'm a curious learner and like things to make sense.

2 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

For loops

There are generally two types of for loops: numeric for, and generic for. While you can click on those links for a more in depth explanation on each one, here's a brief summary:

Numeric for

Numeric for loops will run it's body of code given a certain range of numbers. It only has one variable, which represents the increment that changes per iteration. Here's an example:

for i = 1,10 do
    print(i) -- print's what "i" equals each cycle, which is 1 greater than it's former.
end

The program above would print 1 through 10 each on different lines. There's also a third number you can provide, which is the step variant that determines how the variable i is incremented each iteration. You can read about that in the link given above.

Generic for

Generic for loops require some sort of iterative function that returns a key/value pair back to the for loop (this must be preformed on a table). This key/value pair is typically represented as i, v, where i is the index and v is the value. However, these are just variables and they can be named anything. Here's an example using the ipairs iterator from your code:

local list = {"a", "b", "c"}

for i,v in ipairs(list) do -- Cycles through table 'list', where 'i' is the index and 'v' is the value.
    print(i, v)
end

This will print the key/value pair of each element in the list. In order to get a better understanding of how key/value pairs work, I suggest checking out this explanation on tables.

Your code

In the case of the code you've provided, you're using a generic for loop with the same two variables (index, and value). The only difference is, your index variable is named with an underscore, instead of any other variable name. This is mostly for when the author of the program doesn't intend on using the index variable, and just labels it with an underscore as a place filler. Example:

local list = {"a", "b", "c"}

-- Here I only care about getting the values from this list, not the indices. So I'll just throw something that I'm not going to use as the variable for the index parameter.
for _, v in pairs(list) do
    print(v)
end

This is completely personal preference, and doesn't matter what the name of your variable is. It's just used as a universal way to represent a "blank" character, or a place filler.

0
Thanks so much :D, this showed me the difference between an index and value type variables and the universal "blank" variable. This helps a lot :D blueboy90780 74 — 8y
Ad
Log in to vote
0
Answered by
Notwal 2
8 years ago

The first variable, in this case '_', represents the position of the value in the table.

For example:

local t = {'stuff', 3, 'apple', 6, 20, 'example'}

for i, v in ipairs(t) do
    print(i,v)  --this simply prints one then the other
end

The output would be: (minus the commas) 1, stuff 2, 3 3, apple 4, 6 5, 20 6, example

0
Sorry, the output was mixed up horribly Notwal 2 — 8y
0
Sorry I don't understand, the output doesn't tell me what the value in I is or what the value in V is blueboy90780 74 — 8y

Answer this question