0

I'm trying to make an array of an array to get data (which I have previously added) calling it like "GameMap[i, j, k].Items[m].Index" or "GameMap[i, j, k].Count"

My type part in delphi looks like this:

type
 TItemRec = record
 Index: Integer;
 Volume: Integer;
 Count: Integer;
 Id: string;
end;
TMApRec = record
  ID: LongWord;
  Count: integer;
  Order1: integer;
  Order2: integer;
  Order3: integer;
  Order4: integer;
  Order5: integer;
  Order6: integer;
  Order7: integer;
  Order8: integer;
  Order9: integer;
  Order10: integer;
  Items: array[0..9] of TItemRec;
end;

TMap = class

GameMap : array[0..8,0..14,0..$12] of TMapRec; 

and for example, if I do now:

procedure TMap.Update;
 var
  i,j,k,m: integer;
 begin
  i:=0;
  while i < 8 do
  begin
   j:=0;
   while j < 14 do
   begin
    k:=0;
     while k < $12 do
     begin
      m:= 0;
      while m < 10 do
      begin
       showmessage('asdf');
       GameMap[i,j,k].Items[m].Id:= (inttostr(i)+' '+inttostr(j)+' '+inttostr(k)+' '+inttostr(m));
       showmessage((GameMap[i,j,k].Items[m].Id));
       inc(m);
      end;
      inc(k);
     end;
     inc(j);
    end;
    inc(i);
   end;

it will only show the first showmessage('asdf'), but then it crashes here you have the full code if you want it highlighted http://pastebin.com/xfL94QXU

Thanks again for your time guys

5
  • You really need to put more effort into describing the problem. "It isn't working right, usually it bugs" and "doesn't allow me to take" aren't really helpful; they don't mean anything to anyone but you, because we can't see your screen or read your mind. Please remember that we have zero information about your problem except what you tell us here; if you don't clearly explain the problem, it's extremely hard to figure out how to solve it. Commented Apr 22, 2013 at 20:18
  • Perhaps you forgot end, in the other words, it works for me, eg: GameMap[0,1,$2].Items[3].Id := 4; Please reformat your post to show code correctly. Commented Apr 22, 2013 at 21:37
  • check first post please, I have written it again Commented Apr 23, 2013 at 0:37
  • Did you create a TMap object? You're still not showing all the relevant code. Commented Apr 23, 2013 at 7:48
  • "it crashes here" is no good at all. You need to supply a complete error message. Commented Apr 23, 2013 at 8:05

2 Answers 2

1

The only way in which your code can produce a run time error is if you failed to instantiate an instance of TMap.

I suspect that the code that calls Update looks like this:

var
  Map: TMap;
begin
  Map.Update;
end;

This will fail because Map is not initialized. Fix it like this:

var
  Map: TMap;
begin
  Map := TMap.Create;
  try
    Map.Update;
  finally
    Map.Free;
  end;
end;

As a more general piece of advice I strongly recommend that you read about how to create a Short, Self Contained, Correct (Compilable), Example.

Sign up to request clarification or add additional context in comments.

1 Comment

:O thanks David, you were completely right, didn't remember that!!! thank you guys for helping me =D
1

Declare a type for your Items record first:

type
  TItemRec = record
    Index: Integer;
    Volume: Integer;
    Count: Integer;
    Id: Integer;
  end;

Now use that type in your GameMap:

GameMap : array[0..8,0..14,0..$12] of record
  ID: LongWord;
  Count: integer;
  Order1: integer;
  Order2: integer;
  Order3: integer;
  Order4: integer;
  Order5: integer;
  Order6: integer;
  Order7: integer;
  Order8: integer;
  Order9: integer;
  Order10: integer;
  Items: array[0..9] of TItemRec;
end;

2 Comments

On the path of clarity, it is worth to "extract" the outer struct too.
okey, I have written again the first post.. but still bugged :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.