Robocode/Eclipse/Debugging Robot

From Robowiki
Jump to navigation Jump to search

This page describes how to use the Eclipse IDE for debugging your robots.

Debugging your robot with Eclipse

In some situations, you might want to debug your robot by setting break points (e.g. conditional breakpoints), step through lines of codes and see how the values of your variables changes as the code goes along, or just check if an event handler is ever being called.

Fortunately, this is possible to do with Robocode!

Before you can start debugging with Eclipse, you must be able to run your robot from Eclipse. So if you have not already set up your project accordingly to Running your robot from Eclipse, you must do this before you are able to proceed with debugging.

Notice: You must also add -Ddebug=true to the VM argument too. This will slow your Robocode down a little, but if this flag is missing, the Robocode will freeze when it reaches the breakpoint.


In the following example we set a breakpoint in the first line of the run() method of a robot. In this case, the robot is a simple Robot where each command takes exactly 1 turn to execute before it processes the next.

Right-click in the border area of the source editor till a popup menu shows up, where you can select Toggle Breakpoint.

Shows how to toggle a breakpoint in Eclipse by first right-clicking in the border area of the source editor, and then select "Toggle Breakpoint" in the popup menu that occurs


When the breakpoint has been set, a blue bullet will be shown beside the line to break at.

Shows a breakpoint that has been set in Eclipse as a blue bullet in the border of the source editor


Now, let's try to start debugging our robot.

You start debugging by clicking on the debug button that looks like a "bug" and selecting your launch configuration. Alternatively, you could just click on the debug button, which will start debugging from the last launch configuration that was started.

Shows how to start debugging of the MyRobots project in Eclipse


When Robocode has started up, you start your robot by selecting New from the Battle menu or by using the hotkey Ctrl+N. Next, you select your robot and perhaps some opponent robots, and press Start Battle.

Notice: As soon as the battle begins, it is important that you must press the Pause/Debug button at the bottom of the battle window.

Shows the Pause/Debug button of the Battle View of Robocode, which has been pressed down


This will pause Robocode making debugging possible. If you do not pause Robocode, the game will run, and the robot that you debug will be seen as inactive when it hits a breakpoint, as it has not been active for some time. Besides, when Robocode is paused the internal time is also put on hold, which can be critical when debugging.

In this example, Eclipse should already have switched into the Debug Perspective. If this is the first time you debug with Eclipse, it might ask you if you want to switch into the Debug Perspective. Here I normally answer Yes.

In the Debug Perspective, the execution of your robot should already have stopped at our breakpoint.

Shows the current stack trace in the Debug Perspective in Eclipse when the execution has stopped at the breakpoint that has been set


If Eclipse has not stopped at our breakpoint, you should resume the battle by clicking on the Pause/Debug button until Eclipse stops at our breakpoint, and then click on the Pause/Debug button again to pause the battle.

Now we want to step to the next line after our breakpoint. Therefore we press the Step Over button or alternatively press the F5 key.

Shows the "step over" button of the toolbar for the debugger in Eclipse


Nothing happens? Well, Robocode is still paused, so we will not be able to proceed the debugging before we continue the battle. However, we do not want Robocode to rush with continuing the battle, playing lot's of turns before we get the chance of getting to the next line after our breakpoint. We just want to execute one turn at a time when debugging.

Here the Next Turn button becomes handy, which is located next to the Pause/Debug button at the bottom of the battle window. This button can only be activated when the game is paused, and is used for letting Robocode execute to the next turn of the battle, i.e. it performs a single turn.

Shows the Next Turn button of the Battle View of Robocode, which has been pressed down


In our case, we should carefully click on the Next Turn button approximately 4 times (might be more or less times) until Eclipse steps to the next line of code after our breakpoint. The reason why you have to hit the button several times is because it takes several turns until the robot has finished turning.

Shows a single step from a break point that has been in Eclipse


Now we have made a single step with our robot. So now we could set a breakpoint on the fire(1) line in the onScannedRobot() method, which is the event handler that is called whenever our robot scans another robot.

Here we don't want to make one turn at the time. Instead, we just resume the game by clicking on the Pause/Resume button.

When our robot scans another robot, Eclipse will jump right to the breakpoint set in onScannedRobot().

References

See also

Using Eclipse IDE

Robot API

Tutorials

News and Releases

Home pages