🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

lua: passing arguments to scripts loaded from file

Started by
1 comment, last by Fragmo 19 years, 11 months ago
Is there a way to call a script that was loaded from a file and pass arguments to it without creating a global table to contain the arguments? I'm having a problem when another script is loaded and run from within a script it changes the global arg table (all scripts use an arg table with the same name) and gives errors when the original script continues after the other script is run. lua_call takes a parameter count so I'm guessing it is possible, but I'm not sure of the syntax. thanks, dave
Advertisement
Could you give an example of the lua code causing this problem?

I find it really interesting that your lua code is stomping all over itself.
I don't have the code on this machine so I'll abbreviate it.

luaL_loadfile(L, file);
lua_newtable(L); // create a table to hold the parameters
// create table entry for each parameter
lua_setglobal(L, "arg");
lua_pcall(L, 0, LUA_MULTRET, 0); // run the script

this code is executed for each script that is loaded and run. The problem I have is exactly what I would expect, when a script file is loaded and executed as a result of an action taken in another script it will reassign the global 'arg' and when the second script completes and control is returned to the first script the 'arg' variable no longer contains what I want. I'm looking for a way to load a file as a function that can accept 1 or more parameters so the 'arg' variable can be local rather than global.

thanks,
dave

This topic is closed to new replies.

Advertisement