PDA

View Full Version : All-Proof - Understanding Variant/TVariantArray



Daniel
10-09-2008, 09:21 AM
Understanding Variant/TVariantArray's
By Dan's The Man

NOTE: I tried to make this as noob-proof as possible (by my standards :p). 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:
What are Variants?
How to use Variants.
Understanding TVariantArrays.
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:
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:
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.
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 :p).

4 - Putting it all together:
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!

Naum
10-21-2008, 03:51 PM
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!

TViYH
10-21-2008, 05:49 PM
Thanks for this.

Very helpful tutorial!

Daniel
10-26-2008, 08:12 AM
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 :)

Thanks for this.

Very helpful tutorial!
Thanks ;)

BazzBarrett
12-01-2008, 04:38 PM
thanks i realy whanted 2 look up on this :D

marpis
06-08-2009, 08:14 AM
I think you should add here stuff like this:

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.

Da 0wner
06-08-2009, 04:17 PM
O_O, I did not know that there was constants for var types. Thoguht you just had to use 4, 255, etc.

Imagine
03-11-2012, 03:30 AM
So whats the advantage of using say a string or an integer over a variant?