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:

  1. 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

  2. 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
    

  3. Do not use the this pointer outside a object's Method.

Exercise:

  1. Which of the following Method Declarations follows AOBP and Label Convention Guidelines:
    1. Function BombDrop(this.alien,bombtype)
    2. Function alienBombDrop(this.alien,alienbombtype)
    3. Function alienDropBomb(a.alien,bomb)
    4. Function alienDropBomb(alienbomb,this.alien)
  2. Find 4 errors in the following Method Declaration:
    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
    

  3. What is the third rule of using the this pointer?

Email-in this Exercise for Grade!