SCRMaps
/
Sign in

object: making a struct

How to group values in a language whose only value is a 32-bit integer - and where a copy is not a copy.

11 min read

One integer value type does not mean no structs: object does that job. It groups fields, carries methods and can have a constructor. Just remember the semicolon after the closing brace.

object Coord { var x, y; };          // 세미콜론에 주의 / mind the semicolon
object Triangle;                     // 먼저 이름만 알리기 / forward declaration
object Triangle {
    var p: Coord * 5;                // Coord 5개짜리 배열 멤버 / a member array of 5
    var q;
    var prev: selftype;              // selftype = 자기 자신의 타입
    function constructor(q) { this.q = q; }
    function add(other: selftype) { this.q += other.q; }
};
selftype means this object's own type - what you use for a structure that refers to itself, such as a linked list.
The forms
Coord()Construct, running the constructor if there is one.
(Coord * 5)()A member array has to be made this way before it is indexed.
Coord.cast(v)Reinterpret a plain variable as this type - needed after something like var e = a; loses the type.
a.copy()A real copy. Plain assignment only makes another name for the same thing.
a.x, a.y = 5, 7;Tuple assignment.
const a, b = List(), List();Two declarations on one line.
if (a)An object converts to a condition: is it non-null.

copy() is shallow. Scalar members are copied; object-valued members are shared. After b = a.copy(), writing b.p[0].x = 5 also makes a.p[0].x 5, while b.q = 2 leaves a.q alone. The official compiler test asserts exactly this.

Objects inherit, too: object B extends A { ... } adds its own fields to A's. It does not sit well with dynamic allocation, though.

alloc() does not work on every object. The official compiler test asserts that calling it on a class with eleven fields - ten from a base the test calls ObjectWithTooManyFields, plus one - is a compile error. The test does not say whether the limit is the field count or the inheritance, so if you plan to allocate a large or derived struct dynamically, try alloc() before the design depends on it.

Several conditions can be bundled with list and used as one: list(c1, c2, c3) is true when all three are. With the ternary operator it picks a value without stacking ifs.

Putting an array inside an object is a recurring question. var a: EUDArray; holds a pointer, so every instance points at the same array. But there is a question to ask first: does it have to be an array?

object Rect      { var x, y, w, h; };
object BetRegion {
    var pos: Rect;
    var betHereUnitEPD;
    var betChecker: EUDFuncPtr(1, 1);
    var description: TrgString;
};
Grouping related values into a struct is almost always better: rect.w reads and a[2] does not.

If you genuinely need a different array per instance, make an array pool: one large buffer plus a precomputed list of free offsets into it.

const objectN = 32768, size = 5;
const buffer  = EUDArray(objectN * size);
const offset  = EUDArray(objectN);
var   remaining = objectN;

function init() {                                  // onPluginStart에서
    for (var i = 0; i < objectN; i += 1) offset[i] = 4 * size * i;
}
function alloc(): EUDArray { remaining -= 1; return buffer + offset[remaining]; }
Because the offsets are precomputed rather than searched for, allocating is one subtraction and one addition. Freeing is the reverse.

When you free the object you have to free its array too. Nothing links the two, so forgetting leaks silently.

Everything has an address