Skip to main content
GameDev.net gamedev.net
🔒 Locked

Lua interface design

Started by JoachimKlahr Feb 29, 2012 at 9:37 PM 1 replies 2.3k views
Original Post
JoachimKlahr
JoachimKlahr
Hi,

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()
end

function onRender()
end

function onTick()
end

function onDestroy()
end


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!
http://epiccode.org/
meeshoo
meeshoo
I have no experience with this, but you might take a look at World of Warcraft's GUI scripting API. It is so powerful that users created thousands upon thousands of GUI mods, some very sophisticated. http://www.wowwiki.com/World_of_Warcraft_API
Ashaman73
Ashaman73
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}
end

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

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

-- any event function present ?
if eventFunction~=nil then
eventFunction(pEventData)
end
end

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

return eventHandler
end

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

-- 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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.