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

Local var in global function

Started by EveryOS, 28 April 2016 - 11:10 AM
EveryOS #1
Posted 28 April 2016 - 01:10 PM

--File A
local txt = 'works'
_G.printer = function()
  print(txt)
end

--File B
printer()
--Returns 'works'
txt = 'this is where my problem lies'
printer()
--Returns 'this is where my problem lies'
Edited on 28 April 2016 - 11:11 AM
valithor #2
Posted 28 April 2016 - 02:10 PM
Edit misread question
Edited on 28 April 2016 - 12:12 PM
Tiin57 #3
Posted 28 April 2016 - 02:10 PM
Have you tried making txt local in File B? It's probably overwriting the reference to txt in _G.printer since you're not specifying local for the File B declaration.

Edit: Ninja'd by valithor, but I don't quite think he got the problem.
Edited on 28 April 2016 - 12:11 PM
Bomb Bloke #4
Posted 28 April 2016 - 02:20 PM
Have you tried making txt local in File B? It's probably overwriting the reference to txt in _G.printer

It won't either way, or at least, assuming the code shown here is complete. It simply can't do it without modifying the environment of the function in concern.

This makes me assume that the question is "how do I make it act like this", in which case valithor's on the right track in asking "why?" (though the short answer is "lookup getfenv()/setfenv()").

If it really is acting the way he describes already, then either he's not running the code he's showing us, or he's using an emulator or sandbox or… something with a really weird bug.
EveryOS #5
Posted 28 April 2016 - 02:49 PM
It previously did it? With the same code. That's strange.
Edited on 28 April 2016 - 12:51 PM