Page 1 of 1

How to check if procedure exists and call it during runtime?

Posted: Fri Jan 19, 2018 3:06 pm
by Kukulkan
Hi,

I wonder if it is possible to check during runtime, if a function with a given name exists and how to call it then?

Pseudo-Code example where I named a conceived function FindFunctionRuntime() for showing:

Code: Select all

Procedure test1()
  MessageRequester("Test 1", "Just a test", #PB_MessageRequester_Ok)
EndProcedure

*pointer = FindFunctionRuntime("test1"); will find something :)
If *pointer <> 0
    CallFunctionFast(*pointer)
EndIf

*pointer = FindFunctionRuntime("test2"); will not find something :(
If *pointer <> 0
    CallFunctionFast(*pointer)
EndIf
Is such possible?

Re: How to check if procedure exists and call it during runt

Posted: Fri Jan 19, 2018 3:12 pm
by wilbert

Re: How to check if procedure exists and call it during runt

Posted: Fri Jan 19, 2018 3:28 pm
by kenmo

Code: Select all

Runtime Procedure test1()
  MessageRequester("Test 1", "Just a test", #PB_MessageRequester_Ok)
EndProcedure

*pointer = GetRuntimeInteger("test1()"); will find something :)
If *pointer <> 0
    CallFunctionFast(*pointer)
EndIf

*pointer = GetRuntimeInteger("test2()"); will not find something :(
If *pointer <> 0
    CallFunctionFast(*pointer)
EndIf

Re: How to check if procedure exists and call it during runt

Posted: Fri Jan 19, 2018 3:30 pm
by Fred
Best is to use a real function pointer instead of CallFunctionFast() declared with Prototype

Re: How to check if procedure exists and call it during runt

Posted: Fri Jan 19, 2018 4:19 pm
by Kukulkan
Thanks, I was not aware of these functions! Great :-)

Re: How to check if procedure exists and call it during runt

Posted: Sun Jan 28, 2018 6:54 pm
by Joubarbe
Prototypes can have parameters and return value:

Code: Select all

Runtime Procedure test(param.i)
	MessageRequester("Test 1", "Just a test, with a parameter: " + param, #PB_MessageRequester_Ok)
	ProcedureReturn 2
EndProcedure

*pointer = GetRuntimeInteger("test()")
Prototype prot(param.i)
Define foo.prot = *pointer
result = foo(1)
Debug result