Assigning Functions?
BlitzMax Forums/BlitzMax Programming/Assigning Functions?
| ||
| Is there an efficient way to "assign" functions to an object? I was thinking about function pointers though the issue is each function i would like to assign has a different number of parameters, |
| ||
| Make the functions a base class/type/object, and then inherit this by all the objects that need these functions assigned |
| ||
| Good idea, guess i had just had a brain fart |
| ||
Type TPlayer
Method Update()
...
End Method
Method Draw(x, y)
...
End Method
Function Create:TPlayer(x, y, lives)
...
End Function
End TypeLike this? |
| ||
Type TBaseClass Function Blah() Print "This function is necessarily unnecessary." End Function Function Blah2() Print "Do you know how many friends I make with this function?" End Function End Type Type TPlayer Field BaseClass:TBaseClass End Type Local aBaseClass:TBaseClass = New TBaseClass Local aPlayer:TPlayer = New TPlayer aPlayer.BaseClass = aBaseClass aPlayer.BaseClass.Blah() aPlayer.BaseClass.Blah2() |
| ||
Type TBaseClass
Method Blah()
Print "This function is necessarily unnecessary."
End Method
Method Blah2()
Print "Do you know how many friends I make with this function?"
End Method
End Type
Type TPlayer Extends TBaseClass
Method UniqueToPlayer()
Print "I'm a player!"
End Method
Method Blah2()
Print "You can replace functions too"
End Method
End Type
Local aPlayer:TPlayer = New TPlayer
aPlayer.UniqueToPlayer()
aPlayer.Blah()
aPlayer.Blah2()
|
| ||
Print "Cool!" compile this to reveal a hidden message! |
| ||
| The coolest part is you can have several types, such as players, enemies, even pick ups, extending the same base class which has methods such as Update and Draw. You can put code unique to that type in its own type, but common code to all types can go in the base class. The even cooler feature is, if you add all these players, enemies etc to the same TList, you can use For Local base:TBaseClass = Eachin MyList
base.Update()
Next for example,and this will call the Update method of everything in that list which extends TBaseClass! So you can update and draw all your typical game objects from just a single procedure. |