02: Custom Types Advanced
"The Custom Type is the most powerful feature in Blitz3D Language."
The Custom Type features Dynamic Memory Allocation, Pointer Referencing , and Built-in Double Linked List Management. In this lesson you will learn various ways to access and assign data to Custom Type Variables, Arrays, and Pointers.
The New keyword creates a instance of a Custom Type object which is automatically inserted at the front of the collection. Unlike Globals, Arrays, and Variables you can create new instances during the program's run-time without reserving the memory. This is known as Dynamic Memory Allocation.
When you declare a variable of a Custom Type to store a new instance (ie: myalien.alien = New alien), you are in fact creating a Pointer to the object.
Custom Types Double Linked List Management allow you to use following keywords: Insert, Before, After, First, Last to manipulate the order of objects in the collection sequentially.
Insert myalien Before Last alien
Think Objects!
To enforce the 'object' mindset, I will refer to Custom Types as Objects From this point forward.
Object Definition
Type alien Field id% Field x# Field y# End Type
Returning an Object Pointer from a Method
Function alienNew.alien() this.alien=New alien alien_Index=alien_Index+1 ;global counter ;default values this\id=alien_Index this\x#=20.0 this\y#=10.0 Return this End Function myalien.alien = alienNew()
Passing an Object Pointer to a Method
Function alienDelete(this.alien) Delete this End Function alienDelete(myalien)
Array of Object Pointers
An Array of Object Pointers can be used to randomly access specific object instances.
Dim alien_ID.alien(ALIEN_MAXCOUNT%) alien_ID(1)=alienNew() alien_ID(1)\x#=1.0 alienposx = alien_ID(1)\x# myalien.alien = alien_ID(1) ;Alternative Blitz Array Global alien_ID.alien[ALIEN_MAXCOUNT%] alien_ID[1]=alienNew() alien_ID[1]\x#=1.0 alienposx = alien_ID[1]\x# myalien.alien = alien_ID[1]
Array as a Property
Arrays as a Property can be only one dimension and the number of elements must specified with a constant or value, no variables.
Type alienMotherShip Field alienstackpointer Field alienstack[ALIEN_MAXCOUNT%] End Type Function alienmothershipChildPush(this.alienmothership,alienchildID) this\alienstack[this\alienstackpointer]=alienchildID this\alienstackpointer=this\alienstackpointer+1 End Function Function alienmothershipChildPop(this.alienmothership) this\alienstackpointer=this\alienstackpointer-1 Return this\alienstack[this\alienstackpointer] End Function
Object Pointer as a Property
Object Pointers are null upon creation of the object instance. You have to devise a method of assignment.
Type martian Field alien.alien Field antennas End Type martian1.martian = New martian martian1\alien.alien = alienNew() martian1\alien\id=1
Array of Object Pointers as a Property
Type martianMotherShip Field alienstackpointer Field alienstack.martian[ALIEN_MAXCOUNT%] End Type Function martianmothershipChildPush(this.alienmothership,martian.martian) this\alienstack[this\alienstackpointer]=martian this\alienstackpointer=this\alienstackpointer+1 End Function Function martianmothershipChildPop.martian(this.alienmothership) this\alienstackpointer=this\alienstackpointer-1 Return this\alienstack[this\alienstackpointer] End Function
Object and Handle Keywords
These two unique Keywords are specifically used with Custom Types. Although these commands are not documented, many Blitz3D Programmers who are aware of them, rely on them to provide random access to individual object instances. I speculate that Blitz3D has its own internal ID Assignment System for objects. The Handle Keyword returns the object's ID. If the ID is assigned to a variable, the Object Keyword will return the Object Pointer from the variable.
handlevar% = Handle(myalien.alien) myalien.alien = Object.alien(handlevar%)
Exercise: