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.
Declare the model
Section titled “Declare the model”- Open
/plot classesand create instance class PlayerStats. - Add a public Number field
points. - Define a constructor with Number parameter
initialPointsand assign points from it. - Define public instance method AddPoints, parameter Number
amount, return Void. - 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 + amountThis is a conceptual block layout. Configure field Variables and arithmetic inputs in the editor; the diagram is not a text language.
Construct once and reuse
Section titled “Construct once and reuse”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.
Observe identity
Section titled “Observe identity”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.
Extend with inheritance
Section titled “Extend with inheritance”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.