Results 1 to 12 of 12

Thread: Function and Procedure 'variables'.

  1. #1
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,691
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default Function and Procedure 'variables'.

    Well, this is a cool thing you can do in SCAR, I don't know how usefull it is, but it is still pretty cool.

    Say, you got this program:

    SCAR Code:
    program New;

    procedure writemessage(s: String);

    begin
      writeln(s);
    end;

    begin
      writemessage('hello');
    end.

    This will write hello in your debug.

    You can make 'shortcuts', as I like to call them, to this procedure.
    A shortcut can be made by adding an @+ProcedureName.
    SCAR Code:
    Write:=@WriteMessage;

    You might know this way of adressing functions, I was first introduced to them when I worked with forms and TTimers.

    SCAR Code:
    Timer.OnTimer := @SomeProcedure;
    // Or
    Button.OnClick := @SomeProcedure;

    SCAR Code:
    program New;

    procedure writemessage(s: String);

    begin
      writeln(s);
    end;

    Var
       Write: Procedure (avar: string);
       
    begin
      Write := @WriteMessage;
      Write('hello');
    end.

    Please not that the shortcut's procedure input and output (only for functions) has to be the same.

    A simple example with a function:

    SCAR Code:
    program New;

    procedure writemessage(s: String);

    begin
      writeln(s);
    end;

    function returnnumber:integer;

    begin
      result := 5;
    end;

    Var
       Write: Procedure (avar: string);
       Number: Function: Integer;
       
    begin
      Write := @WriteMessage;
      Number := @ReturnNumber;
      Write(IntToStr(Number()));
    end.

    Please note the Number(), if you don't add this SCAR will interpret it as a type mismatch.

    You can also remove the link from the shortcut by simply setting it to nil.

    SCAR Code:
    Write := nil;

    You can make it more complicated.
    I wrote this when I was experimenting on using multiple locations (Lumbridge, Varrock) in one script, without using cases in walking (Like VYC does).

    First, I made a record of a Location.
    SCAR Code:
    Type
       TLocation = Record
         Name: String;
         ToMine, ToBank, BankItems, Start, Stop: Procedure;
         MyMine: Function: Boolean;
       End;
       TLocationArray = Array Of TLocation;
    Var
      Locations: TLocationArray;
      CurrentLocation: TLocation;

    Load the Locations array like this:

    SCAR Code:
    Procedure LoadLocations;

    Begin
      SetLength(Locations, 1);
      Locations[0].Name := 'VEM';
      Locations[0].ToMine := @VEM_ToMine;
      Locations[0].ToBank := @VEM_ToBank;
      Locations[0].BankItems := @VEM_BankStuff;
      Locations[0].MyMine := @VEM_MyMine;
    End;

    This way your mainloop could look like this:

    SCAR Code:
    Repeat
        ResetLocation(CurrentLocation);
        CurrentLocation := Fetch_Location(Players[CurrentPlayer].Strings[3]);
        {This string (Strings[3]) holds the location name.
          Fetch_Location returns a TLocation. }



        CurrentLocation.Start();
        If Players[CurrentPlayer].Loc = 'Bank' Then
        Begin
          CurrentLocation.ToMine();
        End;

        If Players[CurrentPlayer].Loc = 'Mine' Then
        Begin
          Repeat
            Wait(100);
            If Not CurrentLocation.MyMine() Then
              Wait(50);
            If FlagPresent Then
              Wait(2000 + Random(500));
            FindNormalRandoms;
            If Not LoggedIn Then Break;
            If Not FindPick then Break;
          Until (InvFull Or ((GetSystemTime - MyTimer) > 60000 * MinutesPerLoad));

          CurrentLocation.ToBank();
          CurrentLocation.BankItems();
        End;
        ProgressReport;
        CurrentLocation.Stop();
        //Something with player logging in and out.
    Until False;

    Fetch_Location could look like this:

    SCAR Code:
    Function Fetch_Location(Name: string): TLocation;

    Var
       I: Integer;
    Begin
      Name := LowerCase(Name);
      For I := Low(Locations) To High(Locations) Do
        If Name = LowerCase(Locations[I].Name) Then
        Begin
          Result := Locations[I];
          Exit;
        End;
    End;


    You can also make Array of Function/Procedure.

    SCAR Code:
    program New;

    type TFuncArray = Array Of Function: Integer;

    Var
       a: TFuncArray;
       
    Function Return1: Integer;

    Begin
      result := 8;
    End;

    Function Return2: Integer;

    Begin
      result := 4;
    End;

    var
       count: integer;
    begin
      setlength(a, 2);
      a[0] := @return1;
      a[1] := @return2;
      for count := 0 to 1 do
        writeln(inttostr(a[count]()));
    end.

    I hope this helped.

    Raymond, SKy, nielsie and everyone else:
    Please correct me on name-mistakes, general mistakes, extra features, or anything else that you think I should change or add to this tutorial.

    ~Wizzup?



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  2. #2
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Wizzup? View Post
    I hope this helped.
    This tutorial helped me very much to get really cool mainloops like you.. Thanks!
    Verrekte Koekwous

  3. #3
    Join Date
    Aug 2006
    Location
    London
    Posts
    2,021
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    wow this is great, its a lot like function pointers in C
    Join the Official SRL IRC channel. Learn how to Here.

  4. #4
    Join Date
    Dec 2006
    Location
    utah
    Posts
    1,427
    Mentioned
    2 Post(s)
    Quoted
    7 Post(s)

    Default

    Dis how i did my randoms

    I wish i didn't have problems doing this

    Procs := [@FindTalk, @FindIven, FindTeled];

    or something like that
    Co Founder of https://www.tagcandy.com

  5. #5
    Join Date
    Jun 2006
    Posts
    3,861
    Mentioned
    3 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Wizzup? View Post
    You can also remove the link from the shortcut by simply setting it to nil.

    SCAR Code:
    Write := nil;
    Didn't know that, thanks
    That's the first thing I've learned in a long time =/

  6. #6
    Join Date
    Mar 2007
    Location
    Ohio
    Posts
    138
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    i've never seen that before, at first im like a procedures and functions tut in advanced, better check it out. But its pretty advanced but its good to know this thanks!

  7. #7
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by bullzeye95 View Post
    Didn't know that, thanks
    That's the first thing I've learned in a long time =/
    You would've know it, I've used it before =].. Guess in what "game"..
    Verrekte Koekwous

  8. #8
    Join Date
    Jul 2007
    Posts
    1,431
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by mastaraymond View Post
    This tutorial helped me very much to get really cool mainloops like you.. Thanks!
    Though you rarely release a script.

    Anyways, didn't knew I can manipulate with functions. Now I do, thanks for this.
    [CENTER][SIZE="4"]Inactive[/SIZE]I forgot my password[/CENTER]

  9. #9
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Negaal View Post
    Though you rarely release a script.

    Anyways, didn't knew I can manipulate with functions. Now I do, thanks for this.
    It was sarcastic
    Verrekte Koekwous

  10. #10
    Join Date
    Aug 2006
    Location
    London
    Posts
    60
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Pretty interesting, had always suspected after seeing it being used in forms as well... Surprised I never bothered to find out earlier :P

    Thanks for this, but are there any other benefits apart from saving coding time and keeping code neater (performance benefits etc..)? Just because I wonder if this would effect the scripting standards...?

  11. #11
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,691
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by 1337. View Post
    Pretty interesting, had always suspected after seeing it being used in forms as well... Surprised I never bothered to find out earlier :P

    Thanks for this, but are there any other benefits apart from saving coding time and keeping code neater (performance benefits etc..)? Just because I wonder if this would effect the scripting standards...?
    I have combined some Mining scripts of mine.
    I'll post a few samples:

    SCAR Code:
    Type
       TLocalVar = Record
         Value: Variant;
         IsSet: Boolean;
         Name, vType: String;
       End;
       TLocals = Array Of TLocalVar;
       
       TMineInfo = Record
         HasNewRocks: Boolean;
         GasCheck, IsNewRock: Function (P: TPoint): Boolean;
         Ores: TStringArray;
         MMRockName, RunDir: String;
         MineDist: Integer;
       End;

       TFunctionBArray = Array Of Function: Boolean;

       tExtraFunc = Record
         Func: TFunctionBArray;
         PointInScript: Integer;
       End;

       TExtraFuncArray = Array Of tExtraFunc;

       TLocation = Record
         Name: String;
         ToMine, ToBank, SetOreColor, Start, Stop: Procedure;
         OpenBank: Function: Boolean;
         AutoColorOptions: Array Of String;
         MineInfo: TMineInfo;
         LocalVars: TLocals;
         Loads, TimeRan, WorkedTimer: Integer;
         Funcs: TExtraFuncArray;
       End;
       TLocationArray = Array Of TLocation;
       
       myReport = Record
         Bmp, Value: Integer;
         Name, wType: String;
       End;
       TReportArray = Array [0..19] Of myReport;


    Const
       LOC_BANK = 0;
       LOC_ATMINE = 1;
       LOC_MINING = 2;
       LOC_DONEMINING = 3;
       LOC_AFTERFIGHT = 4;

    Var
      Locations: TLocationArray;
      CurrentLocation: TLocation;
      ReportArray: TReportArray;

    SCAR Code:
    Function ExtraFuncs(Current: Integer): Array Of Boolean;

    Var
      I , C: Integer;
    Begin
      For I := 0 To High(CurrentLocation.Funcs) Do
        If Current = CurrentLocation.Funcs[I].PointInScript Then
        Begin
          SetLength(Result, Length(CurrentLocation.Funcs[I].Func));
          For C := 0 To High(CurrentLocation.Funcs[I].Func) Do
            CurrentLocation.Funcs[I].Func[C]();
          Exit;
        End;
    End;

    Location loading.

    SCAR Code:
    Procedure LoadLocations;

    Begin
      SetLength(Locations, 2);
      With Locations[0] Do
      Begin
        Name := 'VEM';
        ToMine   := @VEM_ToMine;
        ToBank   := @VEM_ToBank;
        Start    := @VEM_Start;
        Stop     := @VEM_Stop;
        OpenBank := @VEM_EasyBank;
        SetOreColor := @VEM_SetOreColor;
        AutoColorOptions := ['anvil'];
        Loads := VEMLoads;
       
        SetLength(Funcs, 0);
        SetLength(LocalVars, 0);

        With MineInfo Do
        Begin
          HasNewRocks := True;
          IsNewRock := @VEM_IsNewRock;
          GasCheck := @w_GasCheck;
          Ores := ['Iron', 'Tin', 'Copper'];
          MMRockName := 'brown rock';
          RunDir := 'S';
          MineDist := 34;
        End;
      End;
     
      With Locations[1] Do
      Begin
        Name := 'LSM';
        ToMine   := @LSM_ToMine;
        ToBank   := @LSM_ToBank;
        Start    := @LSM_Start;
        Stop     := @LSM_Stop;
        OpenBank := @LSM_OpenBank;
        SetOreColor := @LSM_SetOreColor;
        AutoColorOptions := ['bank'];
        Loads := LSMLoads;
        SetLength(Funcs, 2);
       
        With Funcs[0] Do
        Begin
          SetLength(Func, 1);
          Func[0] := @LSM_SetCallibrateTimer;
          PointInScript := LOC_ATMINE;
        End;

        With Funcs[1] Do
        Begin
          SetLength(Func, 1);
          Func[0] := @LSM_Callibrate;
          PointInScript := LOC_MINING;
        End;

        SetLength(LocalVars, 2);
        With LocalVars[0] Do
        Begin
          Name  := 'Draynor Bank DTM';
          IsSet := False;
          Value := 0;
          vType := 'DTM';
        End;
       
        With LocalVars[1] Do
        Begin
          Name  := 'Callibrate Timer';
          IsSet := False;
          Value := 0;
          vType := 'Timer';
        End;

        With MineInfo Do
        Begin
          HasNewRocks := True;
          IsNewRock := @LSM_IsNewRock;
          GasCheck := @w_GasCheck;
          Ores := ['Coal', 'Mithril', 'Adamant'];
          MMRockName := 'brown rock';
          RunDir := 'E';
          MineDist := 50;
         End;
      End;

    And the mainloop:

    SCAR Code:
    begin
      SetupScript;

      Repeat
        FreeLocation(CurrentLocation);
        Fetch_Location(CurrentLocation);
        CurrentLocation.Start();
       
        If Players[CurrentPlayer].Loc = 'Bank' Then
        Begin
          Location_Debug('Going to the Mine');
         
          ExtraFuncs(LOC_BANK);
         
          Try
            CurrentLocation.ToMine();
          Except End;
        End;

        If Players[CurrentPlayer].Loc = 'Mine' Then
        Begin
       
          ExtraFuncs(LOC_ATMINE);
         
          Players[CurrentPlayer].Level[15] := GetSkillInfo('mining', False);
          SetRun(True);
         
          Location_Debug('Players.Loc = Mine');
          Location_Debug('Starting Mine Loop.');
         
          MyTimer := GetSystemTime;
         
          Repeat
            Wait(200);
            If Not MyMine Then
              if TooFar then
                If MiddleOfMine(Dx, Dy) Then
                Begin
                  Location_Debug('Walking back to the mine');
                  Mouse(Dx, Dy, 2, 2, True);
                  FFlag(0);
                End;
            If FlagPresent Then
              Wait(2000+Random(500));
             
            ExtraFuncs(LOC_MINING);
           
            FindNormalRandomsTimeEx;
           
            If Not LoggedIn Then
            Begin
              Location_Debug('Not Logged-In, breaking.');
              Break;
            End;
            If Not FindPick Then
            Begin
              Location_Debug('No FindPick, breaking.');
              Break;
            End;
          Until (InvFull Or ((GetSystemTime - MyTimer) > 60000 * MinutesPerLoad));
         
          ExtraFuncs(LOC_DONEMINING);
         
          Location_Debug('Walking to the bank.');
         
          Try
            CurrentLocation.ToBank();
          Except End;
         
          Location_Debug('Players.Loc := Bank');
         
          Bank;
        End;
         
        If LoggedIn And (Players[CurrentPlayer].Banked Mod CurrentLocation.Loads = 0) Then
          Begin
            WriteLn('Switching Players. Time to leave, ' + Players[CurrentPlayer].Name + '.');
            LogOut;
            NextPlayer(True);
            InitPlayer;
          End;

        If Not LoggedIn Then
          Begin
            NextPlayer(False);
            NoPick := False;
            InitPlayer;
            If NoPick Then LogOut;
          End;
         
        CurrentLocation.Stop();
       
        ProgressReport;
      Until False;
    end.



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  12. #12
    Join Date
    Jan 2007
    Location
    Kansas
    Posts
    3,760
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    Very nice. I may try them out.


Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 5
    Last Post: 11-10-2008, 01:11 AM
  2. A Function/Procedure
    By Abbott in forum OSR Help
    Replies: 7
    Last Post: 09-09-2008, 08:48 PM
  3. 1 procedure or function
    By Kasi in forum OSR Help
    Replies: 1
    Last Post: 03-03-2008, 10:12 PM
  4. Function Return variables? How?
    By kodeejs in forum OSR Help
    Replies: 14
    Last Post: 08-19-2007, 02:10 AM

Posting Permissions

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