Summary:
Saving proc indirectly is possible and 1 way of doing so is to save the script code used by the proc in string form, then clear the proc right before saving and restore it right after saving and loading.
While proc generally costs more memory, it generally outperforms eval in terms of time. In situations where codes must run as quickly as possible, I'd prefer using proc over eval if both can be used.
eval:
The simpler but also slower in general. If the script codes are complicated and to be executed per frame(such as atb scripts), using eval can cause a significant fps drop unless the machine is powerful.
proc/lamnbda:
More complicated but also faster in general. It generally costs more memory though, as the proc/lambda variable should be kept until it no longer needs to be used, and it seems to me that its size can be quite notable, especially if the script codes are complicated(I'm not sure about this though).
As proc/lambda can't be serialized, users have to either store the variables using other methods(I don't know any about them if there's any), or clear them and save the script codes in string form instead. Of course they'll have to be restored after loading as well.
For example, the below methods clears the proc variables right before saving and restores them right after saving and loading:
Spoiler
alias save_game_without_rescue_ecatb save_game_without_rescue def save_game_without_rescue(index) # Added to clear actors' ecatb proc instance variables before saving clear_ecatb_procs # save_game_without_rescue(index) # Added to restores actor's ecatb proc instance variables after saving update_ecatb_configs # end # save_game_without_rescue alias extract_save_contents_ecatb extract_save_contents def extract_save_contents(contents) extract_save_contents_ecatb(contents) # Added to restores ecatb proc instance variables after loading update_ecatb_configs BattleManager.update_ecatb_configs # end # extract_save_contents def clear_ecatb_procs # $game_party.members.each { |member| member.clear_ecatb_procs } # end # clear_ecatb_procs
Code:
def update_ecatb_configs DoubleX_RMVXA::ECATB::GAME_BATTLER.each { |proc, test| proc = proc.id2name update_ecatb_config(proc) check_ecatb_config(proc, test) } update_ecatb_def_configs update_ecatb_note_configs end # update_ecatb_configs def update_ecatb_config(proc) eval(%Q(@#{proc} = eval("proc { " + $game_system.#{proc} + " }"))) end # update_ecatb_config
Code:
def clear_ecatb_procs # @ecatb_def = @ecatb_item = nil DoubleX_RMVXA::ECATB::GAME_BATTLER.each_key { |proc| eval("@#{proc.id2name} = nil") } # end # clear_ecatb_procs
Where the custom codes used by those procs are stored as strings under class Game_System:
Spoiler
# Stores all configurations DoubleX_RMVXA::ECATB::CONFIG.each { |configs| configs.each_key { |config| eval("attr_accessor :#{config.id2name}") } } #
Code:
def init_ecatb_configs # Initializes the configuration settings DoubleX_RMVXA::ECATB::CONFIG.each { |configs| configs.each { |config, val| eval("@#{config.id2name} = #{val}") } } # end # init_ecatb_configs
Users can change $game_system.proc and call update_ecatb_config(proc) to change both the custom code string and its proc variable, effectively changing the script content on the fly without sacrificing too much performance.
The below method, which is called every frame, shows at least 1 reason why the procs are used instead of eval:
Spoiler
def ecatb_update? # Checks if the clock can be updated and the wait condition isn't met return false if scene_changing? || !SceneManager.scene_is?(Scene_Battle) !$game_message.visible && !@ecatb_wait_cond.call # end # ecatb_update?
Where the default value of @ecatb_wait_cond is this:
Spoiler
:ecatb_wait_cond => %Q(%Q(#{WC1} || #{WC2} && (#{WC3} || #{WC4} || #{WC5} || #{WC6} || #{WC7} || #{WC8} || #{WC9} || #{WC10} || #{WCC1}))),
Code:
# Checks if the atb clock is forced to stop WC1 = %Q(@ecatb_force_clock == :stop) # Checks if the atb clock isn't forced to run WC2 = %Q(@ecatb_force_clock != :run) # Checks if an action is executing WC3 = %Q(!BattleManager.ecatb_can_esc) # Checks if the cancel window is active WC4 = %Q(@ecatb_cancel_actor_window.active) # Checks if the party command window is active WC5 = %Q(@party_command_window.active) # Checks if the actor target window is active WC6 = %Q(@actor_window.active) # Checks if the enemy target window is active WC7 = %Q(@enemy_window.active) # Checks if the skill window is active WC8 = %Q(@skill_window.active) # Checks if the item window is active WC9 = %Q(@item_window.active) # Checks if the actor command window is active WC10 = %Q(@actor_command_window.active) # Checks if the combat log window is visible WCC1 = %Q(update_msg_open_ecatb_compatibility)
Using eval instead of proc/lambda or instance_eval(or something like that) could and would probably cause a significant fps drop unless the machine is powerful(and in this case, it's not the only thing being run every frame).
The below is my informal benchmark for eval, proc, lambda and instance_eval(I counted the seconds with the aid of a clock):
Spoiler
class Test def make_def(method) instance_eval(method) endendblock = "nil"test = Test.newtest.make_def(%Q( def new_def #{block} end))#~ p("instance_eval")#~ 100_000_000.times { test.new_def }#~ p("instance_eval")#~ # Roughly 20 seconds#~ test = eval("lambda { #{block} }")#~ p("lambda")#~ 100_000_000.times { test.call }#~ p("lambda")#~ # Roughly 36 seconds#~ test = eval("proc { #{block} }")#~ p("proc")#~ 100_000_000.times { test.call }#~ p("proc")#~ # Roughly 36 seconds#~ p("eval")#~ 10_000_000.times { eval(block) }#~ p("eval")#~ # Roughly 63 seconds
The script code string is "nil"
instance_eval, lambda and proc are run 100,000,000 times with result roughly being 20, 36 and 36 seconds respectively, while eval are run 10,000,000 times with result roughly being 63 seconds.
It'd probably need roughly 630 seconds to run the above eval 100,000,000 times.
Another example:
Spoiler
class Test def make_def(method) instance_eval(method) endendblock = "rand > rand || rand < rand ? [rand,rand].min : [rand,rand].max"test = Test.newtest.make_def(%Q( def new_def #{block} end))#~ p("instance_eval")#~ 10_000_000.times { test.new_def }#~ p("instance_eval")#~ # Roughly 18 seconds#~ test = eval("lambda { #{block} }")#~ p("lambda")#~ 10_000_000.times { test.call }#~ p("lambda")#~ # Roughly 21 seconds#~ test = eval("proc { #{block} }")#~ p("proc")#~ 10_000_000.times { test.call }#~ p("proc")#~ # Roughly 21 seconds#~ p("eval")#~ 1_000_000.times { eval(block) }#~ p("eval")#~ # Roughly 19 seconds
The script code string is "rand > rand || rand < rand ? [rand,rand].min : [rand,rand].max"
instance_eval, lambda and proc are run 10,000,000 times with result roughly being 18, 21 and 21 seconds respectively, while eval are run 10,000,000 times with result roughly being 63 seconds.
It'd probably need roughly 190 seconds to run the above eval 10,000,000 times.
Of course such script code string is a bit exaggerated, but these 2 test results may suggest that the more complicated the script code is, the less the perfromance difference between eval, proc/lambda and instance_eval. According to test 2 test results, eval is much, much slower than proc/lambda and instance_eval, the speed of proc and lambda are almost the same, and they're significantly slower than instance_eval.
Check Tsukihime's post for eval vs lambda for more info(including formal benchmark) about eval vs lambda:
http://forums.rpgmakerweb.com/index.php?/topic/31474-formula-optimization-speed-up-your-formula-eval-code/?hl=%2Beval+%2Blambda