Sign in to follow this  
AverageJoeSSU

Calling function when lua table value is changed.

Recommended Posts

AverageJoeSSU    564
I would like to call functions based on what value inside of a table is changed. I know i can use metamethods but i dont quite understand if i can do this. so if x,y,z values change i would call setPosition(x,y,z) or if Active changes to true from false I would call self.setActive() . would i use the __index metamethod? how can i check for the changed value. would i have to duplicate the table with old values and do a compare? is there a simpler way to do this? Thanks for any ideas. -Joe

Share this post


Link to post
Share on other sites
AverageJoeSSU    564
after thinking about it... i think i'll just do the check outside of the lua environment, will make it easier. I almost feel like something like x,y, and z position that can possibly change every frame would likely want to exist in the native level and be queryable by lua.

Share this post


Link to post
Share on other sites
ddn3    1610
You might want to keep all that data inside Lua if Lua owns those objects. Calling a native callback every time you want to query the state of the object in Lua will be very expensive. My design is all state is stored in Lua and only when the object is "dirty" does it push the state back into the native replica and only then once per frame is possible to minimize the overhead of Lua<->Native interface.

Good Luck!

-ddn

Share this post


Link to post
Share on other sites
AverageJoeSSU    564
I should prolly put more context....

I'm referring to my editor's interaction with lua... let alone lua's interaction with the engine.

x,y,z is a bad example, since the native renderer/updater/physics is the only thing that cares about x,y,z EVERY frame.

but... basically i have the ability to edit a lua table live... in a WPF control... pic. The problem is I am changing the state, but not handling the state change.

But the thing is... being able to manipulate and handle these things is different based upon each value... and i may be missing a layer of interaction altogether.

Share this post


Link to post
Share on other sites
m_switch    234
This can be done, however like ddn3 noted it can be expensive. I wouldn't use this in time-critical code.

The way you'd do this is to use a proxy table that acts as an 'event handler'. The proxy table never holds any data, but has the metamethod for __newindex. The issue that you probably ran into that newindex is only called, as the name suggests, when a new index is accessed. Thus, after the first time you set the index the metamethod no longer gets called.

As an example, here's how it works written in Lua. You'd obviously want to connect your proxy to your C callback in the real version.


//This would be your monitored table/object
local realTable = {}

//This function is called when a property is set
local function OnNewIndex(self, key, value)
print(("Updating field: %s to %s"):format(key, value))
//Set the property on the real table
getmetatable(self).__index[key] = value
end

//For anything that might change the properties you want to watch they have to access the real table through the proxy
local proxy = setmetatable({}, {__newindex = OnNewIndex, __index = realTable})

assert(proxy.foo == nil)
proxy.foo = "bar"
assert(proxy.foo == "bar")


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