Just having an attr_accessor won't help you here. Values passed as method arguments are still only local variables, unless explicitly placed in another variable.
Assigning a 'user' in initialize is equally pointless. 'user' is the battler object initiating an attack, skill or item. There is no way to tell what this would be during initialize. You need to trap it at a point where you know the value exists (eg., when it is passed as an argument to make_damage_value) so you can pass it along appropriately. The variables themselves are just pointers to specific objects, so any object stored in more than one variable is simply another reference to that object. All we need to do is keep the reference around for as long as we need it.
Examples for what I mentioned:
Spoiler
class Game_Battler def make_damage_value(user, item) value = item.damage.eval(user, self, $game_variables) value *= item_element_rate(user, item) value *= pdr if item.physical? value *= mdr if item.magical? value *= rec if item.damage.recover? value = apply_critical(value, user) if @result.critical # this line needed to be changed value = apply_variance(value, item.damage.variance, user) # and this one value = apply_guard(value) @result.make_damage(value.to_i, item) end alias apply_critical_pass_user_lv apply_critical def apply_critical(damage, user = self) damage = apply_critical_pass_user_lv(damage) # alternatively, just overwrite the method # do stuff with user return damage end alias apply_variance_pass_user_lv apply_variance def apply_variance(damage, variance, user = self) damage = apply_variance_pass_user_lv(damage, variance) # alternatively, just overwrite the method # do stuff with user return damage end end
Using this approach, you
have to overwrite make_damage_value in order to pass user down to the other methods. You have to redefine apply_variance and apply_critical to add the user argument (optional here, defaulting to self if not given), but you can either use aliases or completely overwrite here.
Or:
Spoiler
class Game_Battler alias make_damage_value_pass_user_iv make_damage_value def make_damage_value(user, item) @user = user # assign to instance variable make_damage_value_pass_user_iv(user, item) # @user is avaiable to all methods called within @user = nil # user is not meant to be persistent, nullifying it here end alias apply_critical_pass_user_iv apply_critical def apply_critical(damage) damage = apply_critical_pass_user_iv(damage) # alternatively, just overwrite # do stuff with @user return damage end alias apply_variance_pass_user_iv apply_variance def apply_variance(damage, variance) damage = apply_variance_pass_user_iv(damage, variance) #alternatively, just overwrite # do stuff with @user return damage end end
We take the user variable passed into the method and store it as an instance variable so it can be accessed by all the methods called within the method we are aliasing (make_damage_value). Using this approach, we can make the variable available without having to overwrite make_damage_value just to pass some values. This is typically done to attempt to preserve compatibility with a script that would want to modify make_damage_value, since it is a fairly central method and we don't have to overwrite it completely just to pass along a single variable. However, we are also making the assumption that any redefinition of make_damage_value would call apply_variance and apply_critical, otherwise avoiding overwriting it would be pointless. Also, since the reference to user is not meant to be persistent, we need to nullify the instance variable before the method terminates. Note that nullifying the variable does not alter the
object in any way, we are simply removing the reference so we don't have an unnecessary refernece to a different battler within this battler. Again, with the apply_variance and apply_critical methods themselves you can either alias and change the damage after it has been returned from the original method, or just overwrite them completely.