onChangeEntity accepts just one entity argument. How can I find what changes on it? For example, I want to handle the case: the entity’s transformation changes and others keep the same. How can I identify the case?
I guess you have to write your own method(s) to keep track (store to variables or attributes) of the “properties” in question. Then compare the new values to stored one when the observer will fire. (Then replace the “storage” values with new values to to be ready for the next compassion comparison…)
1 Like
Yes I agree. Attach a dictionary to the “watched” instance (transforms only apply to group and component instances,) with a "transform"
attribute.
To purposely “watch” an instance’s transform, call …
def watch_entity(entity, observer)
save_transform(entity) if entity.respond_to?(:transformation)
entity.add_observer(observer)
end
def save_transform(entity, hash = nil)
if entity.respond_to?(:transformation)
# Build the transform data hash if not passed as 2nd arg:
hash = transform_properties(entity.transformation) if hash.nil?
# Must convert hash to array of nested key/value pair arrays.
# API dictionaries cannot store symbol keys, only string keys:
data = hash.transform_keys(&:to_s).to_a
entity.set_attribute(DICT, "transform", data)
end
end
Example, onChangeEntity
callback using the transform_properties
method from the “How to compare transformations” topic thread …
def onChangeEntity(entity)
if entity.respond_to?(:transformation)
prev = entity.get_attribute(DICT,"transform",nil)
# Will be nil if no "transform" attribute exists.
if prev # an array written by "save_transform" method
hash = prev.to_h.transform_keys(&:to_sym)
trans = transform_properties(entity.transformation)
unless hash == trans # Use Hash#== not Object#!=
# The entity's transform was changed.
# ... do something here ...
# Then update the dictionary with new data:
save_transform(entity, trans)
end
end
else
# check other things
end
end
1 Like