Elements

An Element is a plain value record – a bundle of fields (and, if you want, methods) that you use as a piece of data. It sits alongside Type and is the answer to a simple question: "do I want a tracked entity, or just some data?"



Declaring an Element

Use Element ... EndElement, with Field declarations just like a Type:

Element Particle
    Field x:Int
    Field y:Int
    Field speed:Int
EndElement

Element names don't need the T prefix that Types use – an Element isn't a Type, so call it whatever reads well (Particle, Point, Item).



Making one

You do not use Create – that's for Types. An Element is a value, so you just declare it and it's ready to use, its fields at their defaults:

Local p:Particle       ' a value, ready to go - no Create
p\x = 3
p\y = 4
p\speed = 10


Methods

Elements can have Methods, exactly like Types, and this\ refers to the element's own fields:

Element Particle
    Field x:Int
    Field y:Int

    Method Show()
        Print "(" & ToString(this\x) & "," & ToString(this\y) & ")"
    EndMethod

    ' Compare lets a Sequence Of Particle be sorted
    ' (return a negative number, zero, or a positive number)
    Method Compare:Int(other:Particle)
        Return (this\x + this\y) - (other\x + other\y)
    EndMethod
EndElement


Value Semantics (the important bit)

An Element is copied whenever it's stored or passed – just like an Int or a String. Two elements never secretly share data:

Local a:Particle
a\x = 5

Local b:Particle
b = a          ' b gets a COPY of a
b\x = 99       ' changing b does NOT change a

Print ToString(a\x)   ' still 5

The big payoff is with Sequences: a Sequence Of Particle owns its elements outright. Adding an element copies it in; removing it (or clearing the list) frees it. No references to get tangled, no cleanup.



No Cleanup Needed

Because an Element is a value, there is nothing to free:



Type vs Element – at a glance
                    Type                          Element
                    ----                          -------
What it is          a reference (a handle)        a value (the data itself)
Make one            Local o:TFoo = Create TFoo    Local e:MyEl        (no Create)
Tracked?            yes - internal instance list  no
Iterate them all    For EachIn TFoo / First/Last  no - keep them in a Sequence
Copying             copies the reference (shared) copies the value
Cleanup             Remove destroys it            none - freed at end of scope
In a Sequence       not allowed                   yes - the natural use
Use it for          managed entities              plain data records
                    (player, boss, NPC)           (bullet, point, item)


When to use which

Key Points

See Also

Examples

See Sequences.bam (the Elements section) and Bullets.bam for Elements in action.


BambooBasic © 2026 Michael Denathorn