05: OBP Basics Part II: The this Pointer
"Many Object-Oriented Languages contain a special pointer that is called this."
In this lesson you will learn how to use the mysterious this pointer. The this Pointer is used within an object's Method to represent the current object that is being manipulated. In most OOP Languages the this pointer is implicit (in other words, you dont have to type it or see it). With OBP in Blitz3D we have to explicitly designate and assign the pointer.
There a few rules we adhere to that make use of the this pointer clear and effecient:
- All methods that operate on a instance are passed a object of its own as the first parameter using the this pointer.
Function alienDelete(this.alien) Delete this End Function
- The this pointer can be used in a For... Each construct were no this pointer is passed and operation on all object instances is performed.
Function alienUpdate() For this.alien = Each alien ;update this Next End Function
- Do not use the this pointer outside a object's Method.
Function CollideBomb.this(b.bomb,player) b\y=b\y-1 If b\y = player\y Then If b\x > player\x-5 And b\x < player\x+5 Return True EndIf EndIf End Function
Email-in this Exercise for Grade!