Game Development Community

Is EVAL my new best friend?

by Chris Schirlinger · in Torque Game Builder · 05/12/2006 (10:36 pm) · 6 replies

I just broke the back on T2D this afternoon. (by that I mean I all "made sense" suddenly script codeing wise)

A question, how expensive is EVAL? Is it something that should be used sparingly? Or could you code everything in a never ending series of EVAL statements?

#1
05/12/2006 (10:45 pm)
A rough rule of thumb to eval is: If you find yourself having to use eval(), you are probably doing something wrong. There are only a couple of cases when you have no choice but to use eval.

T.
#2
05/13/2006 (10:34 am)
Eval is very expensive. The only time you should be using eval is when you are dynamically building statements that need to be executed.
#3
05/13/2006 (4:40 pm)
Hurm, ok thats what I was using eval for (Dynamically building statements)

I am using them to allow the game UI to pass one datapoint and an action on that concept ("Increase Power" for example) then use that datapoint value to update the UI, update background data structures and so on in a neat way

Considering each datapoint could have several buttons, text boxes, and other graphical elements plus the base data structures containing the data, and there are a dozen data elements, it seemed EVAL's would simplify the code so rather than a huge honking case statement of 10 x 12 identicle lines of code, I eval build up the button names and underlying variables using the passed datapoint and adjust UI that way

Seems fast enough considering the use maybe pressed those buttons 10 or 20 times during a programs life span :) I won't try and write *EVERYTHING* using EVALs though :) Thanks
#4
05/13/2006 (9:45 pm)
For details... eval is expensive because it has to allocate a new codeblock, compile the script, execute it, then destroy the codeblock. So it's not something you want to do several times a frame if you can avoid it.
#5
05/16/2006 (3:57 am)
Woah I've used eval extensively in my project....best go back and make some changes :P
#6
05/16/2006 (4:31 am)
One of the reasons I started using it was it was used extensivly in the RTS tutorial. I initially started using it just to try and work out how it worked and what it could do for you

I'm not sure how bad it is, I've got some test code that runs...70? 80? Evals on a single button press and it's still nice and snappy.

However, that button press would be in a user interface screen, nothing happening on the screen, and would happen on the users direct action. However, I won't be doung 70 evals per frame in the game loop :)