Learn

Chapter 05 Functions & Collections

#043 Pure vs Impure Functions

Same function, two looks. One with exec pins, one without. That changes when it runs.

  • 1:15 video
  • Written lesson
  • Beginner
Video lesson: Pure vs Impure Functions, 1:15

When it runs

Pick the wrong one, and a function runs more often than you think.

Impure functions have exec pins. They run once, when execution reaches them. Their outputs keep that result, no matter how many nodes read them.

Tick Pure in Details, and the exec pins disappear. Now it runs every time a connected input reads its output.

Use pure for read-only work: getters, math and checks. Engine nodes follow the same rule: Get Actor Location is pure, Set Actor Location is impure.

Example: a damage roll

Roll Damage is a pure function returning a random number. Its output feeds Take Hit and Show Damage Number.

Two readers, so it rolls twice. Take Hit deals twelve, but the screen shows thirty.

Make Roll Damage impure, and both read one result. Keeping it pure? Store the roll in a variable first, then let both nodes read that variable.

Common mistakes

  • Pure functions in loops. Wire a pure function into For Each Loop’s Array input, and it runs on every iteration. If that function is expensive, the cost repeats every pass.
  • Call it once, promote the result to a local variable, and loop over that.

In short

Pure runs per read. Impure runs once.

Covered in this lesson

  • Pure
  • Get Actor Location
  • Set Actor Location
  • For Each Loop
  • Promote to Local Variable

Newsletter

Development updates are coming to your inbox.

The newsletter opens soon. Until then, new lessons, tools and Beach Hunters progress appear first on our channels, linked below.