Forums | developer.brewmp.com Forums | developer.brewmp.com

Developer

Forums

Forums:

I'm wondering if the new() method is supported for a Lua class?

Here's the issue: I've created a transition effect in TrigML + Lua. The trig is loaded into a group, and the trig contains a simple call to a lua method. The lua method uses a timer. When only one instance of the trig is loaded into a group, it works fine. But if two instances of the trig are loaded into separate groups (to perform some synchronized animation), it seems as if the same instance of the Lua class is invoked in both cases. My Lua class looks like the following:

-- Lua script
effectshifter = {}

-- animate the x value of a trigml dom element

effectshifter.el = "transformroot" -- the name of the element to animate

effectshifter.xstart = 40
effectshifter.xend = 0

effectshifter.duration = 2000 -- two seconds

effectshifter.elapsed = 0
effectshifter.timer = nil

function effectshifter.startmeup( elem, xs, xe )

local nilstr = wstring.new 'nil'
local maxstr = wstring.new 'max'

-- make sure the timer is not already in use
if effectshifter.timer == nil then
effectshifter.el = elem

-- x animation
effectshifter.xstart = xs
effectshifter.xend = xe

effectshifter.elapsed = 0
effectshifter.update()
effectshifter.timer = ui.setTimer( effectshifter.timeslice, 100 )
end

end

function effectshifter.timeslice()
effectshifter.elapsed = effectshifter.elapsed + 100
effectshifter.update()
if effectshifter.elapsed < effectshifter.duration then
effectshifter.timer = ui.setTimer( effectshifter.timeslice, 100 )
else
effectshifter.timer = nil
ui.throw( "testblend", "effectend" )
end
end

function effectshifter.update()
t = effectshifter.elapsed / effectshifter.duration

el = ui.element(effectshifter.el) -- "transformroot")
el.x = effectshifter.lerp( t, effectshifter.xstart, effectshifter.xend )
end

function effectshifter.lerp( t, s, e )

return s + ((e - s) * t) ;
end

And the call from trigml is simply effectshifter.startmeup( $elname, $xstart, $xend )
I have tried to modify the Lua class to use the new() method:

function effectshifter:new()
obj = {}
setmetatable(obj, {__index = self})
return obj
end

and then tried the following call from trigml:

effectshifter.new().startmeup( $elname, $xstart, $xend )

and I have also tried using "self" in Lua, and trying the following from trigml

effectshifter.startmeup( effectshifter.new(), $elname, $xstart, $xend )

In both cases the Simulator doesn't know how to handle the return value from new() method.

Does this mean that my effect needs to be implemented as an Actor instead of in Lua?

Hi,
I will look into this and get back to you.
Chris

Hi,
I will look into this and get back to you.
Chris

Hi,
I think the issue you are encountering is that you are not able to refer to Lua variables or functions in the call from TrigML to Lua.
To achieve this you will need to do a small workaround. For example if you were to pass in a reference (e.g.. the group name) into a Lua call instead and within Lua have a small shim layer to decide which instance to call.
In the Lua you could have a table that has that reference name and a pointer to the instance that relates to it and invoke the method based upon the reference passed from TrigML.
It seems that this way of calling Lua is desirable and I will pass it onto Product management for consideration in the future.
Chris

Hi,
I think the issue you are encountering is that you are not able to refer to Lua variables or functions in the call from TrigML to Lua.
To achieve this you will need to do a small workaround. For example if you were to pass in a reference (e.g.. the group name) into a Lua call instead and within Lua have a small shim layer to decide which instance to call.
In the Lua you could have a table that has that reference name and a pointer to the instance that relates to it and invoke the method based upon the reference passed from TrigML.
It seems that this way of calling Lua is desirable and I will pass it onto Product management for consideration in the future.
Chris

As Chris says, the way to do this would be to provide the name of the lua variable you wish to store the new object in as a parameter of the 'new' method.
e.g.

This new variable could then be accessed by a Lua veneer.

You may wish to have a look at the Lua Scripting (Part 2) section of the Trig How To document, which contains an example that uses this approach.

As Chris says, the way to do this would be to provide the name of the lua variable you wish to store the new object in as a parameter of the 'new' method.
e.g.

This new variable could then be accessed by a Lua veneer.

You may wish to have a look at the Lua Scripting (Part 2) section of the Trig How To document, which contains an example that uses this approach.