Is there Node callback on Node's position update or just on every 'update' call

I have two moving objects and I’d like to draw a line between them, I understand that one way to implement it is to use update method of a Scene and update ‘line’ object accordingly. But I’m afraid of this way, because in future it might over complicate scene class, so I’m looking for other options.

Have you considered making your own sub-classes of the node types you plan on using, overriding the set position methods, and adding your own callbacks for when they change?

1 Like

Yes, I thought about it and probably it’s the best way to do it - but I might need it for several node types, in this case I’d need to create a lot of sub-classes. Now I’m also thinking maybe it’s easier to create special Node sub-class which only purpose will be to call custom callback on every update and then just to add this node to every Node I want to ‘listen’ for updates as a child, how do you think - is it a good way?

While it’s a good idea, it will not work, since the child node it’s relative to the parent, so it’s position does not change unless you explicitly call setPosition() on it.

For instance, if you do a parentNode->setPosition(x,y), the child will not receive a setPosition() call, so there won’t be an update for you to handle.

1 Like

Got it, thank you, but at least for every ‘update’ call of parent - child’s ‘update’ method will be called as well - won’t it? Maybe it’ll be enough for me because I’ll be able to get parent Node from the callback and it’s current position, I might be wrong though, I’ll try to check it.

I don’t think that is correct. Update doesn’t get called unless you call node->scheduleUpdate() for any node, so for instance you can do that in the init() override of the node you’re creating. You only need to schedule it once, and update will be called from that point on (for that specific node).

The alternative is to just schedule an update on the parent node, and for any child nodes, you manually call update(). That would also work for setPosition(), for instance, if setPosition is called on the parent, then you can iterate through the child nodes, and call any method on them (even your own custom methods) to do any updates to child nodes.