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

Enviroment

Started by Sewbacca, 19 February 2016 - 09:26 PM
Sewbacca #1
Posted 19 February 2016 - 10:26 PM
Is it possible to start a function in a specific Environment?
valithor #2
Posted 19 February 2016 - 10:44 PM
Is it possible to start a function in a specific Environment?

I would suggest looking into setfenv.

http://www.gammon.co...php?lua=setfenv
Edited on 19 February 2016 - 09:44 PM
Creator #3
Posted 19 February 2016 - 10:56 PM
More specifically:


-- f <-- the function
-- env <-- the environement
setfenvReturnsToo = setfenv(f,env) -- but it also modifies the function it is given
Sewbacca #4
Posted 19 February 2016 - 11:01 PM
THANKS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Creator #5
Posted 19 February 2016 - 11:48 PM
You're welcomeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
apemanzilla #6
Posted 20 February 2016 - 12:02 AM
For compatibility reasons, load should be used instead where possible.
HPWebcamAble #7
Posted 20 February 2016 - 12:10 AM
To expand on what apemanzilla said, getfenv and setfenv are Lua 5.1 features, and will be removed from CC at some point, when the full update to Lua 5.2 is completed.
Sewbacca #8
Posted 20 February 2016 - 01:59 PM
Do load the same thing like setfenv?
Creator #9
Posted 20 February 2016 - 02:15 PM
It combines loadstring and setfenv.

load(string,mode,env)
HPWebcamAble #10
Posted 20 February 2016 - 06:42 PM
Read more about load() here:
http://www.lua.org/manual/5.2/manual.html#pdf-load
Sewbacca #11
Posted 20 February 2016 - 09:42 PM
I'm not sure, that I get it right, setfenv will be remove in CC Lua 5.2.
Creator #12
Posted 20 February 2016 - 10:01 PM
Lua 5.2 has come out. As has 5.3. The thing is that CC uses Lua 5.1
Sewbacca #13
Posted 21 February 2016 - 10:25 AM
Lua 5.2 has come out. As has 5.3. The thing is that CC uses Lua 5.1

I mean, will be setfenv removed, when CC uses Lua 5.2?
Edited on 21 February 2016 - 09:25 AM
Bomb Bloke #14
Posted 21 February 2016 - 10:32 AM
Yes. There's no equivalent to setfenv in 5.2; it's not possible to code your own substitute.

http://lua-users.org/wiki/EnvironmentsTutorial
Sewbacca #15
Posted 21 February 2016 - 10:48 AM
Do you know, why setfenv will be removed? :(/>
Edited on 21 February 2016 - 09:49 AM
Bomb Bloke #16
Posted 21 February 2016 - 10:58 AM
The answer here provides a pretty good example case as to when the new structure might serve better:

http://stackoverflow.com/questions/14290527/recreating-setfenv-in-lua-5-2

Whether or not that specific sort of thing was involved in the actual decision, I dunno.