Sign in to follow this  
Nico Becherer

Individual scripts per object in Lua

Recommended Posts

Nico Becherer    102

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
Gambini    384

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
superman432432    108

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
Ashaman73    13715

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this