Results 1 to 15 of 15

Thread: The Concise Forms and Saved Settings Tutorial

  1. #1
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default The Concise Forms and Saved Settings Tutorial

    The Concise Forms and Saved Settings Tutorial

    Foreword:
    Greetings, everyone! This is not a comprehensive tutorial by any means. If you would like a comprehensive tutorial on forms, visit here. If you want a more comprehensive tutorial on saving and loading settings for your forms... Ask me and I'll add detail

    Table of Contents:
    1. Form Creation
    2. Form Objects
    3. Events
    4. INI Files
    5. Implement Multiple Settings Files
    6. FAQ

    Form Creation:
    You will need to make a couple of Thread Safe Calls to setup the form, the first of which is designated simply to populate and setup the form, while the second is to actually show the form to the user and grant it complete control until it is closed.
    Simba Code:
    var
      settingsForm:TForm;

    //Initialize those form variables!
    procedure InitialiseForm;
    begin
      //A lot of junk that will be explained upcoming and must happen before the show call in the next thread safe call
    end;

    //Show that form to the user!
    procedure ShowModal;
    begin
      settingsForm.ShowModal;
    end;

    begin
      ThreadSafeCall('InitialiseForm', params);
      ThreadSafeCall('ShowModal', params);
    end.
    So far, it looks pretty simple right? Wrong! It really is! So how do we setup a blank form? Well that's fairly simple, we just need to initialize it and tell simba how we want it to look, like this:
    Simba Code:
    procedure InitialiseForm;
    begin
      settingsForm:= CreateForm;//Actually create the form (necessary call)
      settingsForm.Width:= 475;//How many pixels wide will your form be?
      settingsForm.Height:= 420;//How many pixels tall will your form be?
      settingsForm.Caption:= 'Our Settings Form!';//The caption will be the text displayed at the very top of the form in the title bar
      settingsForm.Color := ClWhite;//The default color of the form and all objects that will be added to it
      settingsForm.Font.Color := ClBlack;//The default color of all text that will occur on the form
      settingsForm.Font.Size := 8;//The default font size of all text that will occur on the form
      //A lot more junk for the stuff inside the form
    end;

    Wow, we're keeping those "complicated" forms pretty simple still I hope (and to be fair, pretty empty)! Now we should have a blank form that just appears to be a new empty window. We should fill it up next - and that shall be with the inner objects I explain in the next section.

    Form Objects:
    1. Shared properties
      1. .Create(TForm);//pass the form you created to this. This must be called first before setting any values of any object
      2. .Parent:= //Set it equal to the form you want this sobejct to appear on. By defualt it will set its other values to match the form's defaults
      3. .Caption:= The text the object will have
      4. .Left:= The amount of pixels from the left side of the form that the object will be on
      5. .Top:= The amount of pixels from the top side of the form that the object will be on
      6. .Width:= The maximum amount of pixels wide the object will be
      7. .Height:= The maximum amount of pixels tall the object will be
    2. TComboBox: A drop down list of options - one of which may be selected at a time.
    3. TEdit: A text box that the user can enter custom strings into
    4. TCheckBox: A check box that the user can check (good for boolean values)
    5. TLabel: A label box that the user cannot edit in any manner
    6. TButton: A button that can be pressed

    An important feature of these objects include events. Simply put, whenever the designated action occurs (the event), the tied delegate (or procedure pointer in our case - feel free to read more about them here: http://villavu.com/forum/showthread.php?t=102800) will be called. Ok, finally this part can sound a little daunting and confusing, but I swear it's not that bad after you get your head around it. Your procedure name MUST be styled as such:
    Simba Code:
    procedure ActionClick(Sender: TObject);//It MUST be a procedure and it MUST have that parameter (which you will never pass manually), the actual name of the procedure is up to you.
    So how do we connect the desired events to the resulting action? We connect it through designated events for every object. But Kevin, what kind of situations do we even care about this? We care about this because we can use them to save the form, to load values, or to even make specific objects invisible due to other options making them useless!
    Simba Code:
    var
      settingsForm: TForm;
      timeToRun: TEdit;
      RunForever: TCheckBox;

    //Change the visibility of end early settings.
    procedure SelectRunForever(Sender: TObject);
    begin
      timeToRun.Visible:= not RunForever.CHECKED;//When the boolean (checkbox) to run the script forever is true, then don't allow the user to see or edit the box that sets the time to run for.
    end;

    procedure InitialiseForm;
    begin
      //Keep previous logic in place and add:
      timeToRun:= TEdit.Create(settingsForm);
      timeToRun.Parent:= settingsForm;
      RunForever:= TCheckBox.Create(settingsForm);
      RunForever.Parent:= settingsForm;
      timeToRun.Visible:= false;
      timeToRun.Left:= 255;
      timeToRun.Top:= 115;
      RunForever.CAPTION:= 'Run Forever';
      RunForever.Left:= 170;
      RunForever.Top:= 115;
      RunForever.CHECKED:= True;
      RunForever.ONCLICK:= @SelectRunForever;//Whenever the box is clicked (checked or unchecked), call SelectRunForever
    end;

    Congratulations! This is what your form should now look like with the checkbox unchecked! (And that editable box should disappear when you check the box). Now let's look into combo boxes. These are the drop down menus that are really helpful for allowing the user to choose a pre-defined list of options. Like the other objects, the combo box has top, left, width, height, font, parent, and create properties. However, the combo box has several unique properties such as:
    1. .Style:= csDropDownList;//there are multiple cs* styles, however I like this one best. This defines the style of the combo box
    2. .Items;//This is the list of options in the combo box
    3. .Items.Add(value: String);//Use this to add a new value to the list of options in the combo box
    4. .ItemIndex:= 0;//Finally after all items have been added, you can default the currently selected item to a given value in the list through a value with an index starting at 0

    Now let's look at this wonder in code:
    Simba Code:
    procedure InitialiseForm;
    var
      comboBox: TComboBox;
      label: TLabel;
    begin
      //Same junk as before.
      comboBox:= TComboBox.Create(settingsForm);
      comboBox.Parent:= settingsForm;  
      comboBox.Left:= 255;
      comboBox.Top:= 10;
      comboBox.Width:= 30;
      comboBox.Style:= csDropDownList;
      comboBox.Items.Add('1');
      comboBox.Items.Add('2');
      comboBox.Items.Add('3');
      comboBox.ItemIndex:= 0;            
      label:= TLabel.Create(settingsForm);
      label.Parent:= settingsForm;
      label.FONT.Size:= 10;
      label.Left:= 165;
      label.Top:= 10;
      label.Caption:= 'XP Bar slot';
    end;

    Finally, we need a button and the ability to close our form to start the script. If you have everything else, then here is a piece of cake.
    Simba Code:
    procedure OnButtonClick(Sender: TObject);
    begin
      SetVariablesFromForm;//your own method to set all your variables to the .Caption values of the form
      SaveForm;//Your own method to save your variables to a form using a technique I'm about to describe in the next section
      settingsForm.MODALRESULT:= mrOk;//this command will set the form as ready to be closed and it will close automatically
    end;

    procedure InitialiseForm;
    var
      saveButton: TButton
    begin
      //Everything we had before
      saveButton:= TButton.Create(settingsForm);
      saveButton.Parent:= settingsForm;
      saveButton.Left:= 140;
      saveButton.Top:= 375;
      saveButton.Caption:= 'Start!';
      saveButton.OnClick:= @OnButtonClick;//this way clicking the button does something
    INI Files

    This is easily the simplest method of saving/writing settings to files (originally pointed to me by Mr. Amazing @Ollybest - although I did the reading learning myself ). There are several built in methods for writing settings either to dedicated ini files or the default simba ini file (unused by anyone I know of, and therefore useless, so I suggest using your own per script).
    Saving to files:
    Simba Code:
    WriteINI(sectionName, keyName, valueName, saveFileName: String);
    WriteINI('Settings', 'PlayerName', 'bot 123', (AppPath + 'MySettingsFile.ini'));//example
    The aforementioned example will find the "MySettingsFile.ini" file in the AppPath folder, go to the section labeled "Settings", and set the "PlayerName" variable to "bot 123". And if any of those don't already exist (file, section, variable), it will be automatically created for you. Easy!
    Loading from files:
    Simba Code:
    ReadINI(sectionName, keyName, saveFileName: String): String;//returns the valueName set before
    textBox.Caption:= ReadINI('Settings', 'PlayerName', (AppPath + 'MySettingsFile.ini'));//example - sets the caption of the text box to the value matching all the previous setup
    Implement Multiple Settings Files:

    This is a little more complicated than the aforementioned ideas and it assumes you already have created your own custom load and save forms. I will show everything at once with the upcoming code, but I will comment to try and help. Please ask if you have questions!
    Simba Code:
    var
      SaveFileName: String;

    procedure SaveForm(Sender: TObject);
    begin
      if(comboBox.ITEMINDEX<0)then
        SaveFileName:= AppPath + 'ScriptName' + PlayerName + '.ini';//I will likely not include script name in my personal work so I can start using a single settings file that handles many scripts.
      //Save your form to your file
    end;

    procedure LoadForm(Sender: TObject);
    begin
      if(comboBox.ITEMINDEX<0)then
        Exit;
      SaveFileName:= AppPath + comboBox.ITEMS[comboBox.ITEMINDEX];
      if(not FileExists(SaveFileName))then
        Exit;
      //Expected remaining loading using this new file name variable instead of the constant it was before
    end;

    procedure InitialiseForm;  
    var
      files: TStringArray;
    begin
      //Which file to load related setup
      label.Left:= 5;
      label.Top:= 315;
      label.Caption:= 'Load file: ';
      comboBox.Left:= 70;
      comboBox.Top:= 315;
      comboBox.Width:= 150;
      comboBox.Style:= csDropDownList;
      files:= GetFiles(AppPath, 'ini');
      for i:=0 to high(files) do
      begin
        comboBox.Items.Add(files[i]);
        WriteLn(files[i]);
      end;
      comboBox.ONCHANGE:= @LoadForm;//By not giving an index, it's -1 and then you can save a new form settings file
    end;
    FAQ:
    Ask me some questions and I'll toss them up here.
    Attached Images Attached Images
    Last edited by Kevin; 05-08-2013 at 03:52 AM. Reason: Added pictures and more descriptions as well as a new section.

  2. #2
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Nice one, +repped

  3. #3
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,164
    Mentioned
    26 Post(s)
    Quoted
    433 Post(s)

  4. #4
    Join Date
    Sep 2012
    Location
    Australia.
    Posts
    839
    Mentioned
    16 Post(s)
    Quoted
    225 Post(s)

  5. #5
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    Thank you all! Honestly, I was so intimidated by forms not that long ago, and then it just turned out to be that I had too many details all being thrown at me at once and I just didn't understand what NEEDED to be around and what was optional or what some of these things actually did (like the .Parent property). I hope this helps someone and we can see more forms around here, they're not all that bad after being broken up like this.

    I'm thinking of adding a small section for having multiple settings files to be account specific to support multiple accounts better (or even have basic settings being shared cross script because of that). Any thoughts on whether I should add that?

  6. #6
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,164
    Mentioned
    26 Post(s)
    Quoted
    433 Post(s)

    Default

    Quote Originally Posted by Kevin View Post
    Thank you all! Honestly, I was so intimidated by forms not that long ago, and then it just turned out to be that I had too many details all being thrown at me at once and I just didn't understand what NEEDED to be around and what was optional or what some of these things actually did (like the .Parent property). I hope this helps someone and we can see more forms around here, they're not all that bad after being broken up like this.

    I'm thinking of adding a small section for having multiple settings files to be account specific to support multiple accounts better (or even have basic settings being shared cross script because of that). Any thoughts on whether I should add that?
    Add as much as possible. IMO, the more things the better. Also, this helps me!

  7. #7
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    Quote Originally Posted by StickToTheScript View Post
    Add as much as possible. IMO, the more things the better. Also, this helps me!
    Ok then, note to self for later tonight - add:
    1)Pictures for visual help
    2)Multiple Settings files support
    3)Descriptions of Combo Boxes and related properties (I can't believe I left those at)

  8. #8
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,164
    Mentioned
    26 Post(s)
    Quoted
    433 Post(s)

    Default

    Quote Originally Posted by Kevin View Post
    Ok then, note to self for later tonight - add:
    1)Pictures for visual help
    2)Multiple Settings files support
    3)Descriptions of Combo Boxes and related properties (I can't believe I left those at)
    And i will be looking back here to see when it is updates so that I can learn more!!

  9. #9
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    Quote Originally Posted by StickToTheScript View Post
    And i will be looking back here to see when it is updates so that I can learn more!!
    All has been added like I said I would, lemme know whatcha think!

  10. #10
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,164
    Mentioned
    26 Post(s)
    Quoted
    433 Post(s)

    Default

    Quote Originally Posted by Kevin View Post
    All has been added like I said I would, lemme know whatcha think!
    I will read it tomorrow. I am pretty sure its amazing tho!

  11. #11
    Join Date
    Jan 2012
    Posts
    1,596
    Mentioned
    78 Post(s)
    Quoted
    826 Post(s)

  12. #12
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    Quote Originally Posted by Turpinator View Post
    is there a TL;DR for this?
    Yes. It's called copy pasta.

  13. #13
    Join Date
    Jun 2012
    Posts
    4,867
    Mentioned
    74 Post(s)
    Quoted
    1663 Post(s)

    Default

    Rep+, great tutorial!

    My first form:



    E: Oops, can't rep you again right now.

  14. #14
    Join Date
    Apr 2015
    Location
    FireFox
    Posts
    528
    Mentioned
    10 Post(s)
    Quoted
    227 Post(s)

    Default

    Sorry for the bump, just wondering whether this is outdated or not?
    Scripting with ogLib

  15. #15
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    Quote Originally Posted by srlMW View Post
    Sorry for the bump, just wondering whether this is outdated or not?
    I can't answer with certainty, but I'm pretty sure it does still work as the version of SRL this was written for hasn't been deprecated yet.

Thread Information

Users Browsing this Thread

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

Posting Permissions

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