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

Explain Rawset in this function?

Asked by 9 years ago

I've been dissecting code to understand Lua better and I can't understand what this function does. I have a very basic understanding of rawset and metatables. Could somebody explain in detail what this function does? Thank you!

1Selected = {}
2function CleanSelected()
3    for i,v in pairs(Selected) do
4        if v == nil or v:findFirstChild("Torso") == nil then
5            rawset(Selected,i,Selected[i+1])
6        end
7    end
8end

2 answers

Log in to vote
1
Answered by 9 years ago

Well one thing we have to keep in mind here, is that the rawset function is doing nothing (in this case). If this is indeed all the code behind this program, and there's no other point in time where a metatable is set to it with a __newindex metamethod, there's literally no point in having it. So that narrows down our code to this:

1Selected = {}
2function CleanSelected()
3    for i,v in pairs(Selected) do
4        if v == nil or v:findFirstChild("Torso") == nil then
5            Selected[i] = Selected[i+1]
6        end
7    end
8end

What it's doing?

First off, the first condition of the if statement if v == nil is redundant. nil values are automatically accounted for as something that doesn't exist, so the iteration will not even register it's variable's existence (thus, making the comparison pointless).

Other than that, all this code is doing is checking whether or not a torso exists inside v, and if it doesn't, then it assigns the table the next value in it's storage.

Suggestion?

Whether or not you're actually going to be using this code, or if you were just looking at random stuff and had a question, it can't hurt to clean this up a bit:

01local Selected = {} -- local
02 
03local function CleanSelected() -- also local
04 
05    -- "Selected" was an array in your code, so I guess
06    -- we'll just use a numeric for loop.
07 
08    for i = 1,#Selected do
09 
10        -- Create a variable to represent the value
11        -- Also find out if 'v' has a torso in it first.
12        local v = Selected[i]
13        local torso = v:FindFirstChild("Torso")
14 
15        -- If no torso was found, then...
View all 22 lines...

Hope that helped, let me know if you have any questions.

0
Checking for the existance of `v` is actually valid as `v` not having `FindFirstChild` as a member causes an error in that code. adark 5487 — 9y
0
Actually, no, now I understand what you meant. `v` cannot be nil in a generic for loop... adark 5487 — 9y
Ad
Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

If you know what rawset does, you know that it's just like setting a Table Value, ignoring the metatable attached to that Table, as if there wasn't one.

Looking at this code, however, there is at least one glaring issue: it doesn't actually remove any elements. A much better function to use here is table.remove, as that will shift everything in the Table down by 1, which is what the writer of this code seems to want to do.

However, that introduces a major bug: modifying a Table you are currently iterating over can have unexpected results, for example checking the same item multiple times, or missing items entirely.

A better version of this function, then, involves making a new Table using the valid data in the existing:

01Selected = {}
02function CleanSelected()
03    local newSelected = {}
04    for i,v in pairs(Selected) do
05        if v:FindFirstChild("Torso") then
06            table.insert(newSelected, v)
07        end
08    end
09    Selected = newSelected
10end

Answer this question