Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Linking a procedure to an object for remote execution

  1. #1
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Linking a procedure to an object for remote execution

    Hey guys... I'm working on a tiny gui unit for use in my game projects. One of the things I'd like it to do is associate external procedures and execute them if assigned for say a button click.

    Assigning them would be as simple as

    [pascal]MainMenu.Button1.OnClick := YourProcedure;[/pascal]

    YourProcedure() would be created by the user of the library. And Button1 would be a control object that resides inside the window object (MainMenu).

    [pascal]procedure YourProcedure;
    begin
    // whatever
    end;[/pascal]

    my code to detect mouse clicks on the button object would run the 'linked' procedure from it's own code. How can I do this? Can I simply use pointers?
    Jason McMillen
    Pascal Game Development
    Co-Founder





  2. #2

    Linking a procedure to an object for remote execution

    It's pretty easy in FreePascal. I don't know how you would do it in Delphi though..

    FPC:
    [pascal]procedure YourProcedure; [public, alias: 'YourProcedure'];
    begin
    //Do something. This procedure is in no way visible to the procedure using it
    end;[/pascal]

    [pascal]//In your code
    procedure YourProcedureDecl; external name 'YourProcedure';

    ...
    begin
    MainMenu.Button1.OnClick := @YourProcedure;
    end;[/pascal]

    Is this what you were asking?
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  3. #3
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Linking a procedure to an object for remote execution

    Hmm... not sure. Have you ever used JEDI-SDL's audio mixer?

    You pass a procedure to this external procedure

    [pascal]procedure Mix_HookMusicFinished( music_finished : Pointer );[/pascal]

    and whatever procedure you passed to music_finished will automatically execute when the song ends. all I have to do is add this line of code for my own written procedure.

    [pascal]Mix_HookMusicFinished(@RestartMusic);[/pascal]

    RestartMusic is a procedure of my own making.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  4. #4

    Linking a procedure to an object for remote execution

    I once tried this:
    [pascal]
    type
    TMyEvent = procedure;

    // In DLL
    procedure MyDLLFuncIdle(IdleFunc: TMyEvent);
    begin
    FIdleFunc := IdleFunc;
    end;

    // In External App
    procedure MyDLLFuncIdle(IdleFunc: TMyEvent); external 'MyDLL.dll';

    procedure xxxIdleFunc;
    begin
    // do some shizzle here
    end;

    begin
    MyDLLFuncIdle(xxxIdleFunc);
    end;
    [/pascal]

    And it worked fine for me.

  5. #5
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Linking a procedure to an object for remote execution

    Hmm... well what is FIdleFunc though?

    I've just found something on Delphi's ExecMethod but nothing seems to exist like it for FPC. :?

    Something like ExecProcedure(Func: pointer); would be great. :lol:
    Jason McMillen
    Pascal Game Development
    Co-Founder





  6. #6

    Linking a procedure to an object for remote execution

    Events in normal TButton etc are very simple.

    [pascal]type
    TNotifyEvent = procedure(Sender: TObject) of object;

    // .. and then it is put as a variable
    FOnClick: TNotifyEvent;
    // .. and as a property if want to make a component event of it
    property OnClick: TNotifyEvent read FOnClick write FOnClick;
    [/pascal]

    TNotifyEvent = procedure(Sender: TObject) of object;
    Here " of object" tells that the procedure must be class procedure, not a standalone procedure. Those are defined like:
    TNotifyEventLonely = procedure(Sender: TObject);

    You can manually make a procedure and very easily set it (as long as the parameters match the defined type):
    FOnClick:=YourClickProcedure;

    And in your class you can then call it like
    if assigned(FOnClick) then FOnClick(self)

  7. #7

    Linking a procedure to an object for remote execution

    Quote Originally Posted by WILL
    Hmm... well what is FIdleFunc though?
    Let's assume you have a procedure inside your DLL, that does certain things with the variable FIdleFunc.

    A little example
    [pascal]
    // -- GLOBAL --
    type
    TMyMathEvent = procedure(Z: Single);

    // -- DLL --
    var
    FMathFunc: TMyEvent;


    procedure DLLDoIt;
    var
    x: Single;
    begin
    x := 3.14;

    if Assigned(FMathFunc) then
    FMathFunc(X);
    end;

    procedure AssignMathFunc(TheMathFunc: TMyMathEvent);
    begin
    FMathFunc := TheMathFunc;
    end;

    // -- EXTERNAL APP --
    procedure AssignMathFunc(TheMathFunc: TMyMathEvent); external 'MathDLL.dll';

    const
    Radius = 3.225;

    var
    TheArea: Single;

    procedure CalculateCircleArea(Z: Single);
    begin
    TheArea := Z * (Radius * Radius);
    end;

    begin
    AssignMathFunc(CalculateCircleArea);
    end;
    [/pascal]

  8. #8

    Linking a procedure to an object for remote execution

    Will, could you try to explain a little better what you want to do?

    There's a difference between calling methods and calling normal functions/procedures.

    If you want to handle non-object procedures/functions you simply pass either a pointer or a type of procedure/function:

    [pascal]
    type
    TProcedure = procedure; //Already declared in system

    ..
    procedure SomethingLength(callBack: TProcedure);
    begin
    ...
    Callback; //Calls callback
    ...
    end;[/pascal]

    If you have a pointer you would like to call you simply typecast it to TProcedure and call it

    [pascal]
    TProcedure(funcPointer);
    [/pascal]

    As long as you are using fastcall or stdcall you are safe using this approach
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  9. #9

    Linking a procedure to an object for remote execution

    I think he wants to set procedures for events of objects he create during the runtime of his game/program

    like:
    [pascal]
    objname:=TLabel.Create(self);
    objname.SetOnClick:=Procedurename;
    [/pascal]

    I don't think this would work(I know it won't work).
    but I think that this is what he wants

  10. #10

    Linking a procedure to an object for remote execution

    I think he wants to set procedures for events of objects he create during the runtime of his game/program

    like:
    [pascal]
    objname:=TLabel.Create(self);
    objname.SetOnClick:=Procedurename;
    [/pascal]

    I don't think this would work(I know it won't work).
    but I think that this is what he wants

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •