Is eval() obsolete?

I don’t know if this is the right forum or not to ask this, but I’ll ask away anyway:

I see people mentioning about eval() function to write a script back then. Since MZ now have script command for us to write something in the event, that function is considered obsolete by now right?

1 Like

so

eval("");

is a Built-in JS features, not an MZ features.

The function is meant to parses string.
and scriptCommands are actually just Eval.

Game_Interpreter.prototype.commandScript = functions() {
 eval(parameters[0]);
} 

The reason people talks about eval being wrong is due to how unsafe it is by itself to use. but thats another subject.

1 Like

I used it as an in-game event based debug console.
It was dumb because you can press F12 to open the Web Browser’s DevTools Console. But when MV/MZ first came out, I didn’t know you could just type in it.
In my defense, there was usually 2 to 3 depreciated code warnings as soon as you open the console log. Plus the old debug console for XP and VXAce were black with an obvious blinking cursor like the old DOS input screen. But the DevTool window was white with tabs.

1 Like

The purpose of eval is to “execute a piece of JavaScript code,” and the code being executed can be a variable. In other words, you can dynamically adjust what gets executed during gameplay. Some plugins use this function to allow users to “manually configure” the displayed interface content, achieving a customized interface effect. If you know JavaScript, you can even use this function to switch menu styles during the game, just like in “Evoland.”

Overall, this function is not “obsolete.”

Another thing to consider that javascript ran as an eval() is thousands of times slower than javascript written in a plugin. Unfortunately RPGmaker uses plenty of evals particularly in damage calculation to allow users to write custom formulas. Also, any code supported by any plugin you write in the database will have to be code ran through an eval().

Not exactly. Many plugins do use eval, true, but if they instead use the Function constructor to compile the code at load time, it should be a lot faster than directly using eval.

That’s a big “if”. Yanfly plugins especially don’t do that.

which is a shame people dont use it more. I think the issue is just that it become wonkey with what u can call, (because it act like a sandbox)

Wow, now I’m starting to miss multi-quote reply feature from the old forum.

I thought eval() wasn’t really be in use anymore, but since some of you pointed it out and how it process Advanced > Script…, guess I need to start converting my parallel event as a plugin instead. Thanks all!

1 Like

You can multi-quote, just select the text you wanna quote and this appears. I learned it the hard way after quote-replying like 20 separate posts lmao.


Eval is fine to use. Don’t worry that much about performance, as long as you don’t run the same eval every single frame you’re good. Maybe not even every single frame is to worry about.

Now if you had, say, all events running evals every single frame, now that’s a big NO.

Oh not really, no. I ran it on a parallel event that checks whether the player is moving or not. Currently it goes like this:

Funny thing I found before I reach there is that the player character gets treated as “not moving” by the game whenever they gets into exact .0 coordinate of each tile (so if you order the player to move 2 tiles, it will go move → stop → move → stop). And somehow the former check has slower update than the latter.

My only problem is that I don’t really know how to change this into a plugin. At least in practice, since I pretty much failed on my last attempt.

Hmmm instead of that second if, why not use the else from the previous command?

… right. Why didn’t I thought of that. It still works the same after I tried it.

Be aware that any time you change a switch or variable, it sends a request for all events on the map to perform an update. If it feels like it runs slow, that’s the likely cause and it piles up with maps with more events. This is one of the main reasons that I usually recommend people to avoid coding game mechanics with the eventing system

Off the top of my head, or at least thinking of the general requirements the event system has, it shouldn’t run that update more than once per frame. Usually what the variable/switch change does is enable a flag which asks the engine to refresh all events next frame or so.

That’s how RGSS did it at least, unless something else changed and they deemed it necessary to refresh everything right away.

Not sure what you mean by that? It does function as a sandbox, yes – that is it doesn’t accidentally expose the internals of your plugin to the custom code. But I don’t know what you mean by “wonky” here.

This is what it does, yes. So, even if you change 50 variables all at once, all of which request an update, there’s only one actual update done.

But with events, only one command runs per frame. So you if you have 50 Control Variables statements in a row, that’s 50 updates. If you have 1 Control Variables that updates 50 variables, that’s 1 update.

something about scope but its also how more strict function() is over eval. had some weird edge cases in the past I cant remember on the spot tho.

The Function constructor does have the disadvantage that it needs to be a complete function; unlike eval, it won’t just return the result of the final statement. Could that be what you’re thinking of?

I think it was the case yes, but it has beeeen a long time I did test with string parsing via eval/ function.

That works in some cases, but not all and eval still does have some advantages over new Function() - mostly ease of use. If you aren’t doing something that’s performance-intensive (anything called every single time in the per-frame loop) then eval is just fine and will cause no noticeable performance decrease whatsoever.

Using new Function() when unnecessary just leads to potentially harder code to work with and maintain in some cases.