6

Is there a way of getting screeps code to print strings to the console (or anywhere really) for simple debugging purposes?

bitbutter
  • 580
  • 5
  • 15

4 Answers4

10

You can use standard console.log method for that.

artch
  • 4,487
  • 2
  • 28
  • 35
  • 1
    I tested this yesterday and it wasn't working. I assume you're adding patches all the time so it would be awesome to know what is changing as it changes :) – Cogito Nov 22 '14 at 03:23
4

I use the following to print objects to the console:

console.log(JSON.stringify(<myVariable>))

SScholl
  • 598
  • 6
  • 19
3

I can't find how to do this in Docs. Had to write something like this:

module.exports = function () {
   var log = Memory.log;
   if(log === null || log === undefined){
       log = Memory.log = [];
   }

   var parts = ["["+Game.time+"]"];
   for(var i in arguments){
       parts.push(arguments[i]);
   }
   var msg = parts.join(" ");
   log.push(msg);
   if(log.length > 10){
       log.shift();
   }
}

Will be grateful if someone can provide a better solution.

1

Sometimes when you do console.log you get unhelpful string representations of objects, something like like "[Object]".

If you want to drill down into the object and check out it's properties, the easiest solution is to open your browser's console. The devs made it so any console.log in your script will also reach the standard browser console. I believe it works in all major browsers.

Andrew
  • 91
  • 1
  • 2