RPGMakerMV: How To Eval

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
556
Reaction score
794
First Language
English
Primarily Uses
RMMV
Eval is the secret we don't talk about. For RPGMakerMV plugin developers, learning to use eval can change the game. So, let's talk about what it does and how to use it.



What Does Eval Do For You
Eval allows you to interpret strings as JavaScript. Now, many devs would advise against using it in a traditional development sense. But for RPGMakerMV, you can use it to create limited scopes where people can run JavaScript code. Leveraging this power can allow people with little JavaScript experience a way to play with the game variables in a safe way. This leads me to how to use it.



How To Use Eval
To use eval wrap a string inside the function called eval, the result will then be returned to you. Here's a quick example:

Code:
 eval("3 + 3"); //Returns 6
This number will be returned to you and you can use it however you like; a simple example, but it has a lot of power behind it. Another important part of eval is that it's local to the function you're in. This is important and offers us the benefits of safety in our plugin. Meaning, we can offer select variables to be evaluated. Here's an example use of eval that could apply to RPGMakerMV: returning actor names.

Code:
 eval(" $gameActors.actor(1).name().toUpperCase()") 
//Returns the Game_Actor name of actor 1 in RPGMakerMV in uppercase.
This snippet of code would return the actor name in all uppercase. This could be great for demonstrating shouting in your game for specific words. This is just one use, you could even showcase the actor stats with this kind of evaluation. Here's an example of using it with my own game to display the time in milliseconds.





As you can see, that's the time in milliseconds from when the window was open and Date.now() was evaluated. Now, I've also turned this into a plugin, which I will be releasing soon!

Anyway, try out eval on your own; it can be useful to your plugin development. Happy creating!
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
problem is the performance hit that eval incurs
It can seem to offer superior performance in some context.
but in most case, it must parse, evaluate, then interpret, source code of unpredictable size , it may contain few statements, or very many. Function shares this problem.

That last point, that the input size is not knowable beforehand, means that code minifiers can't minify the blocks of strings ahead of time, and that runtime engines may not be able to optimize lookahead caching (a fancy way of saying, they can't compile it).


However I still prefer to stay away from this method.
Especially in the context of gameMaking
Maybe more interesting for a website, or a app.
But also have some security issu with injection.

https://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea
https://24ways.org/2005/dont-be-eval
https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
556
Reaction score
794
First Language
English
Primarily Uses
RMMV
I 100% agree with not using eval in the context of many, many areas.

But, for plugin making, it gives us the benefit of creating scopes for creating things like Yanfly's Battle Evaluations, or more specifically, his lunatic mode.

Using it sparingly for plugin creation can make for better dev flexibility when you want to give them the power to script. For things like applications or working on your own game solo, no eval is best eval. :p
 

Dr.Yami

。◕‿◕。
Developer
Joined
Mar 5, 2012
Messages
1,003
Reaction score
757
First Language
Vietnamese
Primarily Uses
Other
eval still has benefits for people who don't wanna touch the Plugin (script) itself to config. However, I recommend using new Function('eval string') so that it can be cached, eval('static string') everytime would hit the performance so bad, especially when using in update().
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
556
Reaction score
794
First Language
English
Primarily Uses
RMMV
Ohhh that's a really good idea; caching the result is a lot better. I think some people call ut memoization? @Dr.Yami

I should write a cache function over eval to keep performance up while working on plugins that involve eval.
 

Dr.Yami

。◕‿◕。
Developer
Joined
Mar 5, 2012
Messages
1,003
Reaction score
757
First Language
Vietnamese
Primarily Uses
Other
It's not really caching the results, but yeah many cases caching the results is superb too.
For the new Function(), it can be like this

Code:
class ExtremelyUI {
    constructor() {
        this._someEval = new Function('1 + 1')
    }

    update() {
        this.x = this._someEval()
        // is better than eval
        // this.x = eval('1 + 1')
    }
}
 

LTN Games

Indie Studio
Veteran
Joined
Jun 25, 2015
Messages
704
Reaction score
631
First Language
English
Primarily Uses
RMMV
I avoided eval for a really long time when making plugins but truth is, every other plugin developer(at least more popular plugin devs) does it and now the RM community is adapting to using evals in the plugin they use. As much as I would like to avoid using it all togather, you just can't now, you might as well stick with many of the same standards as other plugin devs to keep things similar in some fashion.
To keep things safer I always wrap my eval in a try catch and if required I would cache it as Dr. Yami mentioned.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,859
Messages
1,017,030
Members
137,566
Latest member
Fl0shVS
Top