Skip to content

Model state with a class

Use a class when a record has related fields and behavior. This tutorial creates a reusable PlayerStats object; it does not automatically create a per-player database.

  1. Open /plot classes and create instance class PlayerStats.
  2. Add a public Number field points.
  3. Define a constructor with Number parameter initialPoints and assign points from it.
  4. Define public instance method AddPoints, parameter Number amount, return Void.
  5. In AddPoints, set the current object’s points to its old value plus amount using the field Variable and Number arithmetic.
Class: PlayerStats
Field: Number points
Constructor(initialPoints: Number)
points ← initialPoints
Method AddPoints(amount: Number): Void
points ← points + amount

This is a conceptual block layout. Configure field Variables and arithmetic inputs in the editor; the diagram is not a text language.

Create Public PlayerStats Variable stats. On Plot Start, New Object → PlayerStats → initialPoints 0 → result stats. Later, Call Method on stats → AddPoints → amount 1.

Display Points: ${public.stats.points} from a suitable Player event. Because stats is Public, this example stores one shared record. For per-player records, use a typed dictionary keyed by a suitable player identity and create/check each entry explicitly.

Assign another compatible Variable from stats in Reference mode. Mutating points through either receiver affects the same object. Reassigning one Variable to a new PlayerStats does not replace the object held by the other.

Compare with Deep Copy on an input when you need a separate snapshot. Do not repeatedly insert a class-item template when you intend to reuse stats: each evaluation constructs a fresh object.

Create a child class that extends PlayerStats, add a field, and explicitly Override a compatible public instance method. Call through a parent-typed Variable holding the child and observe runtime dispatch. Ensure the child constructor satisfies the parent constructor before compiling.