Version: 1.0 Made By: Tharoux Date: September 4th, 2008 Revision: September 4th, 2008 Goal: Creating a simple GUI with a textbox and a button displaying "Hello World !" Even if most of us know SCAR for beeing able to play a game without fiddling with a GUI, it is possible to create one just like in every other language. There's a built-in function to design a form but it's only a small part of the process and you won't go anywhere with just that. Personally, I like to create my form by coding it, thus doing it the hard way, but I think it's important to understand the process before even trying the built-in function. So, let's start. ->Chapter One: Variable Declarations ->Chapter Two: Form Initialization ->Chapter Three: Displaying The Form ->Chapter Four: Adding Objects ->Chapter Five: Adding Code To The Button ->Chapter Six: Nice To Have... ->Script Example ->The End Chapter One: Variable Declarations Like all other variables in SCAR, all fields including the form itself must be declared at the beginning of the script using the syntax "Variable Name : Object Name". Here's a small list of the most common objects used in a GUI: -TForm : For the form itself -TLabel -TEdit -TButton -TCheckBox : -TRadioButton -TListBox -TComboBox If we get back to our goal, we need to create a script with a textbox and a button, so we will use 3 objects and declare them as follow: Code (Text): Program UsingForm; Var frmDesign : TForm; //The form itself txtMessage : TEdit; //The textbox cmdDisplayMessage : TButton; //The button Chapter Two: Form Initialization Now that we have declared all the variables we need in our script, it's time to attack the form itself. For now, we will create an empty form and take a look at the different properties that we can use to customize it. Later, we will take care of putting extra stuffs in there. Everything GUI related must be put into a procedure that we will call InitForm. The first line of the procedure is always the same and tell the variable witch contain the form to create itself. Code (Text): frmDesign := CreateForm; Everything found after this line and beginning with the name of the form is properties that we will customize to make the GUI look like we want it to look. I won't be showing ALL the properties that we can use, because I find this useless. We will only code the needed one. The example below will also explain what each property do. Code (Text): Procedure InitForm; Begin frmDesign := CreateForm; //Create the form frmDesign.Left := 500; //How many pixels from the left of the screen frmDesign.Top := 100; //How many pixels from the top of the screen frmDesign.Width := 350; //The width of the window frmDesign.Height := 250; //The height of the window frmDesign.Caption := 'Hello World !'; //The title of the window frmDesign.Visible := False; //Need to be "false" if we want to call the form within a thread End; Chapter Three: Displaying The Form We have a plain form, but for now, we can't see it. It's a shame ! Now, it's time to do some magic and make it appear. The example I'm going to show you is probably not the only way to show the form but is the one used by all the coder and the one explained in the help section of scar. We will use threading and the best thing off all, we won't need to handle the exception and error return. Yippe !!!! Again, all this coding is contained inside procedures. It's easier because we will need to call those using the thread call function. This will disable the SCAR window but will prevent your form from freezing while waiting for input or processing data. First, the form initialization with the thread Code (Text): Procedure SafeInitForm; Var v: TVariantArray; Begin SetArrayLength(V, 0); ThreadSafeCall('InitForm', v); //Important part: call the InitForm (The GUI) End; Now, our GUI exist, but it's still not showing anywhere. We have to tell the GUI to appear. Code (Text): Procedure ShowFormModal; Begin frmDesign.ShowModal; //Make the GUI visible End; But wait ! I don't see any threading function in there ! Code (Text): Procedure SafeShowFormModal; Var v: TVariantArray; Begin SetArrayLength(V, 0); ThreadSafeCall('ShowFormModal', v); //Important part: make the GUI visible using ShowFormModal End; Ok, everything is ready to use the form but it won't show just yet. We need to call the 2 threading function in our "Main function" or else all this nice code will just sit there waiting to work. Code (Text): Begin SafeInitForm; //Initialize the form using the threading function SafeShowFormModal; //Make the form visible using the threading function End. For those who weren't paying attention, here's what your script should look right now: Code (Text): Program UsingForm; Var frmDesign : TForm; //The form itself txtMessage : TEdit; //The textbox cmdDisplayMessage : TButton; //The button Procedure InitForm; Begin frmDesign := CreateForm; //Create the form frmDesign.Left := 500; //How many pixels from the left of the screen frmDesign.Top := 100; //How many pixels from the top of the screen frmDesign.Width := 350; //The width of the window frmDesign.Height := 250; //The height of the window frmDesign.Caption := 'Hello World !'; //The title of the window frmDesign.Visible := False; //Need to be "false" if we want to call the form within a thread End; Procedure SafeInitForm; Var v: TVariantArray; Begin SetArrayLength(V, 0); ThreadSafeCall('InitForm', v); //Important part: call the InitForm (your GUI) End; Procedure ShowFormModal; Begin frmDesign.ShowModal; //Make the GUI visible End; Procedure SafeShowFormModal; Var v: TVariantArray; Begin SetArrayLength(V, 0); ThreadSafeCall('ShowFormModal', v); //Important part: make the GUI visible using ShowFormModal End; Begin SafeInitForm; //Initialize the form using the threading function SafeShowFormModal; //Make the form visible using the threading function End. Chapter Four: Adding Objects Time to test the script ! Click "Run Script" and you now have a nice GUI showing. Quite plain, isn't it ? Let's add some stuff in there ! First thing to know is that everytime we start working with a new object that will go on the form, we must tell the script to create it like this: Code (Text): "Variable Name := Object Name".Create(variable name of the form) Again, all code after this one starting with the object name is properties witch we will modify to our taste and must be inside the InitForm like the form. We will start with the textbox: Code (Text): txtMessage := TEdit.Create(frmDesign); //Create the textbox txtMessage.Parent := frmDesign; //Add the textbox to the form txtMessage.Left := 100; //How many pixels from the left of the form txtMessage.Top := 50; //How many pixels from the top of the form txtMessage.Width := 150; //Lenght of the textbox txtMessage.Height := 25; //Height of the textbox And the button: Code (Text): cmdDisplayMessage := TButton.Create(frmDesign); //Create the button cmdDisplayMessage.Parent := frmDesign; //Add the button to the form cmdDisplayMessage.Left := 125; //How many pixels from the left of the form cmdDisplayMessage.Top := 150; //How many pixels from the top of the form cmdDisplayMessage.Width := 100; //Width of the button cmdDisplayMessage.Height := 25; //Height of the button cmdDisplayMessage.Caption := 'GO !'; //What's on the label of the button After all those steps, our script should look like this: Code (Text): Program UsingForm; Var frmDesign : TForm; //The form itself txtMessage : TEdit; //The textbox cmdDisplayMessage : TButton; //The button Procedure InitForm; Begin frmDesign := CreateForm; //Create the form frmDesign.Left := 500; //How many pixels from the left of the screen frmDesign.Top := 100; //How many pixels from the top of the screen frmDesign.Width := 350; //The width of the window frmDesign.Height := 250; //The height of the window frmDesign.Caption := 'Hello World !'; //The title of the window frmDesign.Visible := False; //Need to be "false" if we want to call the form within a thread txtMessage := TEdit.Create(frmDesign); //Create the textbox txtMessage.Parent := frmDesign; //Add the textbox to the form txtMessage.Left := 100; //How many pixels from the left of the form txtMessage.Top := 50; //How many pixels from the top of the form txtMessage.Width := 150; //Lenght of the textbox txtMessage.Height := 25; //Height of the textbox cmdDisplayMessage := TButton.Create(frmDesign); //Create the button cmdDisplayMessage.Parent := frmDesign; //Add the button to the form cmdDisplayMessage.Left := 125; //How many pixels from the left of the form cmdDisplayMessage.Top := 150; //How many pixels from the top of the form cmdDisplayMessage.Width := 100; //Width of the button cmdDisplayMessage.Height := 25; //Height of the button cmdDisplayMessage.Caption := 'GO !'; //What's on the label of the button End; Procedure SafeInitForm; Var v: TVariantArray; Begin SetArrayLength(V, 0); ThreadSafeCall('InitForm', v); //Important part: call the InitForm (your GUI) End; Procedure ShowFormModal; Begin frmDesign.ShowModal; //Make the GUI visible End; Procedure SafeShowFormModal; Var v: TVariantArray; Begin SetArrayLength(V, 0); ThreadSafeCall('ShowFormModal', v); //Important part: make the GUI visible using ShowFormModal End; Begin SafeInitForm; //Initialize the form using the threading function SafeShowFormModal; //Make the form visible using the threading function End. Chapter Five: Adding Code To The Button Now, we want the button to display "Hello World !" in the textbox when we press it. To do so, we need to tell the button what happen when it's beeing click using the "@". Code (Text): "variable name".onClick := @"procedure to call" This code need to be in the InitForm again after all the parameters we coded for the button. SCAR will know that this button need to perform an action this way. In our script, the button will call the procedure ButtonClick. Code (Text): cmdDisplayMessage.OnClick:= @ButtonClick; //Call a procedure on click This procedure need to be created. The trick here is that the procedure need to know who sent the command. To do this, we will add a little extra code into the procedure and this will able the script to modify the form even if the form was declared in a thread. But SCAR beeing... well, SCAR, we need to put this procedure BEFORE the InitForm or the button won't be able to call it. (FYI, it's like this for all procedure. You can't call a procedure if the code is after the "caller" in the script") Code (Text): Procedure ButtonClick(sender: TObject); Begin txtMessage.text := 'Hello World !'; //Change the text in the textbox End; The code should look like this at this point: Code (Text): Program UsingForm; Var frmDesign : TForm; //The form itself txtMessage : TEdit; //The textbox cmdDisplayMessage : TButton; //The button Procedure ButtonClick(sender: TObject); Begin txtMessage.text := 'Hello World !'; //Change the text in the textbox End; Procedure InitForm; Begin frmDesign := CreateForm; //Create the form frmDesign.Left := 500; //How many pixels from the left of the screen frmDesign.Top := 100; //How many pixels from the top of the screen frmDesign.Width := 350; //The width of the window frmDesign.Height := 250; //The height of the window frmDesign.Caption := 'Hello World !'; //The title of the window frmDesign.Visible := False; //Need to be "false" if we want to call the form within a thread txtMessage := TEdit.Create(frmDesign); //Create the textbox txtMessage.Parent := frmDesign; //Add the textbox to the form txtMessage.Left := 100; //How many pixels from the left of the form txtMessage.Top := 50; //How many pixels from the top of the form txtMessage.Width := 150; //Lenght of the textbox txtMessage.Height := 25; //Height of the textbox cmdDisplayMessage := TButton.Create(frmDesign); //Create the button cmdDisplayMessage.Parent := frmDesign; //Add the button to the form cmdDisplayMessage.Left := 125; //How many pixels from the left of the form cmdDisplayMessage.Top := 150; //How many pixels from the top of the form cmdDisplayMessage.Width := 100; //Width of the button cmdDisplayMessage.Height := 25; //Height of the button cmdDisplayMessage.Caption := 'GO !'; //What's on the label of the button cmdDisplayMessage.OnClick := @ButtonClick; //Call a procedure on click End; Procedure SafeInitForm; Var v: TVariantArray; Begin SetArrayLength(V, 0); ThreadSafeCall('InitForm', v); //Important part: call the InitForm (your GUI) End; Procedure ShowFormModal; Begin frmDesign.ShowModal; //Make the GUI visible End; Procedure SafeShowFormModal; Var v: TVariantArray; Begin SetArrayLength(V, 0); ThreadSafeCall('ShowFormModal', v); //Important part: make the GUI visible using ShowFormModal End; Begin SafeInitForm; //Initialize the form using the threading function SafeShowFormModal; //Make the form visible using the threading function End. Chapter Six: Nice To Have... The script is pretty much done. But I added this section just for 1 line of code simply because this line is very usefull. Code (Text): "variable name".ModalResult:= mrOk; This code will simply close the form and terminate the script once executed. We need to use it on the form variable directly. Code (Text): frmDesign.ModalResult:= mrOk; //Close the form and terminate the script Script Example This is the final release of the script ! Congratulation ! Code (Text): Program UsingForm; Var frmDesign : TForm; //The form itself txtMessage : TEdit; //The textbox cmdDisplayMessage : TButton; //The button Procedure ButtonClick(sender: TObject); Begin txtMessage.text := 'Hello World !'; //Change the text in the textbox End; Procedure InitForm; Begin frmDesign := CreateForm; //Create the form frmDesign.Left := 500; //How many pixels from the left of the screen frmDesign.Top := 100; //How many pixels from the top of the screen frmDesign.Width := 350; //The width of the window frmDesign.Height := 250; //The height of the window frmDesign.Caption := 'Hello World !'; //The title of the window frmDesign.Visible := False; //Need to be "false" if we want to call the form within a thread txtMessage := TEdit.Create(frmDesign); //Create the textbox txtMessage.Parent := frmDesign; //Add the textbox to the form txtMessage.Left := 100; //How many pixels from the left of the form txtMessage.Top := 50; //How many pixels from the top of the form txtMessage.Width := 150; //Lenght of the textbox txtMessage.Height := 25; //Height of the textbox cmdDisplayMessage := TButton.Create(frmDesign); //Create the button cmdDisplayMessage.Parent := frmDesign; //Add the button to the form cmdDisplayMessage.Left := 125; //How many pixels from the left of the form cmdDisplayMessage.Top := 150; //How many pixels from the top of the form cmdDisplayMessage.Width := 100; //Width of the button cmdDisplayMessage.Height := 25; //Height of the button cmdDisplayMessage.Caption := 'GO !'; //What's on the label of the button cmdDisplayMessage.OnClick := @ButtonClick; //Call a procedure on click End; Procedure SafeInitForm; Var v: TVariantArray; Begin SetArrayLength(V, 0); ThreadSafeCall('InitForm', v); //Important part: call the InitForm (your GUI) End; Procedure ShowFormModal; Begin frmDesign.ShowModal; //Make the GUI visible End; Procedure SafeShowFormModal; Var v: TVariantArray; Begin SetArrayLength(V, 0); ThreadSafeCall('ShowFormModal', v); //Important part: make the GUI visible using ShowFormModal End; Begin SafeInitForm; //Initialize the form using the threading function SafeShowFormModal; //Make the form visible using the threading function End. The End Hope this tutorial helped you with your coding. This still is a very small idea of what SCAR can do for us. I'm currently trying to code a small game with scar using the debug/canvas window but there's so much more things involve when getting into this kind of coding. Anyway, I apologize for any typo, weird words, unreadable sentences: english is not my main language and I'm not perfect so if you see any problem with this tutorial, let me know so I can fix it. I'm also open to questions for SCAR so go ahead, challenge me !