Sign in to follow this  
walkingbush

Tile Selection System Crashing!

Recommended Posts

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!

Share this post


Link to post
Share on other sites
I just took a quick look inside world.cpp and I see a lot of this:

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

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.

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='walkingbush' timestamp='1297920118' post='4775261']
I thought of an array out of bounds thing
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='walkingbush' timestamp='1297921170' post='4775266']
Also, != is faster because it just checks one value instead of every value above or below it right?
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='walkingbush' timestamp='1297921276' post='4775269']
See all the variables?! That would certainly be helpful! Im using Code::Blocks. How do I do that?
[/quote]

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

Share this post


Link to post
Share on other sites
is that what you meant? Ive used breakpoints. I thought you meant like, It has a box with all the variables ive ever declared and their current values.

Breakpoints are pretty awesome though :D

Also, evil, i just got what you meant. Let me check.

EDIT: Yeah, you were right, but i set the arrays to 11 and the twidth to ten XD Its got something to do with the hud and drawing the current tile...

Share this post


Link to post
Share on other sites
[quote name='walkingbush' timestamp='1297921744' post='4775272']
is that what you meant? Ive used breakpoints. I thought you meant like, It has a box with all the variables ive ever declared and their current values.
[/quote]

When you hit a breakpoint you can inspect the values of all the variables that have been set so far that are accessible to the current scope.

-me

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