Reuse the output of a node without re-executing the node


How to store data so it doesn’t have to be recomputed

Some nodes change their output every time they’re executed. The Count node is an example. If you feed an event into any of its input ports — Increment, Decrement, or Set Count — the node outputs a count that’s different from the previous count (except of course in special cases, like doing Set Count when the node is already at that count). What if you don’t want to change the count, and you just want to output the current count?

Here’s an example that increments a count each time the user presses a mouse button, and displays the current count in a window every 5 seconds. (This same pattern could be applied to practical situations, such as a sensor incrementing a count each time a person passes through a doorway and the count periodically being sent over a network to monitor the building’s occupancy.)

The key to this composition is the Hold Value node. Each time the Count node outputs a value, the Hold Value node holds on to it (in other words, stores it). Every 5 seconds, an event from Fire Periodically hits the Hold Value node’s Update port, and the Hold Value node outputs the count that it’s storing.