It’d come as a shock, however not all code works on the primary attempt. (Insert shocked copyright-safe yellow electrical rodent right here)
For that reason, most built-in growth environments (IDEs) embody a debugger that enables the developer to identify and clear up points through the a number of growth iterations. That is illustrated by the next pseudocode:
whereas creating == true:
code
take a look at
debug
When creating a recreation, nevertheless, there are different points to consider, comparable to recreation stability or verifying behaviors that may be too quick or delicate to note throughout gameplay.
Give it some thought. You rigorously crafted your recreation’s problem degree, so that you need to be sure that it’s as much as the problem. However how are you going to rely the variety of enemies in a recreation once they’re shifting round and taking pictures at you?
You would possibly need to verify the sport stability. Is the variety of enemies spawned at a given prompt what you count on? Did the power-up add the injury enhance you anticipated?
These are just some of the questions you would possibly need to reply whereas testing your recreation. That is the place Godot’s debug instruments turn out to be useful, serving to you take a look at and confirm all that.
Getting Began
The instance venture is similar from the Publishing Your Godot Undertaking to itch.io tutorial. Nonetheless, for this text, the code was modified to reinforce the gameplay expertise. Sadly, the sport broke within the course of and it’s essential to discover what’s mistaken with it utilizing the debug instruments. How handy! :]
You’ll be able to obtain the venture utilizing the Obtain supplies hyperlinks on the prime and backside of this text. Word that this venture requires Godot 4.3 or newer to run.
The beginning venture has the identical construction as within the earlier article, however three recordsdata have modified: projectile.gd, enemy_ship.gd, and main_game.gd.
Now is an effective time to obtain and run the venture to evaluation the way it’s working and spot how the bugs affect the sport. The principle difficulty you’ll discover is that it’s not possible to destroy the enemy ships, though they will destroy your ship, so it’s debugging time!
Overview of the Debug Instruments
Godot has a set of highly effective debug instruments you should use to evaluation code for logic errors, or graphics for efficiency points, and even to get an x-ray of how the sport is utilizing its processing time.
Though there’s a Debug menu on the prime of the display screen, this text will give attention to the instruments accessible by the Debugger panel on the backside of the display screen, as a result of these are the instruments that collect data when executing the venture by the Run the venture button.
Debugger Panel
The debugger panel, situated on the backside of the display screen by default, is accessible from the Debugger choice on the backside of the window, to the suitable of the Output choice. The next image reveals the debug panel:
On the panel’s prime, you’ll be able to see a number of tabs. From left to proper, the tabs are:
- Stack hint: Reveals the execution stack, the context and its variables, and permits for controlling how the sport executes through the debug session. You’ll study extra about this tab later on this article.
- Errors: Reveals the error and warning messages through the recreation execution.
- Profiler: Reveals which code is operating and the way it impacts the sport efficiency. You’ll study extra about this tab later on this article.
- Visible profiler: Shows a graph displaying which code is operating and the way a lot time it takes for execution. You’ll study extra about this tab later on this article.
- Displays: Accommodates graphs of recreation data, comparable to frames per second (fps), reminiscence utilization, scene nodes, and extra. The information from the debug session is saved even after the session is over, so it’s attainable to evaluation it even after execution.
- Video RAM: Reveals an inventory of assets and the way a lot video RAM they use whereas operating, in addition to the grand complete on the prime of the panel.
- Misc: Displays and identifies the management nodes clicked throughout runtime.
- Community Profiler: Accommodates the checklist of all nodes speaking over Godot’s multiplayer API and the way a lot knowledge every one in all them obtained or despatched through the debug session.
This text focuses on tabs 1, 2, 3, 4 and 5. Nonetheless, be at liberty to look at the others utilizing the identical venture. A few of them, comparable to Community Profiler gained’t have any attention-grabbing data, although, as the sport doesn’t use the multiplayer API in any level.
Utilizing Breakpoints
After executing the sport, it is best to have observed that the principle difficulty is that it’s not possible to destroy the enemy ships. So, logically, there have to be an issue with the perform invoked when damaging the enemy ships — perhaps the ships don’t take injury when they need to?
To check if that is true, look at the perform that offers injury to the enemies: open projectile.gd and discover the damage_body perform. The perform code is:
func damage_body(physique: Node2D) -> void:
# 1
physique.take_damage(3)
# 2
create_explosion()
# 3
queue_free()
This code does the next:
- When the bullet collides with the enemy ship, it reduces the ship’s well being by 3 factors;
- An explosion seems on the purpose the place the collision occurred;
- The bullet is faraway from reminiscence;
It is a simple perform, and its logic appears to be right. How can it not be working? Wouldn’t it’s nice if there was a approach of getting a deeper take a look at how the code is working? That’s the place breakpoints are available in, permitting you to halt the code execution and dig deeper to find the issue.
Breakpoints
When analyzing code, the error won’t be apparent simply by wanting on the code; you may want to take a look on the code throughout runtime. To just do that, you’ll want to make use of breakpoints and watches, which work collectively to help and confirm what the code is doing.
Whenever you outline a breakpoint, Godot is aware of it might want to execute the venture usually as much as that time. After that, it halts execution and means that you can management how briskly the code ought to execute, and allows you to see which code executes in case of conditionals or loops.
The outline must be sort of summary now, however you’ll see that in observe it’s quite simple and useful!