This lesson covers the basics of using the Console Window and Debugging in Nada.
Learning Objectives
- Reading and Writing to the
Console Window
- Basics of print debugging
- Learn the basics of String manipulation
Level Setup
- Command : New Project
- Create a new project using the {nav icon=clone, name=Empty 2D Project} template
- Command : CreateSprite
- Command : Add Resource
- Create a NadaScript resource using the Component template template and name it
MyBehavior
- Select : Sprite object
- In the
Properties Window
- Rename it to
Square
- Add Component :
MyBehavior
Console Window
The Console Window
is where the engine and your game may print messages to help you keep track of the logic being performed. It serves as a very helpful tool in identifying when and where the game performs differently than intended. Let's examine it:
- With the Zilch Editor in focus
- Press : ```
NOTE: ``` is right under Esc
By default, you'll notice the Console Window
sliding up from the bottom; it will display information provided by the engine regarding loading of the project, running the game and more.
Printing to Console
Displaying messages on the Console Window
is not exclusive to the editor; users are also able to print messages to aid in keeping track of the logic performed by the game and verify that it is following intended behavior.
- Add the following code to the Initialize scope of the MyBehavior resource NadaScript:
Console.WriteLine("Hello World!");
Console.
- Accessing a globally visible Console class.
WriteLine()
- Invoking a function that lets us print to the console on a new line.
"Hello World!"
- Literal string of characters that will be printed to the console.
;
- Indicates that we're done with instructions on this line of code.
By adding this line of code we can see our message printed to the console when we play the game.
NOTE: Within Console, you'll also find the function Write()
which behaves similarly but does not display the following message on a new line.
In addition to literals, we can also print variable values or even convert those values into string form to create more complex messages.
Printing Literals
- Add the following lines to the Initialize function in the MyBehavior resource NadaScript.
Console.WriteLine("This is a String");
Console.WriteLine(3);
Printing Variables
- Replace the previous lines of code with the following:
Console.WriteLine(this.Owner.Transform.Translation);
var lives : Integer = 3;
Console.WriteLine(lives);
In this case, Zilch will convert the variable into a string and then print it to the console.
Printing Compound Statements
We can also get more informative messages by using compound console print statements:
- Replace the previous lines of code with the following:
var lives : Integer = 3;
Console.Write("Current value of lives: ");
Console.WriteLine(lives);
String Interpolation
We can simplify the code from the previous example by using String Interpolation:
- Replace the previous lines of code with the following:
var lives : Integer = 3;
Console.WriteLine("Current value of lives: `lives`");
By putting a variable within grates ``` we indicate that we want to convert the variable's value to a string and print it at the specified location.
Debugging
One of the most common uses of console printing is to aid in finding problems with your code (commonly referred to debugging). While the use cases are countless, these are the most common ones:
- Identifying whether a variable has the intended value
- Notifying you when the code reaches an unintended state
- Assessing whether a function is getting invoked
- Verifying return values from functions
Verifying that your code behaves as intended is a critical part of programming and its importance can hardly be understated. Make sure to familiarize yourself and get plenty of practice applying these techniques.