# Real-time Debugging Techniques

There's nothing quite like the routine of tweaking a variable, compiling code that takes a few minutes, executing the code, realizing that your tweak was in the wrong direction and repeating the process all over again.

I would like to start interacting with my game logic while a session is in progress. What are some of the ways you've handled doing this?

I would be open to hear solutions for iOS, C/C++ and C#/XNA.

Just discovered this for iOS Developers -

AsyncSocket
You set up a server in your application and connect to it via terminal. You can then pump over remote commands and start manipulating variables in real-time. Pretty slick.

Integrating a fully interpreted language can allow you to rewrite game elements very, very easily on the fly.

Python, Lua, etc. can all be utilized expressly for this. Angel Engine probably can serve as an example.

Hot loading is another element to check out (basically, an event is fired off telling you an asset has changed--a config file, an art element, etc.--and you run it through a series of conditioners that modify it for the final format before replacing the existing with the new.

I've worked with a rapid prototyping engine that used hot loading and it was a pleasure.

Well as far as C++, C#, and C# with XNA are concerned, Visual Studio has excellent debugging tools--pause the application in real time at any point in the code, see the values assigned to variables at any time, step through function calls, analyze the call stack, and more. In addition, tools like Pix and the CLR Profiler make development with DirectX (Pix) and CLR based languages (CLR Profiler) great.

In addition, one especially useful part of working with games is that we have this giant area to write debug text, graphics, etc to. Realtime graphs of memory usage, number of objects/vertices drawn, framerate, etc are common things to draw to the screen.

• While VS is an outstanding development tool, normal debugging takes time -- hunting down breakpoints, stepping through code, changing variables, restarting the flow. This question is looking for an immediate way to do things like move an interface component, or tweak a boss's attack ratio, etc. – David McGraw Jul 21 '10 at 17:41

In C/C++ a simple trick is to put your tweakable into a static variable, trigger a breakpoint, change the static in the debugger and it remains persistent until exit.

• Make it a static variable inside a function though. If it's a global static Visual studio might not be able to show its value. – Kaj Sep 12 '10 at 17:52

HTTP Logging is a huge help, and not difficult to get up and running. You can even use Google App engine as a quick way of getting this running. Data is always useful.

Scripts & XML these can be reloaded on the fly and require no reset. Allow them to fail without bringing down the game. This gives a lot of power to artists, testers and designers.

In-Game Debug Menu A simple in game debug menu which you can spawn as a child and freeze the game with both engine sliders and game sliders. Allowing you to tweak key numbers at runtime and maybe perform one or two debug functions, or info dumps.

I also love ASYNC sockets as mentioned in David's answer

Easy to USE Your games team must be able to add to these tools and use them very easily, or they just won't get used.

I know Sean James already said it. But seriously, Visual Studio (specifically the debugging tools) is terrific for this.

I don't use C++ much these days, so I'm not sure about how well these apply there. But for C# (including in C# Express) you have:

• Edit and Continue - where you can make wholesale changes to your game logic.
• The Watch Window - where you can see and modify variables with ease.

And this might blow your mind (it blew mine when I found out about it):

The Immediate Window is actually kind of hard to find (it's in the Debug/Windows menu).

The only major downside to using the Visual C# debugger is that it doesn't like changing const values. So I usually just make my gameplay-related values static while I'm tweaking them.

(Also: having two monitors helps a lot.)

Now I must admit that the above method involves pausing your application - which can be unsatisfyingly slow for a few particularly fiddly things.

On these rare occasions, what I do (in XNA) is to simply hack in a bit of code (probably using Edit and Continue, as above) to grab Keyboard.GetState().IsKeyDown() (actually I have an easier-to-type wrapper for this) and adjust the value by keystrokes. Anything more complicated is not worth the effort.

In practice what I usually find vastly more important is being able to visualise (rather than modify) values in real-time. For this I have a nice little class that can buffer lines and text to be drawn at the end of the frame. It's also useful for quick prototyping.

(And, once again, it's nice to be able to "Edit and Continue" these visualisations in at runtime.)

(source: andrewrussell.net)
(from here)

I'm afraid I don't have "nice" source to publish at the moment (maybe later). But it's basically just a list of lines (for this round line library) and strings (for XNA's built-in SpriteBatch). Just make it public static somewhere, and draw everything with an appropriate transform so that everything appears in "world space" (and then clear the lists for the next frame).

Writing a simple reflection system for C/C++ is fairly trivial. This way you can browse through all your variables in game (be it with a network tool, keyboard or joypad) and change them to your heart's content. It works for me, but all the above answers are equally valid.