lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


My app allows users to create scriptlets and I was hoping to get some
feedback on what might make a good API for the user to interact with.

Each scriptlet ( a stand-alone lua state ) has a predefined number of
inputs and outputs. The user can attach functions which are called when
the inputs are changed. The inputs and outputs are objects which have 3
dimensions - string, position ( 0.0 -> 1.0 )  and value ( floating point
). The objects have the following methods -

getValue(), setValue( val ), getString(), setString( s ), getPosition(),
setPosition( pos ), addEventHandler( func )

My current implementation creates lua objects for each input and output
and places them in the global namespace with names 'input_x' and
'output_x'. The event handler gets called when the input changes and the
output is set accordingly. An example script might look something like
 
-- average the 2 inputs and set the output
function average( changedInput )
  val0 = input_0:getValue()
  val1 = input_1:getValue()
  output_0:setValue( ( val0 + val1 ) / 2 )
end
 
input_0:addEventHandler( average )
input_1:addEventHandler( average )
 
The main problem with this implementation is that there is no way to
easily iterate the inputs and outputs. That makes it difficult to write
generic scripts that could average any number of inputs, for example. I
also think using the global namespace is pretty ugly as well.
 
Each scriptlet also has a single timer available to it. It's current
implementation is as a module - a timer script might look something like
 
count = 0
-- count to 10 over and over again
function onTimer()
  count = count + 1
  output_0:setValue( count )
  if count == 10 then
    count = 0
  end
end
 
Timer.addEventHandler( onTimer )
Timer.start( 1000 ) -- call every 1 second
 
I don't like the idea that the inputs and outputs are global objects
while the timer is a static module. My initial thought is to make a
Controls module which contains 2 arrays, Inputs and Outputs. An
averaging script would then look something like -
 
function average( changedInput )
  count = 0
  sum = 0
  for i,v in ipairs(Controls.Inputs) do 
    sum = v:getValue()
    count = count + 1
  end
  Controls.Outputs[1]:setValue( sum / count )
end

-- attach handler to each input
for i,v in ipairs(Controls.Inputs) do 
  v:addEventHandler( average )
end

Any comments/suggestions? 

Thanks-

John