PDA

View Full Version : What happens when "assinging" methods?



Weltenbrand
October 12th, 2020, 14:17
In some places e.g. number_linked the following construct is used


node.onUpdate = someFunction

Which executes the someFunction if the node is changed (or some other condition depending on what method is "assiniged").

It looks like it is assinging and overriding an function for the node object. But since e.g. multiple number_linked work with the same databasenode it cannot be a simple override.
It behaves more like DB.addHandler.

Is this pattern a wrapper for DB.addHandler? If yes where does the handler get removed if the control is closed?

Trenloe
October 12th, 2020, 14:27
Some background info here: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGU/pages/4063650/databasenode#onUpdate

It's not exactly a wrapper for DB.addHandler, but it behaves similarly (in terms of running a function when a database node, or range of nodes in the case of DB.addHandler, is updated). DB.addHandler is not tied to an object and so any handlers that have been added need to be removed when the code scope is removed/destroyed to ensure there's no invalid functions trying to be called.

<node>.onUpdate is tied to a specific database node object. So when that database node object is removed/destroyed then so is the handler.

Weltenbrand
October 12th, 2020, 14:31
Ok thank you so far.

Is my assumption write that I can <node>.onUpdate = something multiples times and all "handlers" wil lbe called?

Trenloe
October 12th, 2020, 14:42
Is my assumption write that I can <node>.onUpdate = something multiples times and all "handlers" wil lbe called?
I'm not 100% sure, but I would guess no - as there's no way to remove previous onUpdate function assignments it would make sense if you could only have one and using <node>.onUpdate would override any previous assignment. If you want to call multiple handlers you could use different node objects variables - all attached to the same database path, but each a different variable. Or, probably a better idea, have a single event handler that calls different function if you need to have multiple functions.