• Advertisement
Sign in to follow this  

Individual scripts per object in Lua

This topic is 1771 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

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.

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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
  newNode.script = loadstring(_script)
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

Share this post


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

  • Advertisement