Skip to main content

Observers

These functions allow callbacks to be performed in response to nodes being updated.

The defined callbacks will take place following the execution of the script, or during a vrYield.

vrAddPropertyObserver

Adds an observer to monitor and respond to value changes for a single property.

local myNode = vrCreateNode("Assembly", "ObservedNode", vrScenesNode())
vrYield(0)

local myCallback = function(node, value)
print(node:getName() .. " enabled: " .. tostring(value))
end

vrAddPropertyObserver(
"enabledObserver",
myCallback,
"Assembly",
"Enabled"
)

myNode.Enabled = false
vrYield(0)

myNode.Enabled = true
vrYield(0)

vrRemoveObserver("enabledObserver")
vrDeleteNode(myNode)

vrAddNodeObserver

Adds an observer to monitor and respond to changes on a Node.

The function takes a map of callback functions. These will be called in response to the appropriate changes.

KeyCallbackCallback Signature
valuesChangedCalled in response to a property's value being changed.function(node)
addingToRecycleBinCalled in repsonse to the node being sent to the recycle bin.function(node)
restoredFromRecycleBinCalled in repsonse to the node being returned from the recycle bin.function(node)
nodeDestroyedCalled in response to the node being destroyed.function(node)
local myNode = vrCreateNode("Assembly", "ObservedNode", vrScenesNode())
vrYield(0)

local callbackMap = {
valuesChanged = function(node)
print(node:getName() .. " enabled: " .. tostring(node.Enabled))
end,

addingToRecycleBin = function(node)
print(node:getName() .. " removed")
end
}

vrAddNodeObserver(
"nodeObserver",
myNode,
callbackMap
)

myNode.Enabled = false
vrYield(0)

myNode.Enabled = true
vrYield(0)

vrDeleteNode(myNode)
vrYield(0)

vrRemoveObserver("nodeObserver")

vrAddMetaNodeObserver

Adds an observer to monitor and respond to changes on instances of a MetaNode.

The function takes a map of callback functions. These will be called in response to the appropriate changes.

KeyCallbackCallback Signature
nodeCreatedCalled in response to a node being created.function(meta, node)
nodeRenamedCalled in repsonse to a node being renamed.function(meta, node)
nodeParentChangedCalled in response to a node being moved.function(meta, node, oldParent, newParent)
nodeAddingToRecycleBinCalled in repsonse to a node being sent to the recycle bin.function(meta, node)
nodeRestoredFromRecycleBinCalled in repsonse to a node being returned from the recycle bin.function(meta, node)
nodeDestroyingCalled in response to a node being destroyed.function(meta, node)
local callbackMap = {
nodeCreated = function(meta, node)
print(node:getName() .. " created")
end,

nodeRenamed = function(meta, node)
print(node:getName() .. " renamed")
end,

nodeParentChanged = function(meta, node, old, new)
if(old and new) then
print(node:getName() .. " moved from " .. old:getName() .. " to " .. new:getName())
end
end,

nodeAddingToRecycleBin = function(meta, node)
print(node:getName() .. " removed")
end
}

vrAddMetaNodeObserver(
"nodeObserver",
"Assembly",
callbackMap
)

local myNode = vrCreateNode("Assembly", "ObservedNode", vrScenesNode())

myNode:setParent(vrLibrariesNode())
vrYield(0)

myNode:setName("NamedNode")
vrYield(0)

vrDeleteNode(myNode)
vrYield(0)

vrRemoveObserver("nodeObserver")

vrAddTransformNodeObserver

Adds an observer to monitor and respond to the transform related value changes of a Node.

local myNode = vrCreateNode("Assembly", "ObservedNode", vrScenesNode())
vrYield(0)

local myTransformCallback = function(node, value)
print(node:getName() .. " transformed:")
print(tostring(value))
end

local myEnabledCallback = function(node, value)
print(node:getName() .. " enabled: " .. tostring(value))
end

vrAddTransformNodeObserver(
"transformObserver",
myTransformCallback,
myEnabledCallback,
myNode
)

myNode.Enabled = false
vrYield(0)

myNode.Enabled = true
vrYield(0)

local transform = myNode.Transform
transform.position.x = transform.position.x + 1.0
vrYield(0)

vrRemoveObserver("transformObserver")

vrDeleteNode(myNode)
vrYield(0)