Adding Lua: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 16: | Line 16: | ||
Once that is done you will need to make sure your game code calls the init and shutdown functions. In gameinterface.cpp add this into DLLInit before the return true statement (around line 557): | Once that is done you will need to make sure your game code calls the init and shutdown functions. In gameinterface.cpp add this into DLLInit before the return true statement (around line 557): | ||
{{CodeBlock|lang=cpp|<nowiki>#ifdef GE_LUA | |||
#ifdef GE_LUA | |||
// Start LUA | // Start LUA | ||
GELua()->InitDll(); | GELua()->InitDll(); | ||
#endif | #endif</nowiki>}} | ||
</ | |||
And in the DLLShutdown function add this: | And in the DLLShutdown function add this: | ||
{{CodeBlock|lang=cpp|<nowiki>#ifdef GE_LUA | |||
#ifdef GE_LUA | |||
// Shutdown LUA, close all open gameplays | // Shutdown LUA, close all open gameplays | ||
GELua()->ShutdownDll(); | GELua()->ShutdownDll(); | ||
#endif | #endif</nowiki> | ||
</ | }} | ||
Also make sure you include the manager header file up the top: | Also make sure you include the manager header file up the top: | ||
{{CodeBlock|lang=cpp|<nowiki>#ifdef GE_LUA | |||
#ifdef GE_LUA | |||
#include "ge_luamanager.h" | #include "ge_luamanager.h" | ||
#endif | #endif</nowiki> | ||
</ | }} | ||
== Making Your own Lua Instance == | == Making Your own Lua Instance == | ||
Line 48: | Line 44: | ||
Init gets called after Lua gets init and thus provides the best place to load your script. | Init gets called after Lua gets init and thus provides the best place to load your script. | ||
{{CodeBlock|lang=cpp|<nowiki> | |||
void MyLuaHandle::Init() | void MyLuaHandle::Init() | ||
{ | { | ||
Line 79: | Line 75: | ||
CallLUA(GetLua(), 0, LUA_MULTRET, 0, luaFile ); | CallLUA(GetLua(), 0, LUA_MULTRET, 0, luaFile ); | ||
m_bLuaLoaded = true; | m_bLuaLoaded = true; | ||
} | }</nowiki> | ||
</ | }} | ||
=== Shutdown === | === Shutdown === | ||
Line 90: | Line 86: | ||
RegGlobals allows us to register global variables in lua. | RegGlobals allows us to register global variables in lua. | ||
{{CodeBlock|lang=cpp|<nowiki> | |||
void MyLuaHandle::RegGlobals() | void MyLuaHandle::RegGlobals() | ||
{ | { | ||
Line 112: | Line 108: | ||
LG_DEFINE_INT("HUD_PRINTCENTER",HUD_PRINTCENTER); | LG_DEFINE_INT("HUD_PRINTCENTER",HUD_PRINTCENTER); | ||
} | } | ||
</ | </nowiki>}} | ||
Macros provided allow for different types as well. LG_DEFINE_INT for int, LG_DEFINE_STRING for string and LG_DEFINE_BOOL for boolean. | Macros provided allow for different types as well. LG_DEFINE_INT for int, LG_DEFINE_STRING for string and LG_DEFINE_BOOL for boolean. | ||
Line 120: | Line 116: | ||
Like Globals, RegFunctions is where you put all the c functions you want to expose to Lua. | Like Globals, RegFunctions is where you put all the c functions you want to expose to Lua. | ||
{{CodeBlock|lang=cpp|<nowiki> | |||
void MyLuaHandle::RegFunctions() | void MyLuaHandle::RegFunctions() | ||
{ | { | ||
Line 128: | Line 124: | ||
REG_FUNCTION( GetTime ); | REG_FUNCTION( GetTime ); | ||
} | } | ||
</ | </nowiki>}} | ||
Again this has been shorten using a macro. And thus you will need to define this functions in another file. | Again this has been shorten using a macro. And thus you will need to define this functions in another file. | ||
{{CodeBlock|lang=cpp|<nowiki> | |||
extern "C" | extern "C" | ||
{ | { | ||
Line 180: | Line 176: | ||
return 1; | return 1; | ||
} | } | ||
</ | </nowiki>}} | ||
Note: The function name must be the same as the one used in the macro with lua infront. | Note: The function name must be the same as the one used in the macro with lua infront. | ||
Line 188: | Line 184: | ||
On the top of your new LuaHandle class you need to add a function allowing global access to your Lua instance. Add something like this: | On the top of your new LuaHandle class you need to add a function allowing global access to your Lua instance. Add something like this: | ||
{{CodeBlock|lang=cpp|<nowiki> | |||
MyLuaHandle* g_LuaHandle = NULL; | MyLuaHandle* g_LuaHandle = NULL; | ||
MyLuaHandle *GetLuaHandle() | MyLuaHandle *GetLuaHandle() | ||
Line 194: | Line 190: | ||
return g_LuaHandle; | return g_LuaHandle; | ||
} | } | ||
</ | </nowiki>}} | ||
And in your constructor of your LuaHandle add a call to set the instance when you make a new one: | And in your constructor of your LuaHandle add a call to set the instance when you make a new one: | ||
{{CodeBlock|lang=cpp|<nowiki> | |||
MyLuaHandle::MyLuaHandle() : LuaHandle() | MyLuaHandle::MyLuaHandle() : LuaHandle() | ||
{ | { | ||
Line 203: | Line 199: | ||
Register(); | Register(); | ||
} | } | ||
</ | </nowiki>}} | ||
Note: Its important to have Register here other wise it wont work! | Note: Its important to have Register here other wise it wont work! | ||
And finally in your game rules constructor make a new LuaHandle: | And finally in your game rules constructor make a new LuaHandle: | ||
{{CodeBlock|lang=cpp|<nowiki> | |||
CGameRules::CGameRules() | CGameRules::CGameRules() | ||
{ | { | ||
Line 218: | Line 214: | ||
new MyLuaHandle(); | new MyLuaHandle(); | ||
} | } | ||
</ | </nowiki>}} | ||
== Project Settings == | == Project Settings == | ||
Line 233: | Line 229: | ||
This is a bit harder and you will need to consult the lua documents for this but here is an example on how ges does it: | This is a bit harder and you will need to consult the lua documents for this but here is an example on how ges does it: | ||
{{CodeBlock|lang=cpp|<nowiki> | |||
void CGELUAGamePlay::PostRoundBegin() | void CGELUAGamePlay::PostRoundBegin() | ||
{ | { | ||
Line 242: | Line 238: | ||
} | } | ||
} | } | ||
</ | </nowiki>}} |
Revision as of 17:27, 18 July 2023
Adding Lua to
Source is quite easy, and setting up multiple instances is even easier.
Source
Add these two source files to your project:
Init Lua
Once that is done you will need to make sure your game code calls the init and shutdown functions. In gameinterface.cpp add this into DLLInit before the return true statement (around line 557):
And in the DLLShutdown function add this:
Also make sure you include the manager header file up the top:
Making Your own Lua Instance
Now this is the fun part, making your own lua instance. You will need to make your own class that inherits from LuaHandle and provides functionality for Init, Shutdown, RegFunctions and RegGlobals.
Init
Init gets called after Lua gets init and thus provides the best place to load your script.
Shutdown
Shutdown handles the shutting down of Lua and should do things in here like call shutdown functions in your script.
RegGlobals
RegGlobals allows us to register global variables in lua.
Macros provided allow for different types as well. LG_DEFINE_INT for int, LG_DEFINE_STRING for string and LG_DEFINE_BOOL for boolean.
RegFunctions
Like Globals, RegFunctions is where you put all the c functions you want to expose to Lua.
Again this has been shorten using a macro. And thus you will need to define this functions in another file.
Note: The function name must be the same as the one used in the macro with lua infront.
Creating The Instance
On the top of your new LuaHandle class you need to add a function allowing global access to your Lua instance. Add something like this:
And in your constructor of your LuaHandle add a call to set the instance when you make a new one:
Note: Its important to have Register here other wise it wont work!
And finally in your game rules constructor make a new LuaHandle:
Project Settings
In the server project settings you need to add GE_LUA to the preprocesser options (Configure properties -> c++ -> Preprocesser -> Preprocesser defines). Also you will need to add the lua lib (should be the same name as the lib file, mine's lua5.1.lib) to the linker settings (Configure properties -> Linker -> Input -> Additional Dependencies )
Note: Make sure you do this for both release and debug.
Now place the lua lib into the src/lib folder. And you should be able to compile and now have a working Lua.
Creating Calls from c into Lua
This is a bit harder and you will need to consult the lua documents for this but here is an example on how ges does it: