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

Can someone explain i,v in pairs? Automatic inventory slot construction help?

Asked by 6 years ago

So recently I've been working on a new simulator, and I've come to the point of making an inventory. I can't seem to figure out how to make slots in an inventory based on another value. I've tried many simple lines of code that would make new frames inside another frame, but the frames didn't even show up, which would raise the question of whether or not my for/do loop is written correctly. Here is what I put to make the frames.

local handler = script.Parent.Handler

if script.Parent.Handler.SlotCount.Value == 25 then
    for i = 1,25 do
    local slot = Instance.new("Frame",handler)
    slot.Name = "slot1"
    slot.AnchorPoint = Vector2.new(.5,.5)
    slot.Position = UDim2.new(0.5,0, 0.5,0)
    end
end

Since the default of the SlotCount is 25 (and doesn't ever change) it would have to be a true if statement, but then out of the 25 loops, it doesn't go through and make a single frame. What will fix this?

I also need someone to explain i,v in pairs to me. I've watched many videos on making inventories and related stuff, and they all include the "for i,v in pairs do" which doesn't make a lot of sense to me. As I said, I am new to scripting, so try and make the explanation simple so I can understand and comprehend it better.

0
ui grid layout will help you A LOT in this case ^_^ greatneil80 2647 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

Alright, when we were chatting on the community chat, I helped you by telling you to use UiGridLayout which effectively worked for you!!! :D Then you needed help on creating 25 frames right? So here is the code, I will explain it below the code.

local handler = script.Parent.Handler

if handler.SlotCount.Value == 25 then
    for i = 1,25,1 do
        local slot = Instance.new("Frame")
        slot.Parent = handler
        slot.Name = "slot"..i
        slot.AnchorPoint = Vector2.new(.5,.5)
        slot.Position = UDim2.new(0.5,0, 0.5,0)
    end
end

Alright, here is what I fixed, you created handler and then you rewrote the exact same thing on line 3 when you could have said handler.SlotCount then.. You needed an increment, for i = 1,25,1 and then.. You said slot = Instance.new("Frame",handler) using a parent as the second parameter is depricated, thats why I had to set its parent on the next line..

Hopefully this works, remember to click the answer button if it works, Have a great day :)

0
Works as I originally wanted it too. Thanks! rhettsmith6537 109 — 6y
0
You are welcome greatneil80 2647 — 6y
0
@line 3 for i = 1,25 works anyway. The second argument of Instance.new would not be preventing the script from running. The only issue seems to be the slot name. EzraNehemiah_TF2 3552 — 6y
0
^ dude, he fixed it... greatneil80 2647 — 6y
Ad
Log in to vote
2
Answered by 6 years ago

Iterator Functions

There are 3 different types of Iterator Functions excluding custom ones. . pairs, ipairs and next.


Basic For Loop

A Generic For Loop is a loop that keeps track of how many times it has looped. The difference between a Numeric For Loop and a generic one, is that we can use the iterator in our script; the iterator isn't just an index.

for i = 1,20 do
    print(i) --We can use the iterator in our script
end

1 2 3 4 ... 18 19 20

We can use this for loop to get the values of a table.

local table = {1, 2, 3, "Hi", true}

for i = 1, #table do --# is an operator that returns the size/length of a given table or string
    --The size of the table is 5, so it's the same exact thing as saying i = 1, 5
    print(table[i])
end

1 2 3 Hi true


Pairs

Pairs is the most common way people get values from a table.

local table = {1, 2, 3, "Hi", true} --Same as the last script but using pairs

for iteration,value in pairs(table) do
    print(value) --i,v is short for iteration, value.
    --Sometimes i is replaced with _ if you don't need the iteration
end

What Pairs is doing is it is essentially going through every single object inside of the table and getting its value and iteration. It doesn't do it in numerical order. The reason it doesn't read the table in numerical order is in case the table is written as a dictionary. This is a dictionary:

local dictionary = {
[1] = 1,
["Hello"] = "world!"
}

The keys in a normal table go in numerical order. The first item in a table has a key of 1, the second has a key of 2 and etc... In a dictionary, keys can be a bool, string or etc. Using the same dictionary:

for _,v in pairs(dictionary) do
    print(v)
end

Would print everything in the dictionary.

for i = 1, #dictionary do
    print(dictionary[i])
end

Would print 1 and 1 nil because the table would try to call dictionary[2] which isn't set. dictionary["Hello"] is set, however.


Ipairs

ipairs is similar to pairs. The i stands for integer. This means that while pairs can look at every single value in a dictionary, ipairs goes through the entire table and only looks for keys with consecutive integers starting from 1.

local dictionary = {
[1] = 1,
["Hello"] = "world!",
[2] = true,
[4] = "nil but a string",
[3] = "foo",
[6] = "bar"
}

for _,v in ipairs(dictionary) do
    print(v)
end

1 true nil but a string foo

It prints the value of the keys 1-4 because they have consecutive keys starting from 1. The key of 5 is non-existent so it stops before it can print the value of 6. You could also think of it like this: It goes through the table from 1 to math.huge (basically infinity) until it gets a value of nil. That is why, even in normal tables, if there is a value of nil, it will stop at that key.

local table = {true, false, nil, 3}
for i,_ in ipairs(table) do
    print("Inspecting item number "..tostring(i))
end

Inspecting item number 1 Inspecting item number 2

Key #3's value is nil. Ipairs will stop!


Next

Next is used like so:

local table = {nil, 1, 2, true}
print(next(table)) --output's 2 1. 2 is the Key and 1 is the Value
--The first key, who has a value of nil, is ignored because of its value.

--Example 2:
for _,v in next,table do --output is the same as in pairs.
    print(v)
end

--Example 3:
local a = {}
local b = {1}

print(next(a), next(b)) --Output is nil 1 1
--[[This is useful to see if a table is empty or not
since an empty table returns nil.]]

Next has 2 arguments. The first is the table and the second is what is called, "lastKey". What the second argument does is it gives you the next key and its value.

local table = {true, nil, false}
print(next(table, 0)) --Prints true
print(next(table, 2)) --Prints false, not nil.
--lastKey starts counting from 0, so 0 = 1st key.

Next is not used as much as pairs or ipairs.


TL;DR/Layman's Terms

Pairs, Ipairs and Next are all built-in global functions used in generic for loops for indexing through tables. They just work slightly differently.



Hope it helps!


Info on Stateless Iterators

Stateless iterators on official lua site

1
`pairs` and `ipairs` are iterator generator functions, *not* iterator functions. Link150 1355 — 6y
0
Thx for clarification EzraNehemiah_TF2 3552 — 6y

Answer this question