Autor Beitrag
Comp-Freak
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 89



BeitragVerfasst: Di 17.04.07 12:07 
I made an record looking like this:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
type
 Enemy = record
  Name     : String;
  PosX     : Double;
  PosY     : Double;
  Word     : String;
  end;

And an array looking like this:
ausblenden Delphi-Quelltext
1:
2:
 var
Enemys : array of Enemy;

on startup:
ausblenden Delphi-Quelltext
1:
setlength(Enemys,50)					

and to clean up:
ausblenden Delphi-Quelltext
1:
finalize(Enemys);					

now what im looking for is something like this:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
 for i to length of enemys do
  begin
  if enemys.enemy[i].name = 'Test' then showmessage('Yay...'+inttostr(i));
  end;

I now there is 0 percent chance it will work but just to give you an idea.
Also when you want to add something to the aray do you go:
addtoarray(bla bla)
or do i have to set a new length and just fill in the created record of enemy?
Thanks for Helping.
Kroko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1284

W98 W2k WXP
Turbo D
BeitragVerfasst: Di 17.04.07 12:11 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
 for i:= 0 to length(enemys)-1 do
  begin
  if enemys.enemy[i].name = 'Test' then showmessage('Yay...'+inttostr(i));
  end;

_________________
Die F1-Taste steht nicht unter Naturschutz und darf somit regelmäßig und oft benutzt werden! oder Wer lesen kann, ist klar im Vorteil!
Stefan.Buchholtz
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 612

WIN 2000, WIN XP, Mac OS X
D7 Enterprise, XCode, Eclipse, Ruby On Rails
BeitragVerfasst: Di 17.04.07 12:34 
When adding to the array you have to set the new length and fill the record. It is not advisable to grow the array by one every time you need a new record - this is very slow and memory intensive. You should keep the number of enemies in a seperate variable and grow the array by a large chunk if it gets too small. If you cannot predict how many records you will need the most efficient solution (in terms of speed, not necessarily memory) is to double the size of the array every time it gets to small.
Another thing: a common naming convention in Delphi is to begin every type with a T. You should call your record TEnemy.

Adding a record to the array should look like this:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure AddEnemy(const AEnemy: TEnemy);
begin
  if enemyCount >= Length(enemies) then
  begin
    SetLength(enemies, Length(enemies) * 2);
  end;
  enemies[enemyCount] := AEnemy;
  Inc(enemyCount);
end;


When searching the array you have to look at the used records only:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
for i := 0 to enemyCount - 1 do
begin
  ...
end;


Stefan

_________________
Ein Computer ohne Windows ist wie eine Schokoladentorte ohne Senf.
HelgeLange
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 735
Erhaltene Danke: 6

Windows 7
Delphi7 - Delphi XE
BeitragVerfasst: Di 17.04.07 12:44 
The plural of enemy is enemies ;)

_________________
"Ich bin bekannt für meine Ironie. Aber auf den Gedanken, im Hafen von New York eine Freiheitsstatue zu errichten, wäre selbst ich nicht gekommen." - George Bernhard Shaw
Comp-Freak Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 89



BeitragVerfasst: Di 17.04.07 12:56 
Danke vielmals und sorry ich hab gerade wieder rausgefunden das ich enlish getippt hab. sh.... passiert mihr immer. Nochmahls danke =).