Daniel
2008-10-09 05:21:50
Understanding Variant/TVariantArray's
By Dan's The Man
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!