Pointers to Functions

BlitzMax Forums/BlitzMax Beginners Area/Pointers to Functions

Shortwind(Posted 2009) [#1]
Ok, I'm stupid. How do I get the address of a function in my BMax program?

Or is this not possible in this version of BMax?

:<


Gabriel(Posted 2009) [#2]
Global SomeFunction:Int(A:String)=MyFunction
Function MyFunction:Int(A:String)
   Return A.ToInt()
End Function

Print SomeFunction("911")



Shortwind(Posted 2009) [#3]
Maybe I don't quite understand, here is what I'm trying to do:

Fake Code:
Initialize SomeArray array or list

SomeArray(0)=FirstFunctionAddress
SomeArray(1)=SecondFunctionAddress
SomeArray(2)=ThirdFunctionAddress
SomeArray(....etc....)=OneThousandthFunctionAddress
Repeat
  J=Random(0..somenumber)
  call SomeArray(J)
Until some_set_condition


:)

This is "kinda" like what I want to do. I assume this could be put into a TList instead of an array....

So, instead of having a thousand select case, or If..else statements, I have minimal coding.... ie: faster, easier to maintain, easier to extend.

These functions, of this type and for what I want to do, don't need any input, and don't need to return anything.

Or am I just being stupid... I guess it could be equated to the OLD days of "ON A GOSUB 100,200,300,400,500,600...."

:)


TaskMaster(Posted 2009) [#4]
'Create the array
Global ArrayOfFunctions()[]
'Increase its size by one.
ArrayOfFunctions = ArrayOfFunctions[..ArrayOfFunctions.Length + 1]
'Add the function to the array
ArrayOfFunctions[ArrayOfFunctions.Length - 1] = SomeFunction

'The function that got added.
Function SomeFunction()
 'Code here
End Function

'To call the function
ArrayOfFunctions[0]()




Shortwind(Posted 2009) [#5]
Thanks!

Who would have known that adding the stupid "()" would fix my whole problem! Ha hah aha ah....

Duh! LOL


"You know, shortcuts are nice, but make for confusion and are just EVIL......."
:)

One question: What type does the "Global blah()[]" get assigned too? Int??? Just curious....


TaskMaster(Posted 2009) [#6]
They are function pointers. Size is probably Int I guess, I am not sure.

But their type is not Int.