Hello, I'm looking for a way to make the item and skill damage formula "safe" to evaluate.
By safe I mean limiting as much as possible out-of-scope effects like variable modifications, attributes changes and state addition.
The reason behind my issue is that in my script, there is a preview damage system used both by the interface to show an estimation of the item effects and by the computer-controlled units to rate the efficiency of their actions and choose one.
The preview can be called many times during the process.
I'm aware that by the nature of eval as it runs script commands, it's impossible to predict everything and the user of the script should ensure that they don't do out-of-scope effects in their damage formula. Still I think creating a somewhat safe environment could be helpful.
Here is my current code:
I guess I should also make some
By safe I mean limiting as much as possible out-of-scope effects like variable modifications, attributes changes and state addition.
The reason behind my issue is that in my script, there is a preview damage system used both by the interface to show an estimation of the item effects and by the computer-controlled units to rate the efficiency of their actions and choose one.
The preview can be called many times during the process.
I'm aware that by the nature of eval as it runs script commands, it's impossible to predict everything and the user of the script should ensure that they don't do out-of-scope effects in their damage formula. Still I think creating a somewhat safe environment could be helpful.
Here is my current code:
Ruby:
#==============================================================================
# Game_SafeVariables
#------------------------------------------------------------------------------
# Temporary class to handle variables and be discarded right after
#==============================================================================
class Game_SafeVariables < Game_Variables
#--------------------------------------------------------------------------
# override method initialize -> copy data object
#--------------------------------------------------------------------------
def initialize(game_variables)
super()
@data = game_variables.instance_variable_get(:@data).dup
end
#--------------------------------------------------------------------------
# override method: on_change -> do nothing
#--------------------------------------------------------------------------
def on_change; end
end #Game_SafeVariables
class Game_Battler
#--------------------------------------------------------------------------
# overwrite method: preview_damage_eval
#--------------------------------------------------------------------------
def preview_damage_eval(user, item)
gsv = Game_SafeVariables.new($game_variables)
item.damage.eval(user.clone, self.clone, gsv)
#original code:
#item.damage.eval(user, self, $game_variables)
end
end #Game_Battler
I guess I should also make some
Game_SafeActor and Game_SafeEnemy to clone the battlers as shallow copies won't make them safe from state changes. Or I could add a temporary boolean to $game_temp to disable methods for Game_Actor and Game_Enemy (and in fact Game_Variables too).