Results 1 to 4 of 4

Thread: TTask

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default TTask

    Simba Code:
    type
      TTask = record
        execute:procedure;
        conditional:function:boolean;
        active:boolean;
        executeEvery, lastTime:integer;
        stopAfter, count:int64;
        taskName:string;
      end;

      TTaskArray = array of TTask;

    procedure TTaskArray.addTask(name:string;exec:procedure;condition:function:boolean;execEvery:integer;stop:int64);
    var
      L:integer;
    begin
      L := length(self);
      setLength(self, L + 1);
      with self[L] do
      begin
        execute := @exec;
        conditional := @condition;
        active := true;
        executeEvery := execEvery;
        stopAfter := stop;
        lastTime := getSystemTime();
        taskName := name;
      end;
    end;

    procedure TTaskArray.addTask(name:string;exec:procedure; exEvery:integer);overload;
    begin
      self.addTask(name, @exec, nil, exEvery, 9223372036854775806);
    end;

    procedure TTask.executeProc();
    begin
      if (self.active = false) then
        exit();
      if ((getSystemTime - self.lastTime) > self.executeEvery) then
      begin
        inc(self.count);
        if (self.count > self.stopAfter) then
        begin
          self.active := false;
          exit();
        end else
        begin
          if (@self.conditional <> nil) then
          begin
            if self.conditional() then
              self.execute();
          end else
            self.execute();
        end;
        self.lastTime := getSystemTime();
      end;
    end;

    procedure TTaskArray.executeTasks();
    var
      i:integer;
    begin
      for i := 0 to high(self) do
        self[i].executeProc();
    end;

    procedure TTaskArray.deleteTask(index:integer);
    begin
      delete(self, index, 1);
    end;

    procedure TTaskArray.deleteTask(name:string);overload;
    var
      i:integer;
    begin
      for i := 0 to high(self) do
        if (name = self[i].taskName) then
        begin
          self.deleteTask(i);
          break;
        end;
    end;

    Executes procedures after a certain amount of time, just call TTaskArray.executeTasks();

    One example of how I used it was on my SS fighter I needed to check the players level every minute to see if they leveled up, check if prayer was activated every 30 seconds (if not then drink activate), and other stuff

    example

    Simba Code:
    myTask.addTask('Check prayers', @checkPrayers, 30000);
    myTask.addTask('Progress report', @progress, 2000);
    myTask.addtask('Drink potion', @drinkPotion, @statsLow, 15000, 15);

    Credits to slacky for delete record info

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    I don't see the point in this.. You still have to call "ExecuteTask" synchronously anyway, so you might as well just call the function directly..
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    I don't see the point in this.. You still have to call "ExecuteTask" synchronously anyway, so you might as well just call the function directly..
    It's for timed functions like checking prayer, levels, etc etc that you have to do every few mins

    Unless you want to make a variable for each time

  4. #4
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    418
    Mentioned
    2 Post(s)
    Quoted
    86 Post(s)

    Default

    Bookmarked for later use, thanks Robert!

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
  •