Problems with using functions instead of eval

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,848
First Language
English
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.
 
Last edited by a moderator:

DoubleX

Just a nameless weakling
Veteran
Joined
Jan 2, 2014
Messages
1,790
Reaction score
943
First Language
Chinese
Primarily Uses
N/A
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 :)
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
You can always create your function like this:

Code:
new Function("return " + param);
So there's no need to explicitly state the return.
 

DoubleX

Just a nameless weakling
Veteran
Joined
Jan 2, 2014
Messages
1,790
Reaction score
943
First Language
Chinese
Primarily Uses
N/A
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 :)
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,848
First Language
English
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.
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
Well, you could always wrap the function itself in an anonymous function, like this:

Code:
return (function() { ... }();
I wouldn't do that, though :D
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,848
First Language
English
Well, you could always wrap the function itself in an anonymous function, like this:

return (function() { ... }();I wouldn't do that, though :D
Is there a problem with that approach? It seems perfectly fine.
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
It creates a new function everytime it's called, which is EXTREMELY slow.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,848
First Language
English
Oh. I guess I might as well just use eval if I wanted to do that...
 

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
functions by default return undefined unless specified.

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

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

Latest Threads

Latest Posts

Latest Profile Posts

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!
one bad plugin combo later and one of my followers is moonwalking off the screen on his own... I didn't even more yet on the new map lol.
time for a new avatar :)

Forum statistics

Threads
106,017
Messages
1,018,356
Members
137,802
Latest member
rencarbali
Top