• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Individual scripts per object in Lua

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Noci  Members

Posted 16 March 2013 - 01:11 PM

Hi,

in my current project I have to deal with a graph where each node should provide the capability to run different user defined behaviours on given triggers. An XML definition could (for example) look like this:

<node name="node 1">
<scripts source="function onEnter() print('do something') end"/>
</node>

<node name="node 2">
<scripts source="function onEnter() print('do something different') end"/>
</node>


So as you can see the function onEnter (which will be called when entering the node) appears two times with different implementations through these scripts. While I could solve the problem by having one state per object, this would mean to have a lot of states spread throughout the application which does not seem optimal to me.

I was also thinking to parse the scripts before sending them to Lua (through luaL_loadstring) and replace all functions with dynamically generated functions (e.g. onEnter<UID> instead of onEnter) but somehow I got the feeling that there must be a better solution for this problem.

I'd really be thankful for any guidance you can give me.

### #2Gambini  Members

Posted 17 March 2013 - 10:55 AM

You can wrap all of the <scripts> tags with

return function() <scripts> contents end

when you load them, and set the function environment for those. If you are using 5.1, then getfenv and setfenv are what you are looking for. If you are using 5.2, then the function will have an _ENV variable. I haven't used them myself, so I can't really get any more specific, but maybe that will help direct your search.

Edited by Gambini, 17 March 2013 - 10:56 AM.

### #3superman432432  Members

Posted 17 March 2013 - 12:26 PM

Is it necessary that you use xml? If not you could store data in lua tables instead (and access them via the c stack functions), and then it would just be a matter of setting scripts to be used programmatically, e.g.

node1={script=somefunc}

Also this would most likely involve messing with the lua env if you do this (which is easy).

### #4Ashaman73  Members

Posted 18 March 2013 - 01:32 AM

I would just put it all in one VM using a simple mechanism like this:

obj_list = {}

createNewObject = function(_newId)
local newObj =
{
nodes = {}
}
obj_list[_newId] = newObj
return newObj
end

attachNode = function( _objId, _nodeName, _script)
local obj = obj_list[_objId]
local newNode = {}
obj.nodes[_nodeName] = newNode
end

executeScript = function(_objId, _nodeName)
local obj = obj_list[_objId]
local node = obj.nodes[_nodeName]
if node==nil or node.script==nil then
-- error handling
return
end

return node.script()
end


Ashaman

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.