For loop with nested loop problem

Started by
6 comments, last by 3Dgonewild 15 years, 3 months ago
Hello again to everyone! I've been trying to make an algorithm which makes the inventory of my game and all except one thing goes well so far. When i compile this code:

procedure TMainForm.InventoryCreate(X, Y, Width, Height, Columns, Rows: Integer);
var
  I, J: Integer;
begin

  for I := 1 to Columns do
  for J := 1 to Rows do
      begin
        Inventory[J].Column := I;
        Inventory[J].Row := J;
        Inventory[J].X := X + (Inventory[J].Row - 1)*Width;
        Inventory[J].Y := Y + (Inventory[J].Column - 1)*Height;
        Inventory[J].Z := 2;
        Inventory[J].Image := MainForm.TileMaps.Items[3];
        Inventory[J] := TInvBox.Create(DXSpriteEngine1.Engine);
      end;

end;
i get no errors, but when i execute it i get an access violation and the IDE points the line Inventory[J].Column := I; as there's something wrong there. Could someone explain me what i did wrong. (The idea is to make a an inventory consisting of (1 to Columns) Columns and (1 to Rows) Rows, X and Y are the starting position of the 1st inventory square and width and height are the width and height of 1 inventory box).
Advertisement
Why are you using Pascal or is this your pseudocode?
I've found that a couple of print statements in your loops to keep track of your loop variables or learning to use your debugger greatly helps in figuring out what these loops are doing compared to what you want them to do.
Anyways,it sounds like your arrays are off by the access violation since they are usually caused by trying to access a non-existent element!


[Edited by - daviangel on January 17, 2009 5:33:41 PM]
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Probably the variable "Inventory" is either not declared or allocated properly. Would you mind showing its' declaration?
I'm actually using delphi 6 which is pascal because it's syntax is more understandable for me than c++ and the capabilities of both languages are almost the same as the only advantage of c++ is that windows native libraries are written in c, not pascal.
var  Inventory: array[1..100, 1..100] of TInvBox;

TInvBox is the custom component i'm using for my inventory boxes (it's inherits the class TImageSprite and i give it some modifications.

P.S. I'll try to keep track on the loop variables as you said and i hope that i will see what is the problem as i don't see anything wrong in the code ....
Oh c++ has many more advantages (template metaprogramming, e.g.), but then this is not supposed to become a flamewar ;)


I am a bit rusty with Dephi/Pascal, but could it be you have to create the TInvBoxen, like (pseudo-code)
foreach c : column  foreach r : row    boxen [r][c] := new TInvBox

?
Thanks actually this post made me think what i made wrong and i can crown myself as a teh ubar noob ....
I use all the things
Inventory[J].Column := I;

Inventory[J].Row := J;

Inventory[J].X := X + (Inventory[J].Row - 1)*Width;

Inventory[J].Y := Y + (Inventory[J].Column - 1)*Height;

Inventory[J].Z := 2;

Inventory[J].Image := MainForm.TileMaps.Items[3];
but i havent actually created all of them as i put the create procedure on the last line ......
Thanks for the help and i'll definetly try to use the debugger and see if i ever create the component before i set it's parametres. :)
Welcome! Errors are supposed to happen, so that they won't happen again :)
  for I := 0 to Columns-1 do //modified  for J := 0 to Rows-1 do    //modified      begin        //also im not sure if the object must be allocated        //since the declaration looks like it does the allocation...        Inventory[J] := TInvBox.Create(DXSpriteEngine1.Engine);//swaped line        //or maybe Inventory[J] := TInvBox.Create(self); ??        //or maybe Inventory[J] := TInvBox.Create(surface/form); ??        //if( Inventory[J] = nil )//also some debugging support        //doDebug('unable to allocate image...');        Inventory[J].Column := I;        Inventory[J].Row := J;        Inventory[J].X := X + (Inventory[J].Row - 1)*Width;        Inventory[J].Y := Y + (Inventory[J].Column - 1)*Height;        Inventory[J].Z := 2;        Inventory[J].Image := MainForm.TileMaps.Items[3];        //if( Inventory[J].Image = nil )//also some debugging support        //doDebug('null image...');      end;end;


...

This topic is closed to new replies.

Advertisement