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

How do you 'use' lua?

Started by
4 comments, last by gimp 21 years, 1 month ago
I''m not asking about simple 5+10 examples rahter how to interface the script with my code. Say for example I wan''t a simple spash screen script. I''ll need to: -trigger on things like input to break out. -Set my code to load a world -Play some music All of which requires: -My code to be called from an Lua script to do certain things, like start playing music, load worlds etc. -The Lua script needs to pause at certain points and return control to my code. For example Once the level is loaded the splash screen script will need to wait wait until it needs to call the ''UnloadSplashScreen()'' code. Should I use some kind of generic Sleep() to return control back to my code(at the point where the execution started of course) -Defining additional functions that can be called from script (e.g. PlayMusic(), LoadWorld()) Is this how it all works? How do I define the callback etc to LUA? All I''ve read so far is the tut from gamedev. Feel fre to tell me to piss off and read the doco... Many thanks, Chris Chris Brodie http:\\fourth.flipcode.com
Chris Brodie
Advertisement
Here''s something I did with lua


void CScriptWindow::AddButton(int id, char *caption, int width, int height, int x, int y, char *function){   CControl *control = new CControl();   control->id  = id;   control->cap = caption;   control->typ = "BUTTON";   control->xpos = x;   control->ypos = y;   control->wid = width;   control->heigh = height;   control->func = function;   control->hwnd = CreateWindow(control->typ, control->cap, WS_CHILD | WS_VISIBLE, control->xpos, control->ypos, control->wid, control->heigh, g_hwnd, (HMENU) control->id, g_hInstance, NULL);   ShowWindow(control->hwnd, SW_SHOW);   clist.push_back(control);}int l_AddButton(lua_State* luaVM){   char *caption = NULL;   int width = 0;   int height = 0;   int x = 0;   int y = 0;   char *function = 0;   int id = (int)lua_tonumber(luaVM, 1);   caption = (char *)lua_tostring(luaVM, 2);   width = (int)lua_tonumber(luaVM, 3);   height = (int)lua_tonumber(luaVM, 4);   x = (int)lua_tonumber(luaVM, 5);   y = (int)lua_tonumber(luaVM, 6);   function = (char *)lua_tostring(luaVM, 7);   lua_setglobal(luaVM, function);   theScriptWindow->AddButton(id, caption, width, height, x, y, function);   return 1;}then just used lua_register to register the function to LUA.. and calls from the script as AddButton 


Got it. Your comment re lua_register prompted me to reread the document and I learnt the bit I didn''t know...

All''s well, thanks.

Chris Brodie
http:\\fourth.flipcode.com
Chris Brodie
You should check out a tool called tolua. It reads a simplified C/C++ header (usually with extension .pkg)and generates a C/C++ source file containing binding code. All you need to do is include the generated file in your project, and in your program initialize a lua_State and call the ''open''-function(s) that tolua generated (foo.pkg would yield, among other functions, one named tolua_foo_open(lua_State*), or something similar). After that the functions, classes, variables, etc. that were defined in the .pkg file will be available in Lua scripts run in the context of the lua_State were you ''opened'' them. It''s all very easy, and the website I linked to explains it better.

The only downside is that tolua currently doesn''t support Lua 5.0 (but from what I''ve read it''s in the works, so hopefully it''ll be out soon).

There are other (semi-)automated ways of binding C/C++ functions/classes/etc. to Lua. Links can be found in the Lua users Wiki, here (under ''Code Wrappers'').

And if you like, it''s pretty easy to create the interfaces yourself, similar to what Maega showed above.
There''s really not much need to have Lua stop and return control to the code that called it. Instead you can just provide functions for Lua to call, and have those functions do what you wanted the Lua function to exit back out for. Or you can set it up so the main program calls various Lua functions. Or to do what you said, have the Lua script sleep and return to the caller, and then presumably have the Lua script resume where it left off, you can use Lua''s coroutines.
Actually, I already have a massive database of semi script driven factories for my XML system and would like to use the same code there as well. The thing I can''t understand at this point is how to generically register all my functions.

I can''t use a singleton as I may want to run a few different scripts. Currently I use factories and don''t need any massive set of registration scripts as each class registers with the factory in it''s own file.

How is this normaly done? I''d imagine that each ''LuaState'' would need need to have the lua_register() stuff called to use functions.

Should I use a CLuaFunctions registration singleton so each instance can silently do all the registrations it needs?

Chris Brodie
http:\\fourth.flipcode.com
Chris Brodie

This topic is closed to new replies.

Advertisement