Blitz3D+ Language Reference

Type Initialisation (Extended mode)

Extended mode lets a Type declare compile-time field defaults and one lightweight constructor.

Scalar fields may have defaults:

Type Player
    Field name$ = "Unnamed"
    Field health = 100
    Field speed# = 3.5
    Field target:Enemy = Null
End Type

A default must be a compile-time Integer, Float, or String expression. Global Const values and qualified Enum members may be used. A custom-Type field may explicitly default only to Null. Whole-vector defaults and Function, command, Method, New, Self, field, or runtime-variable expressions are not allowed. Every new object receives its own defaults in declaration order.

Declare one constructor as Method New:

Type Player
    Field name$ = "Unnamed"
    Field health = 100

    Method New(name$, health=100)
        Self.name = name
        Self.health = health
    End Method
End Type

player:Player = New Player("Ranger",150)

Constructor parameters and constant default arguments behave like Method parameters. Argument expressions are evaluated left to right before the object is allocated. Field defaults are then applied before the first constructor statement.

New Player is equivalent to New Player() when a constructor's parameters all have defaults. A required parameter must always be supplied. Parenthesized New Type() is also legal for Types without a constructor.

A constructor may use bare Return for an early successful exit, but cannot return a value or Delete Self. It is invoked only through New; an existing object cannot call object.New(). Construction adds no destructor or automatic ownership: explicitly release resources and Delete objects.

See the compilable initialization example.