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

How do I use parameters this way?

Asked by 8 years ago

So I have a script down below

local test = true

function change(test)
    test = test
end

change(false)
print(test) --returns true

Will the above code set the parameter to itself since it has the same name as the variable on line one? Or will it set the test var on line 1 to false? I know this is a problem when I'm coding in java, typically I just use the this keyword to differentiate the parameter with the instance variable in the class. Let me know if I didn't explain this well enough, its kind of hard to express what I'm asking.

2 answers

Log in to vote
0
Answered by 8 years ago

I cannot explain the inner workings of why this happens but you can check the lua manual.

This is my example of how I got it working, there may be better solutions:-

test = true -- this must not be a local variable or getfenv will not pick it up as we are checking for 0 (global)

function change(test)
    print(test) -- at the function level it returns false which is what we want
-- this shows the variable which are accessible from a global level
    for i, v in pairs(getfenv(0)) do
            print(i, " = ", v)
    end 

-- from the first print statement we can the assume that what test will do is set false = false as from my experience code will always use the same value for the same line of code 

-- now we can attempt to find the global variables in this script, I have not tested if this finds "_G" variables but we know the name of the variable is test so we can then use the name test as a global variable to assign the value false.
   getfenv(0)['test'] = test 
end

change(false)

print(test) --returns false

Hope this help.

Ad
Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
7 years ago
Edited 7 years ago

Hello.

Whenever you set parameters for a function, you are setting local variables to be used within that code block.

The following two code blocks are equivalent:

function change(test)

    -- other code
    test = test
end
function change(...)
    local test = ...

    -- other code
    test = test
end

You are running into a scope problem. As I demonstrated in the second code block, whenever you set parameters for a function, you are creating local variables to be used inside of that function.

That means that when you call function change with a parameter named test, it creates a local variable named test, and will not access a variable of the same name outside of the code block's scope.

local test = true

function change(test)
    test = test -- This sets a local variable named test equal to itself
end

change(false)
print(test) --returns true

In your above function, the test that was declared first is not modified. However, if you change the naming scheme, it will modify the intended value:

local test = true

function change(testValue)
    test = testValue
end

change(false)
print(test) --returns false
0
Thanks, but someone already answered this question... dragonkeeper467 453 — 7y
0
They answered incorrectly. Validark 1580 — 7y

Answer this question