DiscoverLearnDocumentationGet OpenPLXSearch Contact

Inheritance and Traits

We can make our models inherit from other models. By inheriting from another model you can access all of the attributes in the inherited model and whatever model the inherited model inherts from and so on. We can also add traits to our models. Traits define a set of attributes and by adding a trait to a model we can access these attributes.

Inheritance

To make a model inherit from another model, this is done with the is syntax.

Example:

MyModel:
    my_attribute is Real

MyExtendedModel is MyModel:
    my_attribute: 1.0
    my_second_attribute is Math.Vec3: {0, 0, 0}

Here:

  • MyModel is our base model name.
  • MyExtendedModel is a new model extending the base model.

By extending MyModel we now have access to the my_attribute attribute in MyExtendedPart as well as the my_second_attribute attribute.

Another example extending the Physics3D.Bodies.RigidBody to define a rigid body:

BoxBody is Physics3D.Bodies.RigidBody:
    geometry is Physics3D.Geometries.Box:
        size: {1, 1, 1}
    visuals is Visuals.Geometries.Box:
        size: {1, 1, 1}

System is Physics3D.System:
    boxBody is BoxBody
BoxBody is Physics3D.Bodies.RigidBody:
    geometry is Physics3D.Geometries.Box:
        size: {1, 1, 1}
    visuals is Visuals.Geometries.Box:
        size: {1, 1, 1}

System is Physics3D.System:
    boxBody is BoxBody

Traits

Similarily to inheriting from another model, adding traits to a model allows us to use attributes that are not defined in the base model.

To use a trait, we first need to create it. We will create two traits in this example:

trait BoxGeometry
    geometry is Physics3D.Geometries.Box:
        size: Math.Vec3.from_xyz(1, 1, 1)

trait SphereGeometry
    geometry is Physics3D.Geometries.Sphere:
        radius: 1.0

Scene is Physics3D.System:
    box is Physics3D.Bodies.RigidBody with BoxGeometry
    sphere is Physics3D.Bodies.RigidBody with SphereGeometry

Here:

  • BoxGeometry and SphereGeometry are two traits, one defines a box geometry and the other a sphere geoemtry.
  • box is instantiated to be a rigid body, and with the with syntax we give the trait BoxGeometry and its attributes.
  • sphere is instantiated to be a rigid body, with the trait SphereGeometry and its attributes.

When to use inheritance and when to use traits

Traits are useful for injecting attributes into a model without requiring inheritance. When a model inherits from another using is, the inheritance relationship becomes fixed.

For example, if both Car and Truck inherit from Vehicle, it may be tempting to add optional features directly to specialized car or truck models. However, many such features are not universal. A car or a truck might have a Winch or a Bullet, but not all cars or trucks will include them. Traits provide a flexible way to model these optional features. By defining Winch and Bullet as traits, they can be added to any car or truck model regardless of how specialized it already is.

Without traits, you would need to create many variations of the same base model, such as Car, CarWithBullet, CarWithWinch, and CarWithBulletAndWinch. As more optional features are introduced—and the same variations are required for trucks—the number of model combinations quickly grows and becomes difficult to manage.

OpenPLX is a work in progress. This draft version will evolve with user feedback and experience. We welcome your input and collaboration.
X