Skip to main content
gamedev.net
🔒 Locked

TD tutorial question

Started by menyo Mar 30, 2011 at 11:45 AM 2 replies 850+ views
Original Post
menyo
menyo
So i put my arkanoid game on hold for some more learning by tutorial. I found a really good one i think that skips the basics, the Tower defense tutorial. I have only done the firstpart and pretty much understand it all except for one line. Can someone explain what this does because i'm really clueless.

[color="#0000ff"]if (textureIndex == -1) [color="#0000ff"]continue;

It's inside the draw loop:


public void Draw(SpriteBatch batch)
{
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
int textureIndex = map[y, x];
if (textureIndex == -1) continue;

Texture2D texture = tiletexture[textureIndex];

batch.Draw(texture, new Rectangle(x * 32, y * 32, 32, 32), Color.White);
}
}
}


The comment by the creator tells me about all the obvious things except for that line.
Buckeye
Buckeye
Looks like prudent programming. If the map is incorrectly initialized and there's a textureIndex = -1, you'll get an exception at tiletexture[textureIndex].

Also, slight correction: the intent is not to "break" the for-loop. It continues the for-loop (skipping the Draw call) with the next y-value.

EDIT: A more robust implementation would be

if( textureIndex < minTexIndex || textureIndex > maxTexIndex ) continue;

to ensure you attempt to access the tiletexture array with only valid indices.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
menyo
menyo
Ahh i get it.

But if it skips one there would be a tile missing so an error anyway, does this make it more obvious that there is something wrong in the map array?
Buckeye
Buckeye

But if it skips one there would be a tile missing so an error anyway, does this make it more obvious that there is something wrong in the map array?

I have no idea if it would be obvious. As mentioned above, the intent is to prevent exceptions. A "skipped" tile would be the level designer's error.

Consider: you write a program to display tiles, leaving it up to a designer or the user to create data to input to your program. IMHO, the first duty of a programmer is to make life easy for the user. If a user runs your program and it crashes with a memory exception, who's the user going to blame - him/herself or you? More to the point, in fact, it's your fault because your code is not robust.

Admittedly, you can't make your code entirely fool-proof, because, when you do, the universe will create a better fool. However, a primary goal (if not THE primary goal) for coding any program is (or should be) to handle errors gracefully.

In this particular case, in addition to the index testing, a better approach would be to examine the data on input to ensure it's valid for use. Then, either inform the user that the data is incomplete and abort (not really friendly), or (more friendly) inform the user that the data is incomplete and some tiles just won't show, and ask the user if he/she wants to continue.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.