Jump to content
Site Stability Read more... ×
  • Advertisement
Sign in to follow this  

Lua interface design

This topic is 2788 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I have written a game engine which purpose is to power 2D games. The engine is written in C++ (even though some parts are written in Objective-C and Java since it runs on mobile devices), and it is very time consuming to design things like GUI and such. Add a button, start the game and realize that you have to move it a couple of pixels, shut down, make the fix, recompile and start again. You're probably familiar with the scenario and knows that it sucks.

My solution to this is to make a simple editor. The editor will support an outline (SceneGraph) with its corresponding nodes and entities such as sprites and widgets. With this editor I will be able to add sprits to the scene and position them pretty fast with the mouse and text input fields. But since the entities shouldn't be plain dead I want to be able to add a script to each and every node and entity. I can see it in front of me where you add a sprite to the scene, right-click on it and clicks on "Add Script" where a text window appears and you can write your script.

The scripting language I have begin implementing in my engine is Lua, and I have mapped some classes with tolua++ and it works fine. But now I'm stuck with some design issues...

How should I design the Lua usage? My first idea was to have one script file for each and every node and entity I add to the scene. Those files would look something like this, with functions called from C++.
function onCreate()

function onRender()

function onTick()

function onDestroy()

The problem is that I haven't ever worked with a game engine using a scripting language so I don't really know how people wants to work with it.

So to my question.
How do game engines usually utilize scripts and are there any good sites about this I should read? Also, is my initial approach something that sounds OK or what should I think about. Just need some help to get me started. :)

Thanks for your time!

Share this post

Link to post
Share on other sites
Refering a function is quite easy in lua, therefor I would utilise this feature when writing an event engine.
Here's a basic idea for an event handler:

createEvent = function( pEventType, pEventData)
return {type=pEventType,data=pEventData}

dispatchEvent = function(pEvent,pEventHandler)
if pEvent==nil or pEventHandler==nil then

local eventFunction = pEventHandler[pEvent.type]
-- fall back to default ?
if eventFunction==nil then
eventFunction = pEventHandler.default

-- any event function present ?
if eventFunction~=nil then

createEventHandler = function( pOnTrigger,pOnCreate,pOnTick,pDefault)
local eventHandler =
onTrigger = pOnTrigger,
onCreate= pOnCreate,
onTick= pOnTick,
default = pDefault,
dispatcher = eventDispatcher,

return eventHandler

-- example
dummyEvent = function( pEventData)
print("hello world")

-- create handler and a event (attach handler to entity, create event on-the-fly)
local eventHandler = createEvent(dummyEvent,nil,nil,dummyEvent)
local event = createEvent("onTrigger",nil)

-- dispatch event
dispatchEvent( event,eventHandler)

Then I would provide a library of event functions and event handlers, many entities will share the same event handling, the rest is a simple assignment of event function to event handler and event handler to entity.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!