1

I'm attempting to use Blockly to do a "make your own game" sort of thing, and for the most part it works. However, when trying to run code generated (by Blockly's own pre-defined function generators) by declaring a function and calling it, I consistently get told that the function isn't defined, no matter what it is or what it contains.

I'm grabbing and running the code like so:

var code = Blockly.JavaScript.workspaceToCode();
try{
    eval(code);
} catch (e){
    alert(e);
}

Which is how the demos provide on Blockly generate code. I've also echoed the code out elsewhere in the page and it looks right to me:

function whatINameIt() {
    //code I give it
}
//rest of code

Is this something to do with how eval works? The only thing I can think of is that for some reason it's "evaluating" the function code but not adding it as something callable. If that's the case, is there an alternate way I should run the code string Blockly gives me?

Emp
  • 21
  • 6

1 Answers1

0

Maybe you are creating an infinite loop. To solve it, you will have to add the following lines as the documentation of Blockly says:

window.LoopTrap = 1000;
Blockly.JavaScript.INFINITE_LOOP_TRAP = 'if(--window.LoopTrap == 0) throw "Infinite loop.";\n';
var code = Blockly.JavaScript.workspaceToCode(workspace);

Also, if you have created custom Blocks, as it seems in your question, make sure that you are returning the code that you are creating in all of them. If you do not return it, the workspace will not know that these Blocks want to do whatever.

It would be great to help you if you provide the Blocks code that you are creating/using and the code that you are retrieving from your other function.

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167