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

How do you manipulate the variables self, not the value (in a function)?

Asked by 4 years ago

So Basically, I'm trying to make a function that plays a call back whenever a var changes

So I got some code from another Question

function Changed()
    local last = varYouWantedToTrack;
    while true do
        wait()
        if varYouWantedToTrack ~= last then
            -- It changed. How nice.
        end
        last = varYouWantedToTrack;
    end
end

and I want to change the "varYouWantedToTrack" part to a pram (and also add a call back to)

function changed(var --[[This soposed to be able to access this var, and want it to update even when I changes]], Callback ) 
    local last = var;
    while true do 
        wait()
        if var ~= last then 
            -- It changed. How nice. 

            -- How do u make a Callback?
        end 
    last = var;
end

So how do I change or access a passed, Global (to the script) var and use it as a local (to function) var and it to change as the originally passed var changes?

If you read all of that you are pogers :P

So Please Help me if you can.

P.S: @AntoninFearless is Helping me so it might be answered soon

P.P.S. Also, How do you make callback funcs

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Ok so from my understanding you wanted to see if the variable changed, so here is an example on how to do this:

local MyVariable = "Oh look im a string"

function Changed()
    local myOriginalVariable = MyVariable
    local SomeCo = coroutine.wrap(function() --coroutine so it doesn't disrupt other threads
        while true do wait()
            if MyVariable == myOriginalVariable then
                print("Still waiting for a change to happen")
            else

                print("Hey look, this changed! It used to be: "..myOriginalVariable.." and now it is "..MyVariable.."!")
            end
        end
    end)
    SomeCo()
end

Changed()
wait(1)
MyVariable = "Oh boy I am now a new string"
wait(3)
MyVariable = 3

Hopefully, this helps you out a little bit, sorry if it isn't what you are really looking for because my understanding isn't really good at the moment.

Ad
Log in to vote
0
Answered by
Elyzzia 1294 Moderation Voter
4 years ago
Edited 4 years ago

there's no way to pass a variable to a function and have that parameter change along with the original variable

local var = 0

local function foo(var) -- this parameter and the original variable are two different values
    coroutine.wrap(function() -- coroutines allow you to have two different "threads" running at the same time
        wait(1)
        print(var) --> prints 0, despite the variable being changed outside of the function
    end)()
end

foo(var)
var = 1

you could use funky getfenv stuff but that 1. requires you to make your variables global, 2. is pretty hacky and 3. is inefficient

plus, any sort of method that constantly waits takes up resources that could be allocated to other tasks

you'd probably be better off using some sort of setter/getter function in combination with a BindableEvent

local _var = 0 -- putting an _ in front of a variable doesn't change its behavior, but it's a good reminder that you shouldn't directly access that variable
local varChangedEvent = Instance.new("BindableEvent")

local function SetVar(val)
    _var = val
    varChangedEvent:Fire()
end

local function GetVar() -- you don't need a getter function in this scenario, but it's good for consistency, and also helps to remind you that you shouldn't directly change the variable
    return _var
end

varChangedEvent.Event:Connect(function()
    print("var changed, new value: " .. tostring(GetVar()))
end)

wait(1)
SetVar(1)
wait(1)
SetVar(2)

with this method, you can make as many event connections as you want and it won't have a very major impact on performance, as long as you don't do anything intensive when the variable is changed; however, with a method that uses busy waiting, if you try to set up multiple callbacks or try to make a callback for several variables, performance is going to start degrading quickly

alternatively, you could just use a Value if you're lazy and don't feel like doing all this setter getter bindable stuff

you should try to avoid busy waiting whenever possible though

Answer this question