SRL Archive - Help and Support

need help with TIntegerArray


Laimonas171

2012-10-24 16:11:55

function addtoarray;
colorsx: TIntegerArray;
begin

if (Players[CurrentPlayer].Booleans[0] = True) then
begin
//i need to add value 1521995
end;
if (Players[CurrentPlayer].Booleans[1] = True) then
begin
//i need to add value 1321655
end;

// now array should be like [1521995, 1321655]
end;

Footy

2012-10-24 16:19:10

procedure addtoarray;         //Functions return something, so I changed it to a procedure.
var //REMEMBER - Add var before declaring variables
colorsx: TIntegerArray;
begin

if (Players[CurrentPlayer].Booleans[0] = True) then
begin
//i need to add value 1521995
Setlength(colorsx, length(colorsx) + 1); //Increase length of TIntegerArray
Colorsx[high(colorsx)] := 1521995 //Assign last spot in TIntagerArray to 1521995

end;
if (Players[CurrentPlayer].Booleans[1] = True) then
begin
//i need to add value 1321655
Setlength(colorsx, length(colorsx) + 1); //Same thing here
Colorsx[high(colorsx)] := 1231655;
end;

// now array should be like [1521995, 1321655]
end;

Laimonas171

2012-10-24 16:22:14

Thank you.

edit:
maybe here is way to add one then more at once? etc. if true, add two colours

Footy

2012-10-24 16:47:17

To add two colors, you could do the above twice, or this:

setlength(AnArray, length(anArray) + 2);
AnArray[high(AnArray) - 1] := WhatEver;
AnArray[high(AnArray)] := Whateverelse;

Laimonas171

2012-10-24 19:37:43

From: Footy
To add two colors, you could do the above twice, or this:

setlength(AnArray, length(anArray) + 2);
AnArray[high(AnArray) - 1] := WhatEver;
AnArray[high(AnArray)] := Whateverelse;

i did twice by my self as well, but i think here should be cycle or sth to do them at 'once'

riwu

2012-10-24 20:13:24

EDIT, opps forget that you are adding different colors, do this then:

function addtoarray;
var
i: Integer;
colorsx, StaticArray: TIntegerArray;
begin
StaticArray:= [1521995, 1231655];
for i:=0 to 1 do
if (Players[CurrentPlayer].Booleans[i]) then //you don't need the = true
begin
Setlength(colorsx, length(colorsx) + 1);
Colorsx[high(colorsx)] := StaticArray[i];
end;

writeln(Colorsx);
end;

Although if you are trying to let the user input as many colors as they desire, why not just give them an array to fill?

Laimonas171

2012-10-25 01:44:17

From: riwu


Although if you are trying to let the user input as many colors as they desire, why not just give them an array to fill?


because I state the colours and let them to choose True or false for some procedure cases.
edit: example if they choose to mine iron, i put iron colors in, if they choose mine clay as well I put clay colors, and all upTexts as in same way :)

riwu

2012-10-25 02:01:17

From: Laimonas171
because I state the colours and let them to choose True or false for some procedure cases.
edit: example if they choose to mine iron, i put iron colors in, if they choose mine clay as well I put clay colors, and all upTexts as in same way :)

Hmm might be better using an enumeration or something else, but i guess the current state isn't too bad.