2

I'm having a bit of fun on https://blockly-games.appspot.com and have got to the last level where you can write full blown javascript (new Date().getTime(); is very handy).

Despite having the console open in Chrome (ctrl-shift-j) I can't seem to write anything to it with console.log("test"); from within the game. When I do I see:

// Starting battle with 2 players.                         compressed.js:420
// [Player] throws an error: Unknown identifier: console   compressed.js:423
// undefined                                               compressed.js:423
// [Player] dies.                                          compressed.js:416

Yet if I type console.log("hmm"); in the console I properly see:

// console.log("hmm"); 
// hmm                                                     VM1038:2 
// undefined

Same story if I try to use alert("test").

I want to debug, but the only forms of output I've found are manipulating the duck and throwing Unknown identifiers. Is there any way out of this sandbox?

Update: How do I print debug messages in the Google Chrome JavaScript Console? has some ideas for restoring console but they don't seem to work in this case.

Update: just to be clear this is what chrome looks like when experiencing this error.

enter image description here

Community
  • 1
  • 1
candied_orange
  • 7,036
  • 2
  • 28
  • 62
  • This is strange. So you are positive that the code gets executed, yes blockly somehow suppresses both console.log and alert? – Irina Rapoport May 14 '15 at 15:57
  • I'd be curious what happens if you step through alert in debugger. – Irina Rapoport May 14 '15 at 15:58
  • @ТаняТ. I run other code fine but for some reason blockly has undefined the console so I have no reference to it. I'd love to step through but how exactly would I do that when player.js (my code) doesn't even show up as a file in Chrome's debugger? – candied_orange May 16 '15 at 03:43
  • This is strange. Why does not it show up in debugger? Does HTML show up in debugger? Why don't you set up breakpoint in html, call your code from embedded (not included) javascript, and go from there? – Irina Rapoport May 16 '15 at 06:12
  • @ТаняТ. I have no trouble setting and hitting a breakpoint so long as it's in one of the blockly sources (common/boot.js etc). The problem is finding my way into my code (player.js) after that. Can you see a good place to set this breakpoint? I have a suspicion that the closest I can get to it is tracing the interpreter that runs my code. – candied_orange May 16 '15 at 06:40
  • I have a suspicion that your code is never executed. I have not experienced Blockly override these functions, and I am not sure it's even possible (have you tried overriding them yourself?) – Irina Rapoport May 16 '15 at 23:49
  • @ТаняТ. Yes my code is executed. I can scan, navigate, shoot, even read the current time but if I reference the console at all I get an Unknown identifier error. Re: overriding, I've tried everything mentioned in my update link above. Still no luck. – candied_orange May 17 '15 at 02:26

1 Answers1

0

Found a kludgey workaround:

  throw "\nYour message here.";

Displays:

//  [Player] throws an error: Unknown identifier:         compressed.js:423 
// Your message here.                                     
//  undefined                                             compressed.js:423 
//  [Player] dies.                                        compressed.js:416

Killing yourself just to log something may seem harsh but now you can write some snazzy asserts.

function assertTrue(test, message) {
  if (!test) {
    throw message;
  }
}

It works, but Lord knows I wish there was a better way.

candied_orange
  • 7,036
  • 2
  • 28
  • 62
  • Are you still getting this issue? I can make reference both `console.log` or `alert` from my console of Google Chrome either inside the Javascript code. – Francisco Romero Apr 26 '16 at 12:23
  • @Error404 Yes just tried it: "[Player] throws an error: Unknown identifier: console" – candied_orange Apr 26 '16 at 12:31
  • @Error404 "[Player] throws an error: Unknown identifier: alert" – candied_orange Apr 26 '16 at 12:34
  • Are you trying to do it in a custom example or a Google example? The second link that you provide on the question is broken so I cannot see where are you trying to do that. I am using `console.log` on my custom example and it works perfectly. – Francisco Romero Apr 26 '16 at 13:00
  • @Error404 I'm doing it in the javascript field the page provides for you to write all your code in. The field that starts off with `cannon(0, 70);` in it. I've fixed the link. You say it works for you but I can't get it to work on chrome, firefox, or IE. Could you tell me some details about how your getting it to work? – candied_orange Apr 26 '16 at 19:44
  • Mmm.. it is strange. I never had tried with this example. I always have tried with custom programs and I just set the `console.log` or `alert` inside the code that I generated inside the custom Blocks. Look that if you use `console.log` on the console **before** editing the `Javascript` tag you can do it without problems. Inside this tag it is also giving to me the same output that you are getting. – Francisco Romero Apr 26 '16 at 20:19
  • @Error404 I'm using a custom program as well. Are you telling me there is a missing step to set the `console` to something before I use it? For me using `console.log()` in the debugger works fine before and after running the custom program. But always fails when called inside the custom program. – candied_orange Apr 26 '16 at 21:32
  • No, I did not have to set anything to use the `console.log` inside the program. Can you provide the code that you have right now? Or at least a simple code that has the same behaviour that are you getting? – Francisco Romero Apr 26 '16 at 21:41
  • @Error404 see the screenshot that I added to the question. It only has two lines. – candied_orange Apr 27 '16 at 00:57
  • Yes I saw it. I refered if you have another custom program (in local storage) that you are using. Because all my custom programs are stored locally and I can use both functions on them without problems. Only in this case, maybe in this example Google it is blocking the debug options, because I had already did the same as you, and I have the same problem. – Francisco Romero Apr 27 '16 at 09:06