# Best way to debug a code in the game loop?

## Recommended Posts

I have this problem where it takes me hours of just figuring out the bug on the game I am trying to do.

Most the debugging method I do is that I just put a

Debug.Writeline()

on code that I suspect is the culprit. This is obviously becomes confusing when the project becomes larger and theres more class involve and passing things to other class.. The other way is that I use a break point. But what if the bug happens just in the middle of the game? It will take a thousand loop just to get there and break point is not really a good option for that i think.

For all game programmers out there. What is your best way of figuring out the bug in your project?

PS: I am using C#/XNA and Visual studio 2013

Edited by LetsDoThis

##### Share on other sites

I don't see any language or platform tags on your post, but some debuggers allow you to set conditional break points, which will only trigger if your code goes through the statement and if a variable has a certain value. Some environments also allow you to create a statement that will trigger a debug break, like __debugbreak() in MSVC. Otherwise, most semi-modern loggers allow you to have logging hierarchies, and enable different logging levels for different branches of the hierarchy. Ex: by default you might have things logging at warning, but for the suspected subsystem log at info or debug.

##### Share on other sites

In many languages you can also create crash dumps, where if an assertion fails on a user's PC, they can send you the crash dump, and you can open it in your debugger as if it's just hit a breakpoint on that line, and use the debugger to see exwctly to what's gone wrong.

Will this work as well if say the variable is set to false instead of true?  Cause whenever i hear crash dumps i think its mostly the program crashes cause of some random exception?

and uhm do you have a link on how to do this? Thanks

##### Share on other sites

Will this work as well if say the variable is set to false instead of true?

assert breaks on an expression that evaluates to "false."

If you want to break when something is true, instead, use:

assert( some_condition != false );

or simply:

assert( !some_condition );

i think its mostly the program crashes cause of some random exception?

Ain't no such thing as a "random" exception**. Something in the program caused the condition - reading or writing beyond the end of a buffer, out-of-memory, a combination of conditions that the programmer didn't anticipate, failure of the application to determine if the user's hardware is compatible with the demands of the program, etc. Most of those situations can be avoided with good programming practice.

** discounting lightning strikes, EMPs, coffee spilled on the keyboard, etc.

Edited by Buckeye

##### Share on other sites

PS: I am using C#/XNA and Visual studio 2013

As an addition to above suggestions there is also one more thing you can do. In menu Debug->Exceptions be sure to check also "Thrown" column of relevant exceptions (it is unchecked by default). That way it will break at the time any exception is thrown and you will be able to debug the issue.

##### Share on other sites
I tend to do the following:

Use the built in debugger to trace execution and break on error,

Use a wrapper class around OutputDebugString() to give it iostream like behaviour and use it judiciously in debug builds,

Use assertions as much as possible to ensure what you think should be happening is actually happening.

I have also been known to put a regression test suite into my app or game so you can run it in some special way e.g. With a command line parameter and rather than entering the main loop, it initialises all systems and performs tests with known parameters, logging which functions give unexpected results. You can tie this into the release process to check your new features haven't broken old ones. Very strongly advised.

##### Share on other sites

I have also been known to put a regression test suite into my app or game so you can run it in some special way e.g. With a command line parameter and rather than entering the main loop, it initialises all systems and performs tests with known parameters, logging which functions give unexpected results.

Im lost on this part. Can you give me a simple example or link to explain just how this works or how to do this?

##### Share on other sites

PS: I am using C#/XNA and Visual studio 2013

As an addition to above suggestions there is also one more thing you can do. In menu Debug->Exceptions be sure to check also "Thrown" column of relevant exceptions (it is unchecked by default). That way it will break at the time any exception is thrown and you will be able to debug the issue.

Thanks for the tip.. Looking at it. I think the only check box I check is the Common language runtime exception

##### Share on other sites

I have also been known to put a regression test suite into my app or game so you can run it in some special way e.g. With a command line parameter and rather than entering the main loop, it initialises all systems and performs tests with known parameters, logging which functions give unexpected results.

Im lost on this part. Can you give me a simple example or link to explain just how this works or how to do this?

It's simpler than it first sounds.

Simply write down for each function what you expect it to return when called with various parameters. Choose some sane and some insane values and some edge cases which are the extreme inputs.

Then have a function that calls each one, passing it the input you wrote down and checking for the one you expect back. Any that don't return what you expect are broken. It is basically like asserts except you can use it as part of your automated testing to see if your game is broken in some subtle way.

You would then put some code in your game so that if someone did:

Mygame.exe /testsuite

It runs this set of calls instead of starting the game.

The usefulness of this hinges on making sure it does tons of tests of the entire functionality from renderer to sound and that somehow, you can tell if the function is broken without asking the user so it can be automated.

If you look in my open source (non game) project inspircd you will be able to find a C++ example of his, drop me a line if you get stuck :)

##### Share on other sites

Can you give me a simple example or link to explain just how this works or how to do this?

This is just exercising parts of your code automatically and verifying that certain inputs equal certain outputs. If the tests target a specific section of code, they are generally called unit tests. For example, if you have code that reads/writes "save games", your test might supply a save game file, load the game state from it, then save the game state, and verify the new save game file is identical to the old. Or if you had code that determined if two triangles intersect, you'd set up known test cases to verify that the results are true or false, whatever you expect.

If your game supports a replay system (where say all inputs are recorded so that a game can be replayed), then you could use this replay to reproduce bugs that other people have hit. Of course, that requires a lot of planning up front.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628356
• Total Posts
2982252

• 10
• 9
• 13
• 24
• 11