Basic debugging

From Screeps Wiki
Jump to navigation Jump to search


Between Javascript and the screeps, you have several options to determine where errors are in your code. While some can be returned in the game's console, others you can do visually with various visual elements screeps provides.


Console.log()

console.log() will print to the game's console, you can add various variables, strings and the like to the printout. If you want to see plain-text versions of objects (as they would print as [Object object] else wise) you can use JSON.stringifiy() to convert the object to plain text which makes it much easier to determine many things.


Try Catch & Stack

try{ someCode }catch(error){ some backup code } Try Catch allows you to error handle without having to fully break your script. For example, if you are trying out a new spawning method for your creeps, but are unaware if it will fully cover all cases, you can wrap it in a try catch with your old code or older version you know to be stable as a backup, that way your script will keep running in the event of an unforeseen error as elsewise unhandled errors can easily kill colonies of creeps.

To get the lineNumber/stack you can call .stack on the error and print it to the console. For example, try{ someCode }catch(error){ console.log('Error stack: '+error.stack);} should print you the stack with the line number to better help determine the source of the error.


Unhandled Errors

Errors that cause your script to stop executing, these should provide a stack/line number you can trace down into your code to determine where the error is being caused.


Visual Debugging

Screeps provides some powerful visual feedback tools for debugging your creep's actions or actions of your script. These tools allow the user to see in 'real time' the actions or states of their creeps to determine flaws in logic, code errors or the like.

Creep.say()

Creep.say() will display a speech bubble above the creep for the tick it is called, this allows for short-strings of text to be printed or even emoji as it accepts any valid unicode characters. This is very useful when trying to determine the 'state' or action a creep is taking, as a user can asign different emoji or text to different actions to determine where the creep is in its logic. It is also useful for marking different creeps, as you can use different strings or emoji to 'mark' different creep roles or types to easily tell which creep is which. These are just a few examples of course, there are many other uses for creep.say().

RoomVisuals

RoomVisuals are a powerful tool that allow you to display various graphics and visual elements onto a room for the tick that they are called. These can vary from making visual lines for pathfinding, displaying CPU useage, status of buildings or even your own dashboards rendered in a room. How you use them in your colony is up to you, however when you are looking for something very flexible with lots of visual feedback its hard to beat roomVisuals.

MapVisuals

Like roomVisuals, mapVisuals allow you to display various graphic and visual elements but this time on the map overview itself. This can help with a variety of scouting and observer debugging, or longform pathing as you can see an overview of multiple rooms at a time.


Memory Viewer

Along with your script/console, you also have a Memory tab which is the memoryViewer. This allows you to check into or even create various memory objects without having to add checks into code, or print it out to the console. Once a path is being viewed, you need to close it and reopen to have it update (they update every tick unless actively viewed), and modifications are submitted next tick. Be careful adding or editing large-chunks such as from the root or room/all creeps level, as the data was retrieved the tick you opened it and may have since changed, submitting the change will revert the memory to whatever the data you had open was, not what it may be now.

Grafana

A popular tool used by many players, you can either set up your own https://github.com/screepers/screeps-grafana or use a hosted service provided by screeps+ https://screepspl.us/services/grafana/ This tool allows for the various display of graphs using data from the game, as you can store the data on your server / Screeps+, it also allows for long-time tracking of various things such as CPU, market prices, ect, to allow you to find some flaws or challenges that may elsewise be hard to spot in the short-term.