Skip to main content
Version: 🚧 Alpha 🚧

Classes and Objects

GAML classes define object types with attributes and methods. Objects are not agents: they do not have their own reflexes, schedule, or population.

Key concepts​

  • A class defines the attributes and methods available on its objects. Declare one with class ClassName { ... }.
  • An object is a value created by calling the class constructor: ClassName(attr: value, ...).
  • Classes can have attributes (like species variables) and actions (like species actions, but on objects).
  • Classes support inheritance with parent: ClassName.
  • Classes can be virtual (abstract) with virtual: true - they cannot be instantiated directly.
  • Objects can be stored in any GAML variable, passed as arguments, stored in lists, and used as map keys.

Step-by-Step overview​

The model library includes five complete class examples, from basic instantiation to advanced object and agent interactions.

  • Class Definition and Instantiation - Declare a class, create objects with the constructor syntax ClassName(attr: value), store objects in variables and lists.
  • Attributes and Actions - Read and write attributes (dot notation: obj.attr <- value), call actions (obj.action()), chained calls, comparing objects, passing objects as arguments.
  • Inheritance - Extend a class with parent:, override actions, call parent implementation with super.action(), multi-level inheritance, runtime type testing with is.
  • Virtual Classes - Declare virtual (abstract) classes and virtual (abstract) actions, polymorphism, partial abstract classes, concrete implementations.
  • Advanced Features - Objects as agent attributes, passing objects between agents, objects in species, objects as map keys, object equality.

When to use Classes vs Species​

FeatureClassSpecies
Is an agent?NoYes
Has reflex/actions/behaviors?No (methods are plain actions)Yes
Has a population?No (no count or lifecycle)Yes (count, init, lifecycle)
Can be an agent attribute?YesNo (species attributes are values)
Used for?Pure data types, structures, utilitiesSimulated entities in the world

See also​