Problems with using functions instead of eval

Recently I released a plugin that uses a function to evaluate formulas instead of using eval.

So what I do is create a function that takes a bunch of arguments, along with the formula to use

state.damageModifier = new Function("a", "b", "v", "d", formula);and store it in a state.Then, later, when I need to use this function, I can just call it

var fn = states.damageModifier;var a = this.subject();var b = target;var v = $gameVariables;var d = value;value = fn(a, b, v, d);And it does what I want.Two problems

1. I must include "return" in my formula. Otherwise, it will return null.

2. I must never save this object to a savefile. Functions cannot be serialized.

The first one can be a good thing, as it forces users to properly return values inside their fomulas to avoid strange logic issues.

The second one is a serious issue.

If you store your function with the object (which is common in object-oriented programming), and that object is going into a save file, your plugin break the save file functionality.

You could say "well, if I store it with objects the database, I should be fine, because databases are never saved!"

This assumption wouldn't be true if dynamically generated data is involved.

You would need to at least make sure you properly rebuild those functions when the game is loaded.

What are the problems with using functions instead of simply eval'ing a formula using eval?

Note

the testing I did to check the function serialized was like this

var TH_GameActor_initialize = Game_Actor.prototype.initialize; Game_Actor.prototype.initialize = function(actorId) { this._test = new Function("return this._level"); TH_GameActor_initialize.call(this, actorId); }; var TH_GameActor_refresh = Game_Actor.prototype.refresh; Game_Actor.prototype.refresh = function() { TH_GameActor_refresh.call(this); console.log(this._test()); };Which creates the function when the actor is initialized, and I just print out the function call on refresh.When I first start the game, it's fine.

It saves OK.

But when I load the save file, that property is gone and the game crashes. This is what I mean by "saving is broken"

I don't know if it's just the way I stored the function that is incorrect.
2. I must never save this object to a savefile. Functions cannot be serialized.
I'd then do some insane stuffs like this:

$gameSystem.state.damageModifierFormula = formula;
Code:
state.damageModifier = new Function("a", "b", "v", "d", $gameSystem.state.damageModifierFormula);
Upon save:

state.damageModifier = null;Upon load:

state.damageModifier = new Function("a", "b", "v", "d", $gameSystem.state.damageModifierFormula);The codes are incredibly messy, but I hope you can still get what this setup's trying to do 🙂
You can always create your function like this:

Code:
new Function("return " + param);
So there's no need to explicitly state the return.
You can always create your function like this:

new Function("return " + param);So there's no need to explicitly state the return.
And Tsukihime has said in his/her status that the formula can use if, and we don't know yet if they can always be replaced by ternary operators instead in his/her case 🙂
You can always create your function like this:

new Function("return " + param);So there's no need to explicitly state the return.
I initially tried that in my plugin and was kind of satisfied, but then I realized I needed to include a condition to solve a problem, so I wrote

if ( something ) { do stuff}2 + 4And it failed, since `if` was an unexpected token.Now, of course,

return (if ... )isn't valid syntax.But I like the idea of wrapping the formula IN something. However I can't think of a way that would support any code that someone may throw at it.
Well, you could always wrap the function itself in an anonymous function, like this:

Code:
return (function() { ... }();
I wouldn't do that, though 😀
Well, you could always wrap the function itself in an anonymous function, like this:

return (function() { ... }();I wouldn't do that, though 😀
Is there a problem with that approach? It seems perfectly fine.
It creates a new function everytime it's called, which is EXTREMELY slow.
Oh. I guess I might as well just use eval if I wanted to do that...
functions by default return undefined unless specified.

You can tap into DataManager save functions to save this output.

Original thread: https://forums.rpgmakerweb.com/threads/problems-with-using-functions-instead-of-eval.51767/