Sign in to follow this  
Alex131

For loop with nested loop problem

Recommended Posts

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[I][J].Column := I;
        Inventory[I][J].Row := J;
        Inventory[I][J].X := X + (Inventory[I][J].Row - 1)*Width;
        Inventory[I][J].Y := Y + (Inventory[I][J].Column - 1)*Height;
        Inventory[I][J].Z := 2;
        Inventory[I][J].Image := MainForm.TileMaps.Items[3];
        Inventory[I][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[I][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).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 ....

Share this post


Link to post
Share on other sites
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

?

Share this post


Link to post
Share on other sites
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[I][J].Column := I;

Inventory[I][J].Row := J;

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

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

Inventory[I][J].Z := 2;

Inventory[I][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. :)

Share this post


Link to post
Share on other sites

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[I][J] := TInvBox.Create(DXSpriteEngine1.Engine);//swaped line
//or maybe Inventory[I][J] := TInvBox.Create(self); ??
//or maybe Inventory[I][J] := TInvBox.Create(surface/form); ??
//if( Inventory[I][J] = nil )//also some debugging support
//doDebug('unable to allocate image...');
Inventory[I][J].Column := I;
Inventory[I][J].Row := J;
Inventory[I][J].X := X + (Inventory[I][J].Row - 1)*Width;
Inventory[I][J].Y := Y + (Inventory[I][J].Column - 1)*Height;
Inventory[I][J].Z := 2;
Inventory[I][J].Image := MainForm.TileMaps.Items[3];
//if( Inventory[I][J].Image = nil )//also some debugging support
//doDebug('null image...');
end;
end;





...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this