x


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: ["+",<77>,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.

more ▼

asked Sep 23 '11 at 10:15 PM

Mayk1993 gravatar image

Mayk1993
16 4 5 5

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3458
x953
x396
x211
x7

asked: Sep 23 '11 at 10:15 PM

Seen: 965 times

Last Updated: Sep 23 '11 at 10:15 PM