Log in

View Full Version : Variable i



I Karma I
12-20-2006, 09:28 PM
I've been studying Fakawi's Goblin Scheduler, and on it...
procedure PlayerStats;

var Active: string;
var i, temp: Integer;
begin
if Players[CurrentPlayer].Active=True then
Active:='True'
else
Active:='False';
WriteLn ('<~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~>');
Writeln (' Name : '+ Players[CurrentPlayer].Name);
Writeln (' Number : '+inttostr(CurrentPlayer));
Writeln (' Active : '+ Active);
if ( Players[CurrentPlayer].Worked > 0 ) then
Writeln (' Worked : '+ inttostr(Players[CurrentPlayer].Worked));
Writeln (' Location : '+ Players[CurrentPlayer].loc);
Temp:=(GetSystemTime div 1000);
Temp := Temp - StartTime;
if ( playtime < temp ) then
Writeln (' Time Left : '+ IntToStr ( Temp - PlayTime / 60 ) + ' min[s].');
WriteLn ('<~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~>');

for I := 0 to HowManyPlayers - 1 do
begin
if Players[i].Active=True then Active:='T' else Active:='F';
WriteLn ( ' ' + Inttostr ( I) + ' : ' + Players[i].Name + ' = ' + Active
+'. - Lvl : '
+' '+inttostr(Players[i].level[1])
+' '+inttostr(Players[i].level[2])
+' '+inttostr(Players[i].level[3])+
+' '+inttostr(Players[i].level[5])+
+' '+inttostr(Players[i].level[8])+'. '
+'W : '+IntToStr(Players[i].Worked)+' min. '
+'K : '+ IntToStr(Players[i].Killed)+' Goblins. '
+'L: '+Players[i].loc);
end

See how i is declared and used in that procedure, and in this one:
procedure FTWaitD ( Time : Integer );
var T, I : integer;
begin
try

T := (Time div 1000)+1;
for I := 1 to T do
begin
Wait ( 250 );
FindTalk;
end

except

begin
Wait ( Time );
FindTalk;
end;

end;

Can you use i multiple times after its been ended?

Also, btw, whats the "div" mean in the 2nd ScarScript?

Hey321
12-20-2006, 10:00 PM
divided i think.

I Karma I
12-20-2006, 10:01 PM
What ya mean?

Bam Bam
12-20-2006, 11:43 PM
"div" or "/" are the SCAR equivalents to division.

The Variable "i" like any variable can be declared once globally and once within each procedure or function

I Karma I
12-20-2006, 11:49 PM
Ok, thank you.

Boreas
12-21-2006, 12:09 AM
When i(or any variable) is declared in a procedure or function like

procedure blahblah;
var i:integer;
begin
repeat
writeln('stuff');
i:=i+1;
until i=4;
end;

i is 'loaded' at the begin, and 'forgotten' at the end, every time the procedure is called. Every time the procedure is called, booleans start as false, integers as 0, and strings as ''.



program New;
{comment line 6 and leave line 8 uncommented and you should get
123123. comment line 8 and uncomment line 6 and you should get
123456, because global i is 'remembered'}
// comment
//var i:integer; // this line
procedure countto3; // or
var i:integer; // that line
begin
i:=i+1;
writeln(inttostr(i));
i:=i+1;
writeln(inttostr(i));
i:=i+1;
writeln(inttostr(i));

end;

//////EDIT FORGOT THIS PART LOL

begin
countto3;
countto3;
end.



This also means that multiple procedures can use a variable locally without affecting each other. This is why SRL uses local variables more than globals when possible.

I Karma I
12-21-2006, 12:15 AM
Ohhhh ok thx mate that explained alot :)