じ☆ve冰风 发表于 4 天前

Modifying Params

Overview

This tutorial will teach about how to modify params via script calls

The problem

I've seen quite a number of people wanting to modify params via script calls,

using a logical method, which doesn't work.

Look at this

$game_actors.mhp += 100If you just look at it, it looks logical right? You're adding 100 to .mhpBut if you test that out, you'll realize that it doesn't work.

Why? Well because you could only do that if mhp was an attribute of the Game_BattlerBase

class which has writers or accessors attached to it. Sadly, it isn't.

So what is mhp? It's actually just a wrapper for the params method of Game_BattlerBase

All the "normal" param calls (.mhp,.mmp,.atk and so on) are wrappers. Look at this

def mhp;param(0);   end               # MHPMaximum Hit Pointsdef mmp;param(1);   end               # MMPMaximum Magic Pointsdef atk;param(2);   end               # ATKATtacK powerdef def;param(3);   end               # DEFDEFense powerdef mat;param(4);   end               # MATMagic ATtack powerdef mdf;param(5);   end               # MDFMagic DeFense powerdef agi;param(6);   end               # AGIAGIlitydef luk;param(7);   end               # LUKLUcKSee? It simply calls another method. Now let's look at that method and further understandwhy the somehow logical code above doesn't work.

def param(param_id)    value = param_base(param_id) + param_plus(param_id)    value *= param_rate(param_id) * param_buff_rate(param_id)    [.min, param_min(param_id)].max.to_iendNow you'll see that it simply returns a value based from several calculations. It doesn't returnany actual param object so in turn, we cannot modify that param. Technically the param object

doesn't exist.

The question now is, how do we modify params using script calls?

Solution

To solve the issue, I looked upon how Event Commands do it. Yes, event commands holds the answer

to this troubling problem.

Event commands for modifying params work by using this simple method inside Game_BattlerBase

def add_param(param_id, value)    @param_plus += value    refreshendIt modifies the @param_plus array which is the array that is looked upon by the param_plus line on the param method.And this is exactly the method that we will need to call if we want to modify the params using script calls. The param_id is the same as the parameter passed by each wrapper listed above.

Example

So now, to make our max hp modification work, we simply use

$game_actors.add_param(0,100)Ending NotesI hope you learn something from this tutorial which could be helpful to your future endeavors.


本贴来自国际rpgmaker官方论坛作者:Engr. Adiktuzmiko处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/modifying-params.32732/
页: [1]
查看完整版本: Modifying Params