Function Pointers

BlitzMax Forums/BlitzMax Programming/Function Pointers

Drey(Posted 2005) [#1]
How do you set up a function pointer then, just to be sure, how do you call the function using the pointer.

And you can do this to a function inside a type as well right?


marksibly(Posted 2005) [#2]
Function Test1()
	Print "Test1!"
End Function

Type TSomeType
	Function Test2()
		Print "Test2!"
	End Function
End Type

Local func()	'our function pointer (could global,field...)

func=Test1	'set function pointer
func			'call function

func=TSomeType.Test2
func



HCow33(Posted 2005) [#3]
Function PrintThis:Object()
Print "This"
End Function


Function RunThis(obj:Object())
obj()
End Function


RunThis(PrintThis)


Perturbatio(Posted 2005) [#4]
Type TmyType
	Field Action(Sender:TmyType)
	Field X:Int
	Field Y:Int
	
	Function Create:TmyType(X:Int, Y:Int, Action(Sender:TmyType))
		Local tempType : TmyType = New TmyType
			tempType.X = X
			tempType.Y = Y
			tempType.Action = Action
		Return tempType	
	End Function
End Type


Global t:TmyType = TmyType.Create(10, 20, Draw)'note the lack of brackets after Draw

Function Draw(Sender:TmyType)
	DrawRect(Sender.X, Sender.X, 100, 100)
End Function

Graphics 640,480,0,0

While Not KeyDown(KEY_ESCAPE)
	Cls
	
	t.Action(t)
	
	Flip
Wend
End


*EDIT*

took too long :)


Drey(Posted 2005) [#5]
excellent, thanks guys


Drey(Posted 2005) [#6]
alright, i'm pretty pumped up. Got them on an array now. Question though, when i have agruments for the function, does the pointer function have to have the same number of agruments?


Function Agru2( X, Y)

Function Agru1(X )

Function Agru0()

Func()[3] 

Func[0] = Agru0
Func[1] = Agru1
Func[2] = Agru2

func[0]
func[1](1)
func[2](1,2)



Not the exact code, but enough to get the idea? I know this doesn't work because i tried it, but idea is there.


Perturbatio(Posted 2005) [#7]
does the pointer function have to have the same number of agruments?


yes.


Drey(Posted 2005) [#8]
ok, well, i guess all you need is either an agrument type or pointers with an array or something. It'll be nice if it could, but i'll work around it.


Grey Alien(Posted 2005) [#9]
neat, easy too.