generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Changed event for variables?

Previous Thread :: Next Thread 
Augmentary is not online. Augmentary
Joined: 30 Aug 2013
Total Posts: 8081
12 Dec 2014 08:32 AM
Is there a hacky thing I could put in my script to do this?
Report Abuse
Fedorakid is not online. Fedorakid
Joined: 17 Jul 2010
Total Posts: 7079
12 Dec 2014 08:56 AM
You mean like

variable = 9

variable changed something

variable = 3
--something happens since you changed it

???
Report Abuse
Augmentary is not online. Augmentary
Joined: 30 Aug 2013
Total Posts: 8081
12 Dec 2014 09:01 AM
like Variable.Changed:connect(function()
Report Abuse
SLY3 is not online. SLY3
Joined: 10 Jul 2008
Total Posts: 1700
12 Dec 2014 09:16 AM
I don't think there's a way you can do it to script variables, only physical values.
Report Abuse
Fedorakid is not online. Fedorakid
Joined: 17 Jul 2010
Total Posts: 7079
12 Dec 2014 09:28 AM
Well, if I were to do this. I'd have the variables in a table then use the __newindex metamethod.
Report Abuse
Augmentary is not online. Augmentary
Joined: 30 Aug 2013
Total Posts: 8081
12 Dec 2014 09:33 AM
ya i still have to learn that
Report Abuse
breuning is not online. breuning
Joined: 30 Oct 2008
Total Posts: 4268
12 Dec 2014 09:33 AM
function changed(val,functiontorun)
coroutine.wrap(function()
val2 = getfenv(0)[val]

while game:GetService("RunService").RenderStepped:Wait() do
if val2 ~= getfenv(0)[val] then
functiontorun()
val2 = getfenv(0)[val]
end
end
end)
end

not sure if it works
Report Abuse
Fedorakid is not online. Fedorakid
Joined: 17 Jul 2010
Total Posts: 7079
12 Dec 2014 09:41 AM
This is what i'd do, just because I don't get getfenv and setfenv. I get it but I don't get how I'd use it usefully because I'm so unsecure on it.

vars = {
var1 = "hello",
var2 = "hey",
var3 = "hmmm"
}

setmetatable(vars, {__newindex = function(t,i,v)

--What do you want to happen when something changes?

end})




vars is a table where you add variables, add what is supposed to happen when a variable change.

atleast this is how i'd do it
Report Abuse
Accomplishable is not online. Accomplishable
Joined: 13 Aug 2009
Total Posts: 6207
12 Dec 2014 09:44 AM
use a table for your variables, then use metamethods
Report Abuse
ThatChristianGuy is not online. ThatChristianGuy
Joined: 14 Jun 2014
Total Posts: 1283
12 Dec 2014 10:01 AM
So __newindex fires when setting an index that has already been indexed as well?


--ThatChristianGuy
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
12 Dec 2014 11:00 AM
No. __newindex only fires when a nil value is set to something else.
Report Abuse
ArbiterOfDeath is not online. ArbiterOfDeath
Joined: 20 Jun 2011
Total Posts: 1458
12 Dec 2014 11:28 AM
Yes, you can. But only if the variable is global, not local. Put this at the start of your script. Call addWatch with a variable name to add it to the watch. Call addConnection with the variable name to be notified when it changes, and the function to call. Enjoy!

do

local env = getfenv()
local watchVariables = {}
local watchValues = {}

function __newindex (environment,key, value)
if watchVariables[key] then
watchValues[key] = value
for func, TRUE in next, watchConnections[key] do
local success, msg = pcall(func, key, value)
if not success then
error("Watch caught an error wile " .. key .. " was changing: " .. msg, 2)
end
end
else
return rawset(environment, key, value)
end
end

function __index(environment, key)
if watchVariables[key] then
return watchValues[key]
else
return rawget(environment, key)
end
end

function addWatch(varName)
watchVariables[varName] = true
watchValues[varName] = rawget(env, varName)
rawset(env, varNAme, nil)
watchConnections[varName] = {}
end

function addConnection(varName, func)
watchConnections[varName][func] = true
end

function removeConnection(varName, func)
watchConnections[varName][func] = nil
end

function resetConnections(varName)
watchConnections[varName] = {}
end

function removeWatch(varName)
watchVariables[varName] = nil
watchValues[varName] = nil
watchConnections[varName] = nil
end

setmetatable(env, env)

end
Report Abuse
Augmentary is not online. Augmentary
Joined: 30 Aug 2013
Total Posts: 8081
13 Dec 2014 05:54 PM
Most of my variables are local.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
13 Dec 2014 05:56 PM
Just use newproxy and __newindex and a "hidden" table?

This is one without newproxy that I made a while back:
http://pastebin.com/p9QvnuPU but it could be made better
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
13 Dec 2014 05:59 PM
Arbiter, what you said wouldn't work on Roblox since they locked the environment. You would have to create a new table and just set the __index to the default env.
Report Abuse
Augmentary is not online. Augmentary
Joined: 30 Aug 2013
Total Posts: 8081
13 Dec 2014 06:02 PM
thx m8
Report Abuse
ArbiterOfDeath is not online. ArbiterOfDeath
Joined: 20 Jun 2011
Total Posts: 1458
13 Dec 2014 06:16 PM
Either way, you cannot access local variables without access to the Lua debug library. You will have to use global variables.

The function environment works just fine. Here is an example:

value = "Locked"
print(tostring(value)) --> "Locked"
getfenv().value = nil
print(tostring(value)) --> "nil"
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
13 Dec 2014 06:18 PM
I'm talking about this part:

local env = getfenv()
...
setmetatable(env, env)

And also, just create a function to do event stuff instead of making all new global tables doing it?
Report Abuse
ArbiterOfDeath is not online. ArbiterOfDeath
Joined: 20 Jun 2011
Total Posts: 1458
13 Dec 2014 06:35 PM
^ I see. That isn't too hard to fix. The purpose of the above script is to enable the event connections without changing existing code.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image