Methods can only be called from objects that have been created already (eg. r:Rocket = New Rocket).
Functions in types are handy when you're not accessing a specific object (you may not have one yet), or want to create an object. You could use an external function as in previous Blitzes to create and return an object, but that defeats the point of going object-oriented -- you want to wrap it all up into an easily copy-able type:
' This won't work!
Type Rocket
Method Create:Rocket ()
r:Rocket = New Rocket
Return r
End Method
End Type
' You can't do this, because methods can only be called from <i>existing</i>
' objects. Because the object doesn't exist yet, there's no Create method to call:
r:Rocket = Rocket.Create ()
Instead, you use a function embedded in the type definition, and you call it by using the type name, a period, and the function name, as in
MyType.MyFunction ()...
Type Rocket
Function Create:Rocket ()
r:Rocket = New Rocket
Return r
End Function
End Type
' That's better!
r:Rocket = Rocket.Create ()
They're also handy for updating all objects at once, again via a function wrapped up in the type, because a method only works on the calling object (in this case, it would be r:Rocket calling the method and so only r:Rocket would be operated on). Here's an example function for updating all objects in a given list (assume that all 'Oink' objects are added to 'OinkList' when created)...
Global OinkList:TList = CreateList ()
Type Oink
Field x
Function UpdateAll ()
For o:Oink = EachIn OinkList ' Global list...
Print o.x
Next
End Function
' Note that methods can access type fields directly (eg. x),
' whereas functions need to be told which object to access them from (in the above example, o.x)...
End Type
The above would work via a method too, but you'd have to call it from a specific object, and you may not always have an object to call it from (eg. you may have a list of particles where there may not always be a particle on screen to call the UpdateAll method from). You can always call such a function by going:
Oink.UpdateAll