Tile Selection System Crashing!

Started by
10 comments, last by Palidine 13 years, 1 month ago
Hello GameDev.Net! I'm currently trying to to a tile-based editor game. Currently it is *supposed* to load a .txt file, and set the tiles based on the numbers loaded. I dont think that works, but thats for another time. Right now, whenever I scroll right or left using the ingame right and left buttons, the game crashes. There is even code that is supposed to prevent it from crashing. I can't figure it out. I would appreciate help.

The tile.h and .cpp are for the tiles themselves. the world.h and .cpp draw the tiles, load the tiles, and update the tiles, as well as do all that for the HUD. I know i should be using vectors, any suggestion on how to use vectors in this context is helpful too. I also know that the load function probably doesnt work, and there is no save function, and I should have a console, etc. General suggestions are cool too, i guess, but problem first O.O

[attachment=1433:HUDEditorV2.zip]

THANK YOU SO MUCH!
Advertisement
I just took a quick look inside world.cpp and I see a lot of this:

for(int x=0; [color="#ff0000"]x!=twidth; x++)

You are probably running into an "array subscript out of bounds" error or something similar in wording meaning the same thing.

Instead of "!=" in your for loops try using "<" since arrays are 0 based and with your current logic an array that hods 5 elements will try to access array[5] which would be out of bounds as the last element would be array[4].

There are lots of tutorials on arrays on the net - just google "c++ arrays" and quite a few tutorials pop up. Arrays are very common in game development and especially in tile based games as they are a basic building block everything else builds on top of so the more you know about arrays the better.
Evillive2
yeah you are probably right. I thought of an array out of bounds thing, but the odd part is that its also looping over to the other tile set. If you look, you will see there are two arrays in thw world class: theres groundSet and objectSet. they should each have different surfaces loaded to them. Its wierd that you can get to them all without changing layers.
@evillive2 -- using "[font="Courier New"]i != end[/font]" is logically fine. In some coding conventions it's actually preferred over "[font="Courier New"]i < end[/font]" (because if i is a complex iterator, then [font="Courier New"]!=[/font] may be faster than [font="Courier New"]<[/font]).

@walkingbush -- run your code from the debugger and tell us which line it crashes on, and what the crash message is.
Well,

Its line 231, world.cpp. Its a segmentation fault, which im guessing means i dont have urface to draw at the address im calling or something. Ive encountered these very unhelpful error messages before and have always gotten through them, but this time I cannot seem to find out why it will not stop going left or right when the next image will either be out of bounds or empty. :\

I tried to create some sort of blocker like so:
if (bleft.update(event, Player))
{
if (curSet==0)
{
if (!groundSet[curGround-1])
{
curGround=0;
scurrentTile = groundSet[curGround];
printf("curGround = 0/n");
}
else
{
curGround-=1;
scurrentTile = groundSet[curGround];
printf("curGround -= 1/n");
}
}

printf("Custom button input read. B1\n");

if (curSet==1)
{
if (!objectSet[curObj-1])
{
curObj=0;
scurrentTile = objectSet[curObj];
}
else
{
curObj-=1;
scurrentTile = objectSet[curObj];
}
}

printf("Custom button input read. B1\n");
}

(This is for the left gui button) See where it checks if the next one up the list is empty? This should be goin, "Oh dang the next one is empty okay stop at 0 or here or whatever," instead of "Okay lets keep going and crash walkingbush's program,"

Im very confused and its irritating too. :\

Also, != is faster because it just checks one value instead of every value above or below it right?

I thought of an array out of bounds thing


Why are you trying to think of anything? Where exactly is it crashing when you are running it in your debugger? When it crashes you should see all the variable values and be able to tell instantly what the problem is. If not then step through the code before the crash point and see what goes wrong.

You've reached the point of complexity in programming where if you aren't using a debugger you're wasting your time :)

-me
See all the variables?! That would certainly be helpful! Im using Code::Blocks. How do I do that?

Also, != is faster because it just checks one value instead of every value above or below it right?


No. It's definitely not checking every value since that would be an infinite set of numbers. All comparison operators compare exactly 2 values: those on either side. They're equivalent enough that it doesn't matter. Use the correct one. Even if there were a speed difference it would make no difference in the speed of your code: your algorithms will almost always be the bottleneck, not tiny things like this. And really you should never optimize without using an instrumented profiler.

-me

See all the variables?! That would certainly be helpful! Im using Code::Blocks. How do I do that?


http://wiki.codeblocks.org/index.php?title=Debugging_with_Code::Blocks

You want to set a "breakpoint" at the point in your code where you think things are going wrong. There will be a learning curve. Just stick with it. It will change your life ;)

-me

This topic is closed to new replies.

Advertisement