This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Glotz659's profile picture

strange loadstring() behavoir

Started by Glotz659, 08 February 2013 - 02:57 AM
Glotz659 #1
Posted 08 February 2013 - 03:57 AM
When you open the lua shell and try

a=42
f = loadstring("b=a")
f()
it will still return nil for b
Orwell #2
Posted 08 February 2013 - 04:04 AM
That's no bug. The functions created by loadstring have a seperate environment. The solution:

a=42
f=loadstring("b=a")
setfenv(f, getfenv())
f()
Cloudy #3
Posted 08 February 2013 - 04:06 AM
loadstring() operates in the global context. Programs have their own environment, meaning loadstring can't access that environment.

Edit: Orwell is a Ninja.
Glotz659 #4
Posted 08 February 2013 - 04:50 AM
thank you!