[ACE] Help overwriting or adding to the xparam process for xparam_plus addition?

[Explanation, if curious]

I'm trying to add xparam plusses. My reason for doing so is I'm trying to make an aggro (target rate by a certain enemy, aka rage/threat) system using a custom enemy AI script, where the xparams are going to be used as rage by a certain enemy, then I'm going to use a custom stat script, which has its own stats, xstats, for defining of things like hit and evasion rate. xparam(0) for an actor would be the aggro for enemy index 0 for that particular actor, xparam(1) would be the aggro for enemy index 1, and so forth, and whenever you use an action like buffing the party, healing an ally, or damaging an enemy, you will gain aggro from every conscious enemy that's relevant, thus boosting your xparams of numeric ids. So if actor 3 attacked $game_troop.members[4], their xparam(4) would be boosted by a certain amount, usually by 1000 on average. I can't just use variables because variables would need to refer to an actor of a given index, which if you switch the positions of members in battle using a script, then you'd mess up the variables, among other issues.

Like the way the weighted random encounter system works, where you're more likely to encounter an enemy with a higher encounter weight, with my process, an enemy would select a target actor, of which an actor with a higher aggro, be it, the xparam of an id of the index of the enemy, the more likely they'd be targetted.

I don't think the scripts used in this instance are relevant as it is not the process I'm going to achieve it with, but the fact that the xparam_plus addition is the step I need in order to achieve what I'm going for, and the scripts used don't change that.

No added scripts define or alias the xparam process.

---

I added two processes,

  def xparam_plus(xparam_id)    @xparam_plus[xparam_id]  endwhich is based off of 

def param_plus(param_id) @param_plus[param_id] endand

  def add_xparam(xparam_id, value)    @xparam_plus[xparam_id] += value    refresh  endwhich is based off of 

def add_param(param_id, value) @param_plus[param_id] += value refresh endand then defined xparam

# def xparam(xparam_id)# features_sum(FEATURE_XPARAM, xparam_id)# end def xparam(xparam_id) value = 0 + features_sum(FEATURE_XPARAM, xparam_id) + xparam_plus(xparam_id) value endbased off of 

def param(param_id) value = param_base(param_id) + param_plus(param_id) value *= param_rate(param_id) * param_buff_rate(param_id) [[value, param_max(param_id)].min, param_min(param_id)].max.to_i end However, whenever I try to reference xparam, or add in a script that allows you to view an actor's xparams, the game returns this error

Game_BattlerBase:243:in 'xparam_plus': undefined method '[]' for nil:NilClass, NoMethodErrorwhich is referring to def xparam_plus(xparam_id).

 

So I'm not sure how to do this. I assume it's a casual mistake.
Hi,

The actual error lies within the line below:
@xparam_plus[xparam_id]

@xparam_plus  is a new instance variable that has not been initialized up to the point you use it. New instance variables hold the value nil until you assign a new value to them. Many methods, including the element access operator [] are not defined for nil and will cause an exception.

To avoid the exception you have to assign an initial value to your variable. You could do that by either extending the initialize or clear_param_plus method of the Game_BattlerBase class or by doing a "lazy initialization" right before you access the variable:

@xparam_plus ||= Hash.new(0)

The  ||=  will assign a new value to the variable if its old value is either nil or false (e.g. you didn't initialize the variable yet), otherwise this statement does nothing.

By the way: Why do you want to use extra stats for the original xparams if you could use them as threat indicators in the first place?

Despite from that your code should do well. 🙂
Like said, values used have to have numeric ids in order for this to work.

Crystal's Extra Stats don't have ids.

So I'd be adding a ton of conditions in just for a simple process.

So all I can use those for in this instance is the rate that aggro/threat is gained.

But I don't think I understand.

I did

def initialize @hp = @mp = @tp = 0 @hidden = false clear_param_plus #my edit-- added clear xparam_plus to initialize clear_xparam_plus #-- clear_states clear_buffs end def clear_xparam_plus @xparam_plus = [0] * 8 endand it gave me an undefined error for '+' nilclass.

And I wouldn't know where to put the 

@xparam_plus ||= Hash.new(0)

Cause I tried 

$game_actors[1].xparam_plus ||= Hash.new(0)$game_actors[1].add_xparam(8, 7)and it gave me a 0 for 1 argument error

Edit: the problem this time was the fact that I chose the id for xparam to be 8, which doesn't exist.

So

$game_actors[1].add_param(2, 7)$game_variables[1] = $game_actors[1].param(2)while normally having xparam(2) be 0 will return 7.

Thanks.
By using

@xparam_plus = [0] * 8

you assign an array of 8 elements, all initialized with 0. That is sufficient for @params as you only have 8 different parameters (enumerated from 0 to 7), but 10 different xparams. Elements outside of the arrays bounds are substituted with nil.

By default only the object itself has direct access to its instance variables. At the moment,

$game_actors[1].xparam_plus

just refers to this method:

---

I added two processes,

  def xparam_plus(xparam_id)    @xparam_plus[xparam_id]  end
You could have inserted the initialization right before you use the variable:

def xparam_plus(param_id)  @xparam_plus ||= [0] * 10  @xparam_plus[param_id]endHowever, since you decided to initialize the variable when creating the battler, this should not be necessary any more.

I'm glad to hear you already seem to have solved the problem though. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/ace-help-overwriting-or-adding-to-the-xparam-process-for-xparam_plus-addition.38688/