SRL Archive - OSR Advanced Scripting Tutorials

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


Quickmarch

2008-12-24 16:23:49

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:
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:

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:

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:

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:

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:

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:

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

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


And:

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:

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:

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:

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:

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 :)

Iroki

2008-12-24 16:45:52

Nice, nice, nice. +1

EvilChicken!

2009-01-16 11:57:40

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+!

NiCbaZ

2009-01-18 18:30:34

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

lordsaturn

2009-01-18 22:54:02

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

Quickmarch

2009-01-19 02:33:13

From: NiCbaZ
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:
procedure(var LoadPlayers: TUser);
with declaring TUser a type then exporting the procedure, although i can't get mine to work :s

lordsaturn

2009-01-19 02:47:37

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

Quickmarch

2009-01-19 02:54:36

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

nielskool

2009-01-19 15:47:59

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

great tut

NiCbaZ

2009-01-20 18:23:56

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

Quickmarch

2009-01-21 02:22:19

From: NiCbaZ
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 ;)

mastaraymond

2009-01-21 07:21:52

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.

NiCbaZ

2009-01-21 16:57:22

From: mastaraymond
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.

Nava2

2009-03-06 12:09:57

From: mastaraymond
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! :)

Laser

2009-03-29 17:08:19

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