🎉 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 Questions

Started by
4 comments, last by Prozak 20 years, 5 months ago
I'm having a go at LUA, and this is some of my code:
lua_State L;
lua_baselibopen(L)
lua_settop(L, 0);
lua_dofile(L, "LUA\\init.lua");	// load the script
My Question is, do we have to have an L variable per script file, or is there a way arround this? So that I can spread out code into diferent files, etc... Thank you for your replies... EDIT: Small error in code...

[Hugo Ferreira][Positronic Dreams][Colibri 3D Engine][Entropy HL2 MOD][Yann L.][Enginuity]
The most irrefutable evidence that there is intelligent life in the Universe is that they haven't contacted us!

[edited by - pentium3id on January 28, 2004 12:33:43 AM]
Advertisement
Looks like you''re using Lua 4.0. I strongly suggest you upgrade.

Anyways, no, you don''t need a different lua_State.

"Sneftel is correct, if rather vulgar." --Flarelocke
What in the code made you think I had version 4?
My LUA.h file has:
#define LUA_VERSION "Lua 5.0"

So this must be version 5.

So what do I do for each file?
lua_dofile(L, "file.lua"); // load the script

Do I run the above code once for each lua file i have, maintaining the same L variable?
Will it load the functions and global variables and all else?

Thank you for the feedback...

[Hugo Ferreira][Positronic Dreams][Colibri 3D Engine][Entropy HL2 MOD][Yann L.][Enginuity]
The most irrefutable evidence that there is intelligent life in the Universe is that they haven''t contacted us!

quote: Original post by pentium3id
What in the code made you think I had version 4?

lua_dofile() is not often used in Lua 5.0; it''s mostly maintained for compatibility.

quote: Do I run the above code once for each lua file i have, maintaining the same L variable?
Will it load the functions and global variables and all else?

Yeah.

What you have to understand about Lua, is that it only does stuff by executing. Consider a file containing the lua code:

function foo(i)
return i+1
end

Now, what that code ACTUALLY does is something like this:

foo = function(i) return i+1 end

In other words, when executed, it sets the global variable foo to a function, with the provided body.

Think of each lua script file like a big function. It does its thing (usually registering functions), and then returns. If you use lua_dofile() multiple times, it''s like running each function in turn. All of the effects of each function are kept around.

"Sneftel is correct, if rather vulgar." --Flarelocke
WoW! That was truly helpful thanks

Just one thing, if lua_dofile(); is out, what has replaced it?

Thanks for the tips

Jester Says: [Hugo Ferreira][Positronic Dreams][Colibri 3D Engine][Entropy HL2 MOD]
[Yann L.][Enginuity] [Penny Arcade] [MSDN][VS RoadMap][Humus][BSPs][UGP][NeHe]
The most irrefutable evidence that there is intelligent life in the Universe is that they haven''t contacted us!
luaL_loadfile() loads the file into a function (which it places at the top of the stack), but does not call it. You can then call it yourself (usually with lua_call(L, 0, 0)), or save it for execution later.

"Sneftel is correct, if rather vulgar." --Flarelocke

This topic is closed to new replies.

Advertisement