Type typename
Parameters
| typename - the name of the new custom type |
Description
|
Starts the definition of a custom type - a record with named fields, and a list to keep them in. A Type block lists the fields each object will own, and finishes with End Type: Type bullet Field x#,y# Field speed# End Type Types are what you use for "lots of the same thing" - bullets, enemies, particles, pickups, inventory slots. Instead of juggling parallel arrays, each object carries its own copy of every field, and you can create and destroy them freely at runtime without deciding on a maximum in advance. Every type keeps its own list, and every object you make with New is added to the end of it. That list is what makes types so convenient: First and Last get you the ends, After and Before step along it, and For ... = Each walks the lot. Under the hood it is a doubly linked list, so adding and removing objects anywhere in it is cheap, but jumping to "object number 40" is not - types are for iterating, arrays are for indexing. Variables that hold an object are declared with the type name after a dot, as in b.bullet, and fields are read with a backslash: b\x#. Types must be declared in the main program, not inside a function, and a variable holding one needs to be Global if functions are to see it. A debugging trick worth knowing: Str applied to an object prints all its fields comma separated inside square brackets, such as [15,42,"Fluffy"]. See also: Field, End Type, New, Delete, Each, Null. |
Example
; Type Example ; ------------ ; A Type groups related variables - ideal for game objects like enemies Type Enemy Field name$ Field hp End Type ; New creates an object of the Type; \ accesses its Fields e.Enemy=New Enemy e\name="Scout" e\hp=20 e.Enemy=New Enemy e\name="Grunt" e\hp=50 e.Enemy=New Enemy e\name="Brute" e\hp=80 ; Every object made with New joins the Type's collection - loop over it Print "The enemy squad:" For e.Enemy=Each Enemy Print " "+e\name+" has "+e\hp+" hp" Next Print "" Print "Press any key to close the example" WaitKey End
Index