Page 1 of 1

Calling procedure from variable passed to another procedure?

Posted: Tue Dec 04, 2007 2:08 am
by mrjiles
I am trying to call a procedure from within a procedure but the one I'm trying to call is not known (a variable procedure call?). Any ideas? Maybe there's something I'm missing...


Code: Select all

Proc1("Proc2")

Procedure Proc1(WhatToCall$)
     WhatToCall$()    ; I know this won't work but it shows what I'm trying to do :-)
EndProcedure

Procedure Proc2()
     ; code....
EndProcedure

I did look through the manual but I don't know what this would be called. Thanks in advance!

Posted: Tue Dec 04, 2007 2:15 am
by Dare
Would using CallFunctionFast(..) be useful?

Code: Select all

Procedure one()
  ;yada
EndProcedure
Procedure two()
  ;yada
EndProcedure
Procedure tre()
  ;yada
EndProcedure
Procedure etc()
  ;yada
EndProcedure

Procedure JumpAround(*par.l)
  ;yada
  CallFunctionFast(*par)
EndProcedure

JumpAround(@one())   ; Get proc addy
JumpAround(@two())

; alternatively, a jump table

jmpAddy(1)=@one   ; Get proc addy
jmpAddy(2)=@two

;etc, then

Procedure JumpAroundABit(num.l)
  ;yada
  CallFunctionFast(jmpAddy(num))
EndProcedure

JumpAroundABit(1)
JumpAroundABit(2)
Be aware though that if you pass parameters then I think that all procedures will need the same number and type of parameters.

Also that code above is eg only, may have typos.

Posted: Tue Dec 04, 2007 2:16 am
by netmaestro
Probably CallFunctionFast, based on what you've said so far. Pass the pointer to the proc using @Proc() and the receiver does a CallFunctionFast with that as parameter.

[Edit] Hey! You.. You.. Usurper!!

Posted: Tue Dec 04, 2007 2:17 am
by Dare
:lol:

There is a first! And only! And probably last ...


Edit - and there are typos in above, so beware. Spotted and edited some, spotted others .... not fixed

Re: Calling procedure from variable passed to another proced

Posted: Tue Dec 04, 2007 4:17 am
by PurePWNRER
mrjiles wrote:I am trying to call a procedure from within a procedure but the one I'm trying to call is not known (a variable procedure call?). Any ideas? Maybe there's something I'm missing...


Code: Select all

Proc1("Proc2")

Procedure Proc1(WhatToCall$)
     WhatToCall$()    ; I know this won't work but it shows what I'm trying to do :-)
EndProcedure

Procedure Proc2()
     ; code....
EndProcedure

I did look through the manual but I don't know what this would be called. Thanks in advance!
First, open your window and throw blitzbasic to the street.
Secondly,

Code: Select all

Declare.l Proc1( lwPtr.l )
Declare.l Proc2()

Proc1( @Proc2() )

Procedure.l Proc1( lwPtr.l )
  If lwPtr
    CallFunctionFast(lwPtr)
  EndIf
EndProcedure

Procedure.l Proc2()
  MessageRequester("hi","hello")
EndProcedure

Posted: Wed Dec 05, 2007 11:01 pm
by mrjiles
Thanks to all who replied!

Posted: Wed Dec 05, 2007 11:39 pm
by Hroudtwolf
Hi,

I recommend Prototypes.

Code: Select all

Prototype.l pCalling (sString.s)


Procedure.l Proc(*Call.pCalling)
  If *Call
    *Call ("Hello World")
  EndIf
EndProcedure

Procedure.l Proc2Call (sString.s)
  MessageRequester("Test" , sString)
EndProcedure 


Define *test = @Proc2Call ()


Proc (*test)
It's more professional.

Best regards

Wolf