Game Development Community

dev|Pro Game Development Curriculum

LUA Plugins for Torque

by Peter Simard · 04/18/2010 (11:04 pm) · 13 comments

torque.abigholeintheweb.com/public_system/useruploads/lua_plugin.zip

This code will allow you to easily add plugins/addons to your game similar to World of Warcraft. Users can drop scripts into their addon folder and they will automatically load.

This resource is designed allow users restricted access to your game functions. It is not intended to be a replacement for TorqueScript, but rather a sandbox environment. You must specify the game functions you want to expose to your players.

Installation
Place the LUA directory inside your engine/libs folder.

Add "../lib/lua" to C++/General/Additional Include Directories
Add "lua.lib" to Linker/Input/Additional Dependencies

Add luaManager.h and luaManager.cc to your project

Add lua.cs to your game's client directory. Add exex("./lua.cs") inside your initClient() function.

Add loadAddons() to the end of initClient().

Usage
Place LUA scripts inside the addon directory. You can change the default directory in lua.cs loadAddons().

You can expose new available functions to the plugins by adding them to the registerFunctions() C++ functions. Note: You must then implement the new function above it. See the print command as an example.

You can create new events from TorqueScript by using the lua_event function. Example:
lua_event("onDamageDone", %damageDone, %againstName, %playerName, %hitType, %damageType);
In this example, this code would be placed where your client processes the combat damage log it recieves from the server. The corresponding LUA code would look like:
function onDamageDone(damageDone, attackerName, defenderName, hitType, damageType)
    print("You did " .. damageDone .. " damage!");
end

You can create as many events in script as you want. LUA scripts that do not implement the corresponding functions are simply ignored.

If you want to call a function on only one specific script, you can use the luaFunction script function. The first parameter is the script number you wish to call. You can pass the script number to TorqueScript using:

int scriptNumber = LuaManager::getStateIndex(L);

#1
04/19/2010 (6:58 am)
This would be great for exactly what Blizzard uses Lua for - UI add-ons. It would probably be cool to tie it into the camera system for scripting 'cutscenes' too. Or a RPG conversation system. Gah - or a dozen other things.
#2
04/19/2010 (7:25 am)
Oh nice resource.
#3
04/19/2010 (10:41 am)
Thanks Peter, this can be very useful.

I will play with it for sure.

Just to be sure I understand the scope of this, if fact if you want to use properly, you need to set all the GUI in LUA and no more in TS?

So we need to be careful to chat type of function we can exposed to avoid any "object creation" that can be exploited.

I'm sure you did part of this job for your game, is there any chance that you can share some of the "Register" function you made so we have a starting point?

What version of LUA did you use?
Could you confirm the targeted engine of this resource (T3D,TGEA,...)?

If you used it for GUI add-on, can you share some samples?
#4
04/19/2010 (12:18 pm)
The MMO kit uses a LUA addon as well. Perhaps you should take a look on how they intergrated it as well. I think their website is:

mmotest.lethal.net/doku.php
#5
04/19/2010 (2:07 pm)
very nice, this can be used to add other languages?...
#6
04/19/2010 (11:26 pm)
@Jessel,

Excellent idea, I will do that for sure.
#7
04/20/2010 (12:04 am)
MMO kit does not use LUA (Python).
#8
04/20/2010 (11:27 am)
humm sad, so now I'm looking for Peter feedback :D
#9
04/21/2010 (12:37 pm)
The implementation is working well except that I had to comment
dStrcpy(arg8, argv[9]);

because argv[9] = bad pointer.

I'm not sure to understand the root cause of this. If someone can explain I will appreciate.

Thanks,
#10
04/21/2010 (3:01 pm)
@Joshua Halls.. Opps I sit corrected.
#11
04/21/2010 (10:28 pm)
This could be interesting, I'll have to look at this :O) I'm a fan of Lua.

I wrote some Lua addon code that lets Lua and TorqueScript talk to each other somewhat.. maybe it can be useful for this too?

Here's my link http://www.torquepowered.com/community/resources/view/9665
#12
04/21/2010 (11:33 pm)
Joe,
I got your implementation in my bookmark as well.
It was moved to T3D here.

Did you just used Lua for function or also for GUI implementation like Wow, War etc...?

I think in your implementation you are not limiting the function that can be called from Lua where as this one does, which helps up to create a sandbox and avoid cheating :D
#13
04/22/2010 (12:08 am)
I'm not an expert in C++ yet, but don't the current implementation is open to buffer overflows?

Extract from code:
char* arg1 = new char[1024];
...
dStrcpy(arg1, argv[2]);

I mean if you pass a string larger than 1024 characters, you will write out of your buffer.
Can someone confirm? If so, we need to clearly review this code as it's opened to buffer overflow and can cause bad issue (desired or not by the user).