Results 1 to 8 of 8

Thread: All-Proof - Understanding Variant/TVariantArray

  1. #1
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default All-Proof - Understanding Variant/TVariantArray

    Understanding Variant/TVariantArray's
    By Dan's The Man

    NOTE: I tried to make this as noob-proof as possible (by my standards ). So, if you like it or had learned something from it, could you also please give me some more reputation. This'll keep me writing more tutorials (since i won't think they're a waste of time to write). Thanks

    Introduction:
    Now that there's no tutorials on Variant's or TVariantArray's, i thought i'd post one so others can know what they are and how they work To continue, you need basic knowledge of SCAR and you need to know how arrays work, other than that, you're set to go

    Contents:
    1. What are Variants?
    2. How to use Variants.
    3. Understanding TVariantArrays.
    4. Putting it all together!


    1 - What are Variants?
    Variants is a variable type. As you'd probably know, types are variable declarations. So, to declare a Variant, we'd need something like:
    SCAR Code:
    Something: Variant;
    That would be a correct way how to declare a Variant, morally, any variable. Variants supports ANY type. So, it's like, all the types merged together. The string could be an integer, the string could be a boolean, an integer could be an extended value or string, etc. Ok, maybe what i had said before, about it being all types merged together, i was wrong, however, very similar. It's all type conversion methods joined together. That is like StrToInt, IntToStr, FloatToStr, Round.

    2 - How to use Variants.
    To simply put, they are used like any other variable type. You can do something similar to this code below:
    SCAR Code:
    var
      Something: Variant;

    begin
      Something:='116';
      if(Something=116) then
        Writeln('It''s 116!') else
          Writeln(':o It isn''t 116!');
    end.
    You can see that Somethings' value is put in a string, but, when we put it in an if..then statement, we make it an integer. See, it manages to automatically convert itself.
    SCAR Code:
    var
      Something: Variant;
      i: Integer;

    begin
      Something:=Random(500)+11;
      repeat
        i:=i+1;
        Writeln('Something is currently: '+Something);
      Until(i=Something);
    end.
    Again, with this example (above), we can see that when the script starts, it'll make Somethings' value 11 plus a random value of 499 and is an integer. But, in the repeat sequence, in Writeln, it is made, as a string. No IntToStr, no nothing. It's already made.

    3 - Understanding TVariantArrays:
    Simple. TVariantArrays are arrays of Variants It has exactly the same structure as a Variant, however, the information is stored in an array. Even though you were probably expecting more from this section, but, that's all there is to it

    Some procedures, such as ThreadSafeCall use TVariantArrays to store information, for some purpose (i can't find out ).

    4 - Putting it all together:
    SCAR Code:
    program NewProgram;

    var
      TVA: TVariantArray;
      ii: Integer;
     
    function ConvArray(TVA: TVariantArray; Delim: String): Variant;
    var
      i: Integer;
      d: String;
    begin
      for i:=0 to High(TVA) do
      begin
        Result:=Result+TVA[i]+D;
        D:=D+Delim;
      end;
    end;

    procedure SendLines(TV: Variant);
    var
      i: Integer;
      CS, space: String;
    begin
      for i:=1 to Length(TV) do
        if(TV[i]=' ') then
        begin
          Space:=space+' ';
          CS:=Copy(TV, 1, Pos(space, TV));
          Writeln(CS);
        end;
    end;

    function RandArray: TVariantArray;
    var
      i: Integer;
    begin
      SetArrayLength(Result, 100);
      for i:=0 to 99 do
        case (Random(2)) of
          0: Result[i]:=IntToStr(Random(1001));
          1: Result[i]:=Random(2001);
        end;
    end;

    var
      TVar: Variant;

    begin
      TVA:=RandArray;
      TVar:=ConvArray(TVA, ' ');
      SendLines(TVar);
    end.

    Something similar to that (above) should work.

    5 - An error you might encounter:
    Is... [Runtime Error] : Exception: Could not convert variant of type (*) into type (*) in line * in script.
    * - Will vary, depending on your script.

    To error is pretty much self-explanatory It could not convert the variant of type * to another type of *. Such as a string to double. There's no way to over-come this, except to change the string in the Variant.

    Add to my reputation if you like this tutorial

    Thanks for reading and i hope you're one step further into SCAR Scripting than before!
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  2. #2
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    Wo I didn't know that much about Variants, they are also used in the Deposit procedure which is pretty cool. Also Santy has a tutorial on this anyways, but GJ!

  3. #3
    Join Date
    Apr 2007
    Posts
    2,593
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks for this.

    Very helpful tutorial!

  4. #4
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    Quote Originally Posted by NaumanAkhlaQ View Post
    Wo I didn't know that much about Variants, they are also used in the Deposit procedure which is pretty cool. Also Santy has a tutorial on this anyways, but GJ!
    He does? Glad you learned somthing
    Quote Originally Posted by TheVoiceInYourHead View Post
    Thanks for this.

    Very helpful tutorial!
    Thanks
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  5. #5
    Join Date
    May 2008
    Location
    Here :p
    Posts
    194
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    thanks i realy whanted 2 look up on this

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

    Default

    I think you should add here stuff like this:
    SCAR Code:
    procedure Something(Cookie: Variant):
    begin
      case VarType(Cookie) of
       varInteger: writeln('It''s integer');
       varString: writeln('It''s string');
      end;
    end;

    edit: just for every1 to know, you can't gravedig tuts.

  7. #7
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    O_O, I did not know that there was constants for var types. Thoguht you just had to use 4, 255, etc.

  8. #8
    Join Date
    Feb 2012
    Posts
    168
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    So whats the advantage of using say a string or an integer over a variant?

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Understanding and using TPoint/TPointArray
    By JAD in forum OSR Advanced Scripting Tutorials
    Replies: 20
    Last Post: 09-25-2008, 11:57 PM
  2. Understanding sin, cos, and tan. (And the other 3)
    By R0b0t1 in forum OSR Advanced Scripting Tutorials
    Replies: 25
    Last Post: 12-15-2007, 05:04 PM
  3. help with understanding some stuff
    By tpm1164 in forum OSR Help
    Replies: 3
    Last Post: 07-13-2007, 02:20 AM
  4. Understanding SCAR Color Code
    By Yakman in forum News and General
    Replies: 26
    Last Post: 11-03-2006, 12:25 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
  •