|
| 19 Apr 2009 04:20 PM |
W...T....H....
Why does Roblox keep crashing when I do this?
local a = 5
function foo() print(%a) end
foo()
I think it's the %a part, though isn't %a supposed to return a deep copy of it's variable? |
|
|
| Report Abuse |
|
|
leemo
|
  |
| Joined: 16 Feb 2008 |
| Total Posts: 6226 |
|
|
| 19 Apr 2009 04:21 PM |
| a should be global not local. |
|
|
| Report Abuse |
|
|
|
| 19 Apr 2009 04:23 PM |
local = 5 function foo() print("%a") end
You forgot quotes. |
|
|
| Report Abuse |
|
|
|
| 19 Apr 2009 04:24 PM |
Wouoldn't matter, tested it anyway, still got the crash. Halp? |
|
|
| Report Abuse |
|
|
|
| 19 Apr 2009 04:24 PM |
| Lol...DingDong, not what I'm trying to do. |
|
|
| Report Abuse |
|
|
|
| 19 Apr 2009 04:26 PM |
| Then what are you trying to do? Get a percentage of a? |
|
|
| Report Abuse |
|
|
|
| 19 Apr 2009 04:27 PM |
Hehe, Silly.
Upvalues were added to work around the function scope limitation. Prefixing an outer-scope variable reference with % produces a copy of that variable as of the function's instantiation. Only the immediate scope containing the function and the global scope may be accessed in this manner. Since the variable is a copy, it's not possible for the function to alter the original value. A common solution to this problem is to put such variables inside a table and access the table as an upvalue. The table acts as a function closure.
local closure = { a=5 } local foo = function() %closure.a = %closure.a + 1 %closure.b = 'hello' end
|
|
|
| Report Abuse |
|
|
| |
|