Script Debugging

## Course Introduction

Let's learn how to debug a script using the debugging function.

## Breakpoints

Script debugging begins by setting breakpoints. Let's learn how to set or clear breakpoints for a code line that may malfunction.

### Breakpoint Icon

A different icon will display depending on the breakpoint setting.

[{"Icon":"[png](176913580734767ac0b2e5a3e489e8807eeee7f71b71d)","Name":"Default Breakpoint","Description":"This is the breakpoint with default settings without other settings."},{"Icon":"[png](17691358242156cae38288cdd453ca60837ace0875254)","Name":"Conditional Breakpoint","Description":"This breakpoint has a condition entered from BreakPoints."},{"Icon":"[png](1769135835041aebe496d7e2144f0ae477332b7054478)","Name":"Continue Executing Code Breakpoint","Description":"This is a breakpoint with continue executing code set from BreakPoints."},{"Icon":"[png](1769135859948681fdb075cef4ee7ab4fed9f2f4d4ebd)","Name":"Inactive Breakpoint","Description":"This is a deactived breakpoint."},{"Icon":"[png](1769135881421982425439c154f54a9f5db45be1c38a6)","Name":"Deactivating a Continue Executing Code Breakpoint","Description":"This is a deactivated breakpoint with conditional and continue executing code settings."}]

### Breakpoint Settings

To debug from the Script Editor, you first have to set breakpoints. You can set breakpoints by pressing F9 from a code line. [png](163705967832018b0bcb305064e1ab0588a93b33ded86) png on the left side.>

### Removing Breakpoints

Select the code line with a breakpoint and then press F9 again to clear the breakpoint. If you clear a breakpoint, the game will not stop at that point when run in debug mode. [png](1637059714014a97f7af47e9e448687ce6993a387d639)

### Editing Breakpoints

You can edit a breakpoint whenever you want, including during or after debugging. The edited content will be kept regardless of whether debugging took place. Right-click to open the BreakPoint Settings window and edit a breakpoint.

[png](177319929868794f98eef19be40fb98fbc36f88291b3a)

[{"Number":"!","Name":"Enabled (Active Status)","Description":"You can activate or deactive a breakpoint. Uncheck the box to deactivate, which will keep the breakpoint but make it inactive."},{"Number":"!","Name":"Condition (Conditional)","Description":"Set the breakpoint to only function when the specified conditional is true. For example, if the x>1 conditional is input, the breakpoint will function when variable x is larger than 1."},{"Number":"!","Name":"Hit Count","Description":"Can be set to only end the execution when a breakpoint lands a pre-set number of times. Both numbers and formulas can be used. For formulas, operators (>=, >, <=, <, ==, %) may be used."},{"Number":"!","Name":"Log Message","Description":"Log messages can be used the same way as log() functions.For example, if you want to out put the value of variable x, \"x= \", x will function the same way as log(\"x= \", x). The log that's output when debugging is output together with the [Breakpoint Log] header."},{"Number":"!","Name":"Continusouly Execute Code","Description":"Activating continuous code execution will make it so that the world will continue to run even after it reaches the breakpoint. Can only be used when entering a log message."}]

> Tip If the Condition and Hit Count are set together, Condition is evaluated first. The Hit Count only increases when the Condition is true, and the breakpoint or log output only functions when the set Hit Count condition is fulfilled.

#### Hit Count Operator

Creators can precisely adjust the hit count condition of choice using various operators. There are 6 operators that can be used in Hit Count.

  * `>=`, `>`, `<=`, `<`, `==`, `%`



[{"Example":"5, >= 5","Desc":"Always stops execution for the 5th hit."},{"Example":"> 5","Desc":"Stops execution from the 6th hit."},{"Example":"<= 5","Desc":"Only stops execution between 1st–5th hit."},{"Example":"< 5","Desc":"Only stops execution between 1st–4th hit."},{"Example":"== 5","Desc":"Only stops execution on the 5th hit."},{"Example":"% 5","Desc":"Stops execution for every 5 hits. (5, 10, 15...)"}]

### Deactivating Breakpoints

Left-click to deactivate breakpoints. Deactivated breakpoints will be shown as [png](1769135859948681fdb075cef4ee7ab4fed9f2f4d4ebd), [png](1769135881421982425439c154f54a9f5db45be1c38a6) and they can be reactivated from the BreakPoint Settings window.

> Tip Remove the breakpoint by double left-clicking.

### Managing Breakpoints

When using breakpoints in multiple lines and various scripts, it can be difficult to remember where they are all placed or if they're even needed. You can use the Breakpoints panel to see all of the set breakpoints. Click and open Panels - Debug - Breakpoints in the navigation menu. [png](16875077319895fd3c24546fb466abb6f59901a6fd11d)   
The Breakpoints panel provides the script name, function name, and line information. [png](16370597948003bc1552ec1b4445ebb17f3c2e15f2989)

[{"List":"Entry","Explanation":"Script name (e.g. script component or script logic with breakpoint)"},{"List":"Method","Explanation":"Name of Method with breakpoint within Entry"},{"List":"Line","Explanation":"Position of line with breakpoint within Method"}]

In the Breakpoints panel, you can search for breakpoints, but also delete them or move to that location as well.

### Moving to a Breakpoint

Double-click the breakpoints list, or select Move from the context menu to move to the position with a breakpoint. [png](1637059803373a398dd6a85c944c8bb9223a41618be5d)

### Deleting Breakpoints

You can delete a breakpoint by clicking Remove from the right-click menu of the breakpoint you'd like to delete. [png](163705981310639ae31b71811480d9462f92fd1c649dc)

## Debugging

When you're done setting breakpoints in your script, execute the world in debug mode to check the code.

### Starting a Debug

You can start debugging with the [Debug Start/Stop] button [png](163453034389022f3433c55154836a7b16c2f001ec1b2). [png](1769515771503d8b1d1a67a584d88aa17c7df2e8f7ee7)   
When you start debugging, the game plays the same as when you start the game, but when the code with a breakpoint is executed, play stops and you can see the state of the code. [png](1656401357743c56f6df990bf4b47b085b20b7c6838b1)

### Understanding Code

During debugging, you can check each variable or value assigned to a property. [png](1634524682293d425b6b49f234132a341a80cdc17a080) You can see detailed assigned values by using tools like Watch Expression. More simply, you can also hover the cursor over the executed code line's variables or properites to view the allocated value, execution space, type, and more through the tooltip. [png](165640134890606dadbdca6f74d999f16111fbdc8599e) (Ex: You can see the delta value passed as a parameter.)

### Stopping Debugging

You can stop debugging by clicking the [Debug Start/Stop] button or [Start] button. [png](1769515795914e30f405130b04c13a36ffc897f13a8cb)

### Executing Code During Debugging

What should we do if there is no problem with the code in the breakpoint, or if we want to check the code below as well? We can't set breakpoints to all code lines we'd like to check, and it would be inefficient to start/stop debugging to set and clear breakpoints each time. When play has stopped at the breakpoint, you can execute the code that comes after the breakpoint. You can check the state or flow of the code with executing the code by unit through procedure unit execution, code execution step by step, etc. To execute codes by unit, you can use the following buttons next to the Start/Stop button after debugging has begun. [png](17695158074742cea6a4314164861a17aa6f632d4a924)

[{"Button":"[png](163531428695222092623c5ee4bbdb6c765b0257765d4)","Function":"Move to next breakpoint","Shortcut":"F5","Explanation":"Run code from current line to next code line with a breakpoint."},{"Button":"[png](16353142973766005f7a18f6241e0853fcdb4b117d794)","Function":"Execute by procedure","Shortcut":"F10","Explanation":"Execute the code line by line.If there's a function in the focused line, move on to next line, assuming the function has been executed."},{"Button":"[png](1635314349045496e747d90b649a7aa14b0fb1388f0af)","Function":"Execute code by stage","Shortcut":"F11","Explanation":"Executes code line by line, but if there's a function in focused line, enter the code to execute codes within the function line by line."},{"Button":"[png](163531436027902cc8e8685d04d26a2a7d6659e0ed05d)","Function":"Exit procedure","Shortcut":"Shift + F11","Explanation":"Finish calling the function currently running, and move to the location where it was called."}]

## Watch Expression

You can hover over a variable or property to check individual values, but it gets inconvenient to watch the constant change in certain variables on each line. To avoid this, we can register a property or variable declared within the function to Watch Expression. By doing this, we can convienetly keep track of values each time the code is executed.   
Click and open Panels - Debug - Watch Expression in the navigation menu. [png](16875080353841809a26bb9494dbc9cc9bf775f4ff075)   
During debugging, write the variable or property name to Add Expression to figure out its value.

> Tip In the case of variables declared in the currently focused function, the variable name is registered, and in the case of property, it is registered in the form of self.property name.

[png](163531441012044c1f499c222457c8428da2cff7ffeb4)

Once a variable or property is registered to Watch Expression, you can run codes line by line to track the change in values.

[{"Before executing code in function":"- Starts debugging, focuses on the first line of NewMethod3 function which has breakpoint.\- Receives \"Debug12\" as NewMethod3 function's parameter.\- Variable text has not been declared yet.\- Default value 0 is assigned to Property.","After executing code in function":"- Uses F11 to run code up to the 4th line.\- Declares variable text and assigns \"Debug123\".\- Adds 1 to Property value."},{"Before executing code in function":"[png](16370600155057ca13181b27649018ee5ba886944a710)","After executing code in function":"[png](16370600271603294fb67b3024471b88820dda2b0b976)"},{"Before executing code in function":"[png](163531445300862c1a06ecc354207b15e5206b731a731)","After executing code in function":"[png](1635314465563af8446e05b504689aa99b9ee139f7e54)"}]

## Call Stack

Call Stack is used to check where a function currently being debugged was called from. Click and open Panels - Debug - Call Stack in the navigation menu. [png](168750821078566a306d100c6405e8845e38ba39689a8)

Like the example below, if you're debugging the inside of `Method3` from a script that calls the `OnBeginPlay` function, `Method1`, `Method2`, and then `Method3`, you can use Call Stack to check the execution space and the sequence called up to `Method3`. [png](16353144857503a4fa032527a41bd85f26876272b25a1)