Function Reference: Difference between revisions
Jump to navigation
Jump to search
→Function Header
imported>Ereksen mNo edit summary |
imported>Lofgren |
||
| Line 22: | Line 22: | ||
|- | |- | ||
|style="border-style: solid; border-width: 0"|[[Image:InDepth.jpg|48px]] | |style="border-style: solid; border-width: 0"|[[Image:InDepth.jpg|48px]] | ||
|style="border-style: solid; border-width: 0"| | |style="border-style: solid; border-width: 0"| | ||
<br> | |||
The "self" variable is a natural consequence of object oriented programming. Object oriented programming relies on the concept of an abstract ''class'' and an ''instance'' of that class (An instance may also be called an ''object''.) | |||
The relationship between a class and an instance is similar to the difference between a blueprint of a car and the actual car that is made from that blueprint. A single blueprint may be the basis for many cars. The blueprint is an abstraction. You cannot interact act with it. For example, you can't turn on the radio of the car in the blueprint. It's a piece of paper. In order to turn on the radio, you need to interact with the actual car. The blueprint is the class, and the car is the object. The way you find out which object is being acted upon by a function is to refer to the self. For example, ''self.TurnOn(theRadio)'' will turn on the radio of the car that calls that function, and only that one car. | |||
All functions are actually resolved by the compiler to use the '''this/self''' pointer. If a script changes an integer variable '''someInt''' to 5, what it looks like to us is: '''someInt = 5'''. Internally, that variable is labeled with, and considered a component of, the object that calls it, which could be written as '''self.something = 5''' | |||
A simple example: imagine a function invoked on an object that takes a parameter that points to another object of the same class: '''car1.Ram(car2)''' (which should ram your car into another car). Obviously, the car cannot ram itself. By using the "Self" variable, we can ensure that car2 is not equal to Self and prevent damaging or unnecessary code from executing: | |||
<br> | |||
<source lang="papyrus"> | |||
;The function is called on one car like this. | |||
Car1.Ram(car2) | |||
;Then we write the function: | |||
function Ram(object someVehicle) | |||
;if someVehicle is the same as the car that the function was initially called on, kill the function and do not proceed to ram it. | |||
if(someVehicle == self) | |||
return | |||
else | |||
self.Moveto(someVehicle) | |||
endIf | |||
endFunction | |||
</source> | |||
<br> | |||
Another example is a script that is trying to release an object it no longer needs so that the reference no longer consumes memory. However due to the its complexities, the script may accidentally end up referencing the same object. This is a dangling pointer, a reference to an object that was expected to be released. By comparing newObject to Self (''if(newObject != Self)''), we can be sure that when the old object is released it will be properly replaced by newObject. | |||
|} | |} | ||