Results 1 to 8 of 8

Thread: Random function/procedure?

  1. #1
    Join Date
    Nov 2006
    Posts
    2,369
    Mentioned
    4 Post(s)
    Quoted
    78 Post(s)

    Default Random function/procedure?

    Currently im doing this not to always do things in same order... Would there be any shorter way of doing this?
    Code:
    case Random(6) of
        0:
        begin
          Retaliate(True);
          MakeCompass('S');
          SetRun(True);
          SetAngle(True);
        end;
        1:
        begin
          Retaliate(True);
          SetAngle(True);
          SetRun(True);
          MakeCompass('S');
        end;
        2:
        begin
          MakeCompass('S');
          SetRun(True);
          Retaliate(True);
          SetAngle(True);
        end;
        3:
        begin
          SetRun(True);
          MakeCompass('S');
          SetAngle(True);
          Retaliate(True);
        end;
        4:
        begin
          SetAngle(True);
          SetRun(True);
          MakeCompass('S');
          Retaliate(True);
        end;
        5:
        begin
          SetAngle(True);
          Retaliate(True);
          SetRun(True);
          MakeCompass('S');
        end;
      end;
    When i saw this(under)in timing.scar i thought if there could be function/procedure arrays so i could do like FArray[Random(3)] but i think there is not..
    Code:
    Function WaitFunc(Func: Function: boolean; MaxTime: integer): boolean;
    var
      T: integer;
    begin
      T := GetSystemTime + MaxTime;
      while GetSystemTime < T do
      begin
        wait(10);
        if Func() then
        begin
          Result := true;
          break;
        end;
      end;
    end;
    Edit: Woot i think it could be done with this function: CallProc(ProcName: string; var V: TVariantArray): Variant;
    What do you think?
    Quote Originally Posted by DeSnob View Post
    ETA's don't exist in SRL like they did in other communities. Want a faster update? Help out with updating, otherwise just gotta wait it out.

  2. #2
    Join Date
    May 2007
    Location
    England
    Posts
    4,140
    Mentioned
    11 Post(s)
    Quoted
    266 Post(s)

    Default

    Writing up my solution now.

    EDIT:

    SCAR Code:
    procedure DoThings;
    var
      I, T : Integer;
      Y, U, O, P : Boolean;
     
    begin
      repeat
        begin
          T:= Random(4);
          if (T = 0) and (Y <> True) then
          begin
            SetRun(True);
            Y:= True;
          end;
          if (T = 1) and (U <> True) then
          begin
            MakeCompass('S');
            U:= True;
          end;
          if (T = 2) and (O <> True) then
          begin
            SetAngle(True);
            O:= True;
          end;
          if (T = 3) and (P <> True) then
          begin
            Retaliate(True);
            P:= True;
          end;
        end;
      until((Y = True) and (U = True) and (O = True) and (P = True));
    end;

    Richard.
    Last edited by Rich; 08-25-2009 at 09:20 PM.
    <3

    Quote Originally Posted by Eminem
    I don't care if you're black, white, straight, bisexual, gay, lesbian, short, tall, fat, skinny, rich or poor. If you're nice to me, I'll be nice to you. Simple as that.

  3. #3
    Join Date
    Aug 2008
    Location
    Finland
    Posts
    2,851
    Mentioned
    3 Post(s)
    Quoted
    2 Post(s)

    Default

    something based on ThreadSafeCall?
    SCAR Code:
    program New;
    {.include srl/srl.scar}
    var
      procs: array of string;
      tmp: TVariantArray;    
       
    procedure proc1;
    begin
      writeln('proc 1!');
    end;

    procedure proc2;
    begin
      writeln('proc 2!');
    end;

    procedure proc3;
    begin
      writeln('proc 3!');
    end;

    begin
      procs := ['proc1', 'proc2', 'proc3'];
      ThreadSafeCall(procs[random(length(procs))], tmp);
    end.


    SCAR Code:
    procedure SetupPlayer;
    var
      r: integer;
      b: array [0..3] of boolean;
    begin
      repeat
        r := random(4);
        case r of
          0: if not b[0] then SetAngle(True);
          1: if not b[1] then SetRun(True);
          2: if not b[2] then MakeCompass('S');
          3: if not b[3] then Retaliate(True);
        end;
        b[r] := true;
      until(b[0] and b[1] and b[2] and b[3]);
    end;
    Hah, simple as that!
    EDIT: No, wait a second i fix it...
    EDIT: Now it works!
    saves lines, ay?
    Last edited by marpis; 08-25-2009 at 09:37 PM.

  4. #4
    Join Date
    Nov 2006
    Posts
    2,369
    Mentioned
    4 Post(s)
    Quoted
    78 Post(s)

    Default

    Thanks both
    Edit: Wait a minute... you can do this: Array Of Procedure; But im always getting errors when i try to use it


    Doesnt work like this:
    Code:
    program Tests;
    
    var
    procs: Array Of Procedure;
    
    
    begin
      Procs := [@ActivateClient, @SetDesktopAsClient, @ClearTimeOuts];
      Procs[Random(3)]();
    end.
    Failed when compiling
    Line 8: [Error] (8:12): Unknown identifier 'ActivateClient' in script C:\Program Files\SCAR 3.21\Scripts\Testsss.scar
    Quote Originally Posted by DeSnob View Post
    ETA's don't exist in SRL like they did in other communities. Want a faster update? Help out with updating, otherwise just gotta wait it out.

  5. #5
    Join Date
    Apr 2008
    Location
    Marquette, MI
    Posts
    15,252
    Mentioned
    138 Post(s)
    Quoted
    680 Post(s)

    Default

    Quote Originally Posted by antti mies View Post
    Failed when compiling
    Line 8: [Error] (8:12): Unknown identifier 'ActivateClient' in script C:\Program Files\SCAR 3.21\Scripts\Testsss.scar
    Um I don't know exactly if ActivateClient is in SRL, but seeing as you're getting this error, it could be. Make sure you call SetupSRL at the beginning of your main loop. And include SRL at the top of your script.

    Either that, or you don't have a procedure called ActivateClient.
    Last edited by Coh3n; 08-25-2009 at 11:13 PM.

  6. #6
    Join Date
    Oct 2006
    Location
    ithurtsithurtsithurtsithurts
    Posts
    2,930
    Mentioned
    7 Post(s)
    Quoted
    135 Post(s)

    Default

    Quote Originally Posted by antti mies View Post
    Thanks both
    Edit: Wait a minute... you can do this: Array Of Procedure; But im always getting errors when i try to use it


    Doesnt work like this:
    Code:
    program Tests;
    
    var
    procs: Array Of Procedure;
    
    
    begin
      Procs := [@ActivateClient, @SetDesktopAsClient, @ClearTimeOuts];
      Procs[Random(3)]();
    end.
    Failed when compiling
    Line 8: [Error] (8:12): Unknown identifier 'ActivateClient' in script C:\Program Files\SCAR 3.21\Scripts\Testsss.scar
    You can't use SCAR procedures/functions like that. You can only use procedures/functions from scripts and includes like that.

  7. #7
    Join Date
    May 2007
    Location
    England
    Posts
    4,140
    Mentioned
    11 Post(s)
    Quoted
    266 Post(s)

    Default

    Try

    SCAR Code:
    program Tests;

    var
    procs: Array Of Procedure;

    procedure AC;
    begin
      ActivateClient;
    end;

    procedure SDAC;
    begin
      SetDesktopAsClient;
    end;

    procedure CTO;
    begin
      ClearTimeOuts;
    end;

    begin
      Procs:= [@AC, @SDAC, @CTO];
      Procs[Random(3)]();
    end.
    <3

    Quote Originally Posted by Eminem
    I don't care if you're black, white, straight, bisexual, gay, lesbian, short, tall, fat, skinny, rich or poor. If you're nice to me, I'll be nice to you. Simple as that.

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

    Default

    SCAR Code:
    procedure DoThings;
    var
      A: Array of Array of Array of Variant;
      I, R: Integer;
    begin
      A:= T3DVariantArray([T2DVariantArray([TVariantArray([True]), TVariantArray([True]), TVariantArray(['S']), TVariantArray([True])]), T2DVariantArray([TVariantArray(['SetAngle']), TVariantArray(['SetRun']), TVariantArray(['MakeCompass']), TVariantArray(['Retaliate'])])]);
      for I:= 0 to 3 do
      begin
        R:= Random(4 - I);
        CallProc(A[1][R][0], A[0][R]);
        Swap(A[1][R], A[1][3 - I]);
        Swap(A[0][R], A[0][3 - I]);
        SetLength(A[1][0], 3 - I);
        SetLength(A[0], 3 - I);
      end;
    end;
    Safer than marpis', since there is no potential endless loop (though there is really not much of a chance that his will lag your script), but you will need this somewhere:
    SCAR Code:
    type
      T2DVariantArray = Array of TVariantArray;
      T3DVariantArray = Array of T2DVariantArray;

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
  •