0

and thanks for taking the time to look at my question. I'm still a beginner (relatively, a few months of experience) at programming, and I'm trying to make a simple game in processing. I want to have an eval function so that I can evaluate some typed in strings. This is just a simple game, so security isn't a problem, and I don't mind if certain things that get typed in will create unusable code because the code inputted will only be creating new shapes and not messing with existing code. I tested out the eval function by doing this.

eval("background(0);")

but the background obstinately stays white. Am I doing something wrong? (as I suspect) This is my first time dealing with evals.

Thanks for you help!

  • if your code uses eval, you're doing things wrong, and I say this both as a programmer and as the maintainer of Processing.js -- what are you actually trying to do that you believe requires `eval`? – Mike 'Pomax' Kamermans Mar 31 '14 at 02:01

1 Answers1

0

I can confirm that the eval("background(0);") approach does not work for Firefox, IE or Chrome. background(0); does work. Maybe you can work around this by taking apart the string and looking at the specific information there. I was able to do this with the followin example:

<!DOCTYPE html>
<html>
<body>
<script src="processing.js"></script>
<script type="application/processing">
size(600,600);
var str = "background(100);";

if (str.indexOf("background")==0){  //does the string start with "background"
    str = str.replace( /\D+/g, '');//number extraction found on http://stackoverflow.com/questions/10003683/javascript-get-number-from-string
    background((int)str);  //use just the number
    text(str,10,20);
    }
</script><canvas></canvas> 
</body>
</html>
Scott
  • 51
  • 6