Lua integration into a game
#1 Members - Reputation: 158
Posted 14 September 2012 - 12:15 AM
How do you guys manage scripts in your game?
#2 Members - Reputation: 501
Posted 14 September 2012 - 12:23 AM
But, others might know better. This is just how I use lua, and I dont run into trouble with it.
#3 Members - Reputation: 158
Posted 14 September 2012 - 12:39 AM
#4 Members - Reputation: 608
Posted 14 September 2012 - 01:02 AM
#5 Members - Reputation: 501
Posted 14 September 2012 - 01:14 AM
Otherwise, I handle things exactly as you and kd7tck said.
#6 Members - Reputation: 158
Posted 14 September 2012 - 01:28 AM
#7 Members - Reputation: 158
Posted 14 September 2012 - 01:32 AM
Just to be clear, currently all of my game states are implemented as different classes (MainMenu, Options, etc.), and each of them has an init() method which is called when they are switched into the game.
#8 Members - Reputation: 608
Posted 14 September 2012 - 01:47 AM
I don't even go that far. I only run luaL_dofile on a .lua which calls a hooked C++ function that itself calls luaL_dofile on the argument passed. Thus, I never have to recompile the project when I add a new script, or when a script wants to import another script file (loading every file in the scripts folder adds extra hoops for seperating one mod from another). In other words, the engine only looks for init.lua, and init.lua is just a series of "DoScript("file.lua")". By doing this, a mod manager can easily append or delete the varius DoScript("file.lua") lines and your mod is loaded painlessly.
Otherwise, I handle things exactly as you and kd7tck said.
Why would I recompile after adding a script?
Simply program main() to search for all files in the scripts folder and load each one in. No need to pass every file name in through a lua file. This makes things easier for end users; if all an end user has to do is drop in a file then that is ideal.
#9 Members - Reputation: 608
Posted 14 September 2012 - 01:55 AM
Thanks guys for the kind replies and suggestions. Another question, say I want to use Lua to configure my game GUIs. Do I expose the various GUI elements' member functions such as setPosition(x, y) to Lua, and then in the GUI's init method I run the Lua function that handles the positioning of these GUI controls? Or would it be a better approach to make the GUI's class (for example the MainMenu class) exposed to Lua and do everything in Lua instead?
That will depend on how much control you want to give to the end user. Do not do everything in lua, the game loop needs to be in C++ not lua. You could have a hook that generates a custom state dynamically and places it onto a global state machine.
#10 Crossbones+ - Reputation: 500
Posted 14 September 2012 - 05:52 AM
To my pleasant surprise, the performance has been excellent, and no need to chance anything. I should note that I call scripts relatively rarely (a script won't be called every time through the game loop), and my scripts tend to be quite small and self-contained.
Hope that helps,
Geoff
#11 Moderators - Reputation: 5222
Posted 14 September 2012 - 06:59 AM
#12 Members - Reputation: 429
Posted 14 September 2012 - 07:15 AM
Do I expose the various GUI elements' member functions such as setPosition(x, y) to Lua, and then in the GUI's init method I run the Lua function that handles the positioning of these GUI controls? Or would it be a better approach to make the GUI's class (for example the MainMenu class) exposed to Lua and do everything in Lua instead?
Your first approach requires that you tightly couple your GUI element to Lua, so that it can't be used without modification if you decide to move away from Lua in the future. Your second approach is better, but comes with its own set of problems. How much of the interface should you expose to Lua? By exposing the interface, how many other userdatums must be exposed in order to be passed as arguments?
I personally look at my scripts completely differently than my engine code, exposing specifically tailored and simplified interfaces to Lua, rather than the lower level ones.
#13 Members - Reputation: 501
Posted 14 September 2012 - 10:25 AM
That was actually in response to acidleaf, who made it sound like he was hardcoding all of the script file refferences. Rather than do that, I merely hardcode init.lua, which has the ability to expand the library of lua functions however I or a modder sees fit.Why would I recompile after adding a script?
The problem I see there is with modding. If every file is read indescriminately, you cannot then have a moda.lua and a modb.lua in the scripts folder unless you want the user to always use both mods he has installed there. By using a file to describe each file seperately, you can neatly store all of the lua files for a specific mod in a scripts/modname/ folder, with only the scripts/modname_init.lua called in the scripts/init.lua file. Disabling a mod, then, is as simple as removing the modname_init.lua refference in init.lua.Simply program main() to search for all files in the scripts folder and load each one in.
I suppose you could run every modname_init.lua using your blanket method, but enabling and disabling mods would still require you to remove and replace the init file for no net benefit over the alternative. As an end user, I hate having to move files around (and dont trust programs moving them for me), and so I tend to design code that leaves files in place. Deleting and replacing one line is easy to write a mod manager for and involves less file moving. I don't suppose either design is objectively "better" but I always side with less moving files.
Edited by Zouflain, 14 September 2012 - 10:30 AM.
#14 Members - Reputation: 176
Posted 15 September 2012 - 06:05 AM
The Lua objects communicate using message passing, the interaction is coordinated on the C++ side, using uid's. And since the queues are also in Lua I have freedom to send from int's to complex data structures, if necessary.






