- Home /
Help with eval()
So, I asked a similar question the other day, related to the same subject, asking how could I, as a general concept, input a string in my code, and someone told me I should look into the eval() function. So I did. I rewrote my code from C# into Java, and now I have:
var a : String = null;
var b : String = null;
var z : int;
var law : String;
function Update () {
System.Convert.ToInt32(a);
System.Convert.ToInt32(b);
z = eval(law);
}
function OnGUI() {
GUI.Label(new Rect(300, 10, 200, 20), "a");
GUI.Label(new Rect(300, 30, 200, 20), "b");
GUI.Label(new Rect(300, 50, 200, 20), "law");
a = GUI.TextField(new Rect(10, 10, 200, 20), a, 25);
b = GUI.TextField(new Rect(10, 30, 200, 20), b, 25);
law = GUI.TextField(new Rect(10, 50, 200, 20), law, 25);
gameObject.GetComponent(GUIText).text = System.Convert.ToString(z);
}
My problem is I get these errors:
1.
FormatException: Input string was not in the correct format System.Int32.Parse (System.String s) System.Convert.ToInt32 (System.String value) Kalculator.Update () (at Assets/Kalculator.js:9)
2.
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value) Kalculator.Update () (at Assets/Kalculator.js:11)
3.
InvalidCastException: Cannot cast from source type to destination type. Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value) Kalculator.Update () (at Assets/Kalculator.js:11)
4.
CompilationErrorsException: script(1,2): BCE0043: Boo.Lang.Compiler.CompilerError: Unexpected token: +. ---> script:1:2: unexpected token: ["+",,line=1,col=2] --- End of > inner exception stack trace --- > > UnityScript.Scripting.Evaluator.DoCompile > () > UnityScript.Scripting.Evaluator.CompileScript > () UnityScript.Scripting.Evaluator.Run > () > UnityScript.Scripting.Evaluator.Eval > (UnityScript.Scripting.EvaluationContext > context, System.String code) > Kalculator.Update () (at > Assets/Kalculator.js:11)
I also tried int.parseInt(a)/(b) instead of System.Convert, I get the same errors. Any help is gladly welcome.
Maybe I am doing this totally wrong. What I'm trying to do is input 3 strings: a, b, and law. a and b are ints and law is what they do. For example, if I were to input:
a=3 b=5 law=a*b + 1
I should get 16. Anyways, I'm really interested in how this can be implemented. Thank you in advance for all your help.
Ins$$anonymous$$d of using eval
, which would result in horribly insecure code, you should parse your input.
Your answer
Follow this Question
Related Questions
calculate terms in runtime 3 Answers
Need help using GUI.Button/ input storage 1 Answer
Press multiple switches to unlock the door 0 Answers
How to get the key from the button name 1 Answer
Why doesn't this do anything? 1 Answer