Results 1 to 15 of 15

Thread: How to make a Form in a dll and Load it into SCAR

  1. #1
    Join Date
    Jul 2008
    Location
    England
    Posts
    763
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default How to make a Form in a dll and Load it into SCAR

    MOD EDIT: Please note that although this tutorial was designed for SCAR, it still applies to Simba. However, you will need to use Simba's alternative plugin scheme and if you are using Delphi 2009+ (instead of FreePascal/earlier Delphi versions), ensure you pass all string types as ANSI (AnsiString, PAnsiChar, etc). Please refer to the Simba documentation on plugins for more info.

    How to make a Form inside a dll and Load it into SCAR

    The reason i'm making this tutorial is because i think forms are an important part of scripting, and i think you can learn alot from them.

    NOTE. This is my first tutorial and i might have forgotten some stuff


    Requirements:

    Delphi 7 (Maybe other versions i use delphi 7 though ).

    Some Delphi knowledge.

    Small Amount of SCAR Knowledge.



    Lets Begin:

    Note. This is just an example of the stuff you can make i decided to make a calculator.

    First, Open delphi 7, go to File > New > Other, then dll wizard. Now go to File > New > Form, and save it somewhere you will remember.

    Building the Form:

    Make two edit box's to your form and name them CalcTxt and CalcTxt2. Now add five button's to the Form and name them AddBtn, MultBtn, MinusBtn, DivideBtn, AnswerBtn and give them the caption's, +, x, -, / and =

    Now you have your Form ready you can start coding. Go to the code page thing and at the top under var add:
    SCAR Code:
    Answer: Integer;
    (Make sure your on the Form code tab) then go back to the Form and Double click on AddBtn and add the following code:

    SCAR Code:
    var
      Add, Add2: Integer;

    begin
      Add := StrToInt(CalcTxt.Text);
      Add2 := StrToInt(CalcTxt2.Text);
      Answer := Add + Add2;

    Between procedure TForm1.AddBtnClick(Sender: TObject); and end;

    If you do not understand this, please read some beginner or intermediate tutorials. Now back onto the subject, Double click on MultBtn and Add the following code:

    SCAR Code:
    var
      Mult, Mult2: Integer;

    begin
      Mult := StrToInt(CalcTxt.Text);
      Mult2 := StrToInt(CalcTxt2.Text);
      Answer := Mult * Mult2;

    Between procedure TForm1.MultBtnClick(Sender: TObject); and end;

    Then, Double click on MinusBtn and Add:

    SCAR Code:
    var
      Minus, Minus2: Integer;

    begin
      Minus := StrToInt(CalcTxt.Text);
      Minus2 := StrToInt(CalcTxt2.Text);
      Answer := Minus - Minus2;

    Between procedure TForm1.MinusBtnClick(Sender: TObject); and end;

    Then, Double click on DivideBtn and Add:

    SCAR Code:
    var
      Divide, Divide2: Integer;

    begin
      Divide := StrToInt(CalcTxt.Text);
      Divide2 := StrToInt(CalcTxt2.Text);
      Answer := Divide div Divide2;;

    Between procedure TForm1.DivideBtnClick(Sender: TObject); and end;

    And Finally double click on AnswerBtn and Add:

    SCAR Code:
    ShowMessage('Answer is: '+ IntToStr(Answer));

    Between procedure TForm1.AnswerBtnClick(Sender: TObject);
    begin and end;

    That is all the coding for the Form, now we need to call the Form from the dll, so go to the code page, and at the top you should see two tabs, click on the first one (the one with the dll code in it), and add this procedure:

    SCAR Code:
    procedure ShowForm;
    begin
      Form1 := TForm1.Create(nil);
      Form1.ShowModal;
    end;

    This calls the Form with all of its procedures, feel free to mess around with it

    Now add the necessary Scar functions so Scar can load the ShowForm procedure

    SCAR Code:
    function GetFunctionCount(): Integer; stdcall; export;
    begin
      Result := 1;
    end;

    And:

    SCAR Code:
    function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall;
    begin
      case x of
        0:
          begin
            ProcAddr := @ShowForm;
            StrPCopy(ProcDef, 'procedure ShowForm;');
          end;
      else
        x := -1;
      end;
      Result := x;
    end;

    And:

    SCAR Code:
    exports GetFunctionCount;
    exports GetFunctionInfo;

    This exports the function.

    Now save it and go to Project > Build. Now go to the directory where you saved the project and you should see the name of your file .dll put it into the plugins folder in the scar directory. Now go into scar and type:

    SCAR Code:
    ShowForm;

    Press run and you should see your Form(Calculator) and all of its components. Hope this helps.

    Sorry if my grammar is a bit wierd lol.

    Also i may have forgotten some stuff its my first tutorial

    Final code of Form:

    SCAR Code:
    unit TutCalc;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;

    type
      TForm1 = class(TForm)
        CalcTxt: TEdit;
        AnswerBtn: TButton;
        CalcTxt2: TEdit;
        MultBtn: TButton;
        AddBtn: TButton;
        DivideBtn: TButton;
        MinusBtn: TButton;
        procedure AddBtnClick(Sender: TObject);
        procedure MultBtnClick(Sender: TObject);
        procedure MinusBtnClick(Sender: TObject);
        procedure DivideBtnClick(Sender: TObject);
        procedure AnswerBtnClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;
      Answer: Integer;

    implementation

    {$R *.dfm}

    procedure TForm1.AddBtnClick(Sender: TObject);
    var
      Add, Add2: Integer;

    begin
      Add := StrToInt(CalcTxt.Text);
      Add2 := StrToInt(CalcTxt2.Text);
      Answer := Add + Add2;
    end;

    procedure TForm1.MultBtnClick(Sender: TObject);
    var
      Mult, Mult2: Integer;

    begin
      Mult := StrToInt(CalcTxt.Text);
      Mult2 := StrToInt(CalcTxt2.Text);
      Answer := Mult * Mult2;
    end;

    procedure TForm1.MinusBtnClick(Sender: TObject);
    var
      Minus, Minus2: Integer;

    begin
      Minus := StrToInt(CalcTxt.Text);
      Minus2 := StrToInt(CalcTxt2.Text);
      Answer := Minus - Minus2;
    end;

    procedure TForm1.DivideBtnClick(Sender: TObject);
    var
      Divide, Divide2: Integer;

    begin
      Divide := StrToInt(CalcTxt.Text);
      Divide2 := StrToInt(CalcTxt2.Text);
      Answer := Divide div Divide2;
    end;

    procedure TForm1.AnswerBtnClick(Sender: TObject);
    begin
      ShowMessage('Answer is: '+ IntToStr(Answer));
    end;

    end.

    Final code of dll:

    SCAR Code:
    library Tut;

    uses
      SysUtils,
      Classes,
      TutCalc in 'TutCalc.pas' {Form1};

    {$R *.res}

    procedure ShowForm;
    begin
      Form1 := TForm1.Create(nil);
      Form1.ShowModal;
    end;

    function GetFunctionCount(): Integer; stdcall; export;
    begin
      Result := 1;
    end;

    function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall;
    begin
      case x of
        0:
          begin
            ProcAddr := @ShowForm;
            StrPCopy(ProcDef, 'procedure ShowForm;');
          end;
      else
        x := -1;
      end;
      Result := x;
    end;

    exports GetFunctionCount;
    exports GetFunctionInfo;

    begin
    end.

    I'v also included all the dll and project files


    P.S I don't know if this is usefull. I hope it is though



    - Quickmarch
    Last edited by Daniel; 08-10-2012 at 10:18 AM.
    lol

  2. #2
    Join Date
    Jul 2008
    Location
    Poland
    Posts
    375
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Nice, nice, nice. +1
    :P

  3. #3
    Join Date
    Jul 2007
    Location
    Norway.
    Posts
    1,954
    Mentioned
    3 Post(s)
    Quoted
    0 Post(s)

    Default

    I found this really helpful, I'm sitting here with ideas for plugins but I didn't have the extra stuff required for the plugins to work.

    Thank you very much for making this tutorial!
    Rep+!

  4. #4
    Join Date
    Sep 2006
    Location
    include srl/srl.scar ( aussie)
    Posts
    2,877
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks , but how would i use this in a player form,as in reading the userimputted Data.

  5. #5
    Join Date
    Mar 2007
    Posts
    1,699
    Mentioned
    0 Post(s)
    Quoted
    8 Post(s)

    Default

    Thanks, great tutorial for delphi noobs.
    You could add more about how delphi communicates with scar, such as explaining stdcall and export.

  6. #6
    Join Date
    Jul 2008
    Location
    England
    Posts
    763
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by NiCbaZ View Post
    Thanks , but how would i use this in a player form,as in reading the userimputted Data.
    I've been experimenting with this and i think you need to store the player array into a procedure like:
    SCAR Code:
    procedure(var LoadPlayers: TUser);
    with declaring TUser a type then exporting the procedure, although i can't get mine to work :s
    lol

  7. #7
    Join Date
    Mar 2007
    Posts
    1,699
    Mentioned
    0 Post(s)
    Quoted
    8 Post(s)

    Default

    You could try asking Nielsie or Sumilion, they made a form with an embedded SRL playerform for SMM.

  8. #8
    Join Date
    Jul 2008
    Location
    England
    Posts
    763
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Yeh but i like doing things solo i've almost finished it anyway for my goblin shredder
    lol

  9. #9
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    i am planning to script again with scar.
    i was already looking for a guide with .dll plugins for a long time.

    great tut
    We merely play "against the rules". We cheat. Thats what we are good at, thats what we do.
    http://www.frement.net/srl/simba_user4.png

  10. #10
    Join Date
    Sep 2006
    Location
    include srl/srl.scar ( aussie)
    Posts
    2,877
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Hmm, would this work? make a function
    ( in the plugin) that returns the value of a text box?

  11. #11
    Join Date
    Jul 2008
    Location
    England
    Posts
    763
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by NiCbaZ View Post
    Hmm, would this work? make a function
    ( in the plugin) that returns the value of a text box?
    Yeh but i think it would be easier just to transfer the player array into another player array declared by a procedure or function
    lol

  12. #12
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,623
    Mentioned
    4 Post(s)
    Quoted
    0 Post(s)

    Default

    Why would you put the SRL Player form into a .dll? Doesn't really make sense.. Other than the fact it looks fancier in the .dll it doesn't have a purpose.
    Verrekte Koekwous

  13. #13
    Join Date
    Sep 2006
    Location
    include srl/srl.scar ( aussie)
    Posts
    2,877
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by mastaraymond View Post
    Why would you put the SRL Player form into a .dll? Doesn't really make sense.. Other than the fact it looks fancier in the .dll it doesn't have a purpose.
    Erm? It has a big purpose, the purpose is that, i learnt lots and that i can now use delphi only things in my Forms.

    @Quick, could you please pm me or make the tut longer on how to save the players so its useable, i tryed to make a function return the context of one 1 string but when used in Scar, i get a runtime error.

  14. #14
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    7,809
    Mentioned
    4 Post(s)
    Quoted
    3 Post(s)

    Default

    Quote Originally Posted by mastaraymond View Post
    Why would you put the SRL Player form into a .dll? Doesn't really make sense.. Other than the fact it looks fancier in the .dll it doesn't have a purpose.
    You can do many more things with a delphi form than with a scar one.

    Not to mention, its faster as its pre-compiled.

    Anywho, great tut!
    Writing an SRL Member Application | [Updated] Pascal Scripting Statements
    My GitHub

    Progress Report:
    13:46 <@BenLand100> <SourceCode> @BenLand100: what you have just said shows you 
                        have serious physchological problems
    13:46 <@BenLand100> HE GETS IT!
    13:46 <@BenLand100> HE FINALLY GETS IT!!!!1

  15. #15
    Join Date
    Oct 2006
    Location
    On a boat
    Posts
    18
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Great tutorial! I'm going to use it on the script I'm working on now =D

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to Make a Form Without a Title Bar
    By Widget in forum OSR Advanced Scripting Tutorials
    Replies: 18
    Last Post: 07-17-2007, 11:35 PM

Posting Permissions

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