PDA

View Full Version : dofile() and loadstring() variable problem


Mex
05-16-2009, 10:51 PM
yo,

here's the code i've got so far:

function blah()
local aaa = "testing"
dofile("SomeFile.txt")
end

SomeFile.txt:
outputConsole(aaa)

When lua executes 'SomeFile.txt', instead of outputting "testing" in the console, it outputs '(null)'.
After reading around I think it's because lua executes the file's content in the global enviroment, and because aaa hasn't been defined in the global enviroment, it doesn't see it.

Does anyone know how I could get it working? Ie. Lua sees the variable 'aaa' and outputs 'testing' in the console.

Thanks!
Mex

tuntis
05-17-2009, 04:35 AM
Uh... try global aaa instead?

Mex
05-18-2009, 01:37 PM
thx tuntis, but i thought of that, and seeing as my original plan was to include the dofile() line in over a 100 functions all with a different amount of incoming parameters, it wouldn't really be logical to assign every param global, when i could simply tell lua to execute dofile() in the environment it is being called in.

anyway i didn't end up using dofile() in the end, used loadstring() and functions.