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.
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:
