Let's say one of your characters has an ability that they can use only once per day. This ability applies a State that works as a bonus, so perhaps you want to balance the fact that you can use it sparingly by not making the State disappear at the end of the battle, and make it persist a little longer. The standard way to do this is by toggling the "Remove After Battle" and "Remove After Tot Steps" options, but i found the latter a bit limiting. This one cancels the State after you take a tot of steps, but what if you wanted it to lower the State's Turn Counter for every Tot steps you took instead? That's what this Tutorial is here for!
FOR GRANDMA RANDOR01's RECEIPE YOU'LL NEED:
- Yanfly's BuffsStateCore
Ready? Then let's get started!
PART 1: DATABASE AND YEP_BUFFSSTATESCORE
Alright, the first part will proceed in the States Database. Here, make sure to toggle the "Remove After Battle" of the States you want to work with OFF, and toggle the "Remove After Tot Steps" option ON. In that, write how many steps you must take for the Turn Counter of that State to drop down.
Spoiler
After that, it's time to use Yanfly's plugin. This plugin makes you able to execute JavaScript code in a State's Notetag when that State is Applied, when it's removed, when your party wins, loses or flees from a battle and one of the members has this State and so on. For each of these conditions exists a tag, where we'll write our code:
Code:
<Custom Victory Effect>target._stateSteps[22] = (target._stateTurns[22] * $dataStates[22].stepsToRemove);</Custom Victory Effect><Custom Escape Effect>target._stateSteps[22] = (target._stateTurns[22] * $dataStates[22].stepsToRemove);</Custom Escape Effect>
These two tags, respectively, will run their code when the player Party wins a fight or escapes from one. (If your game has battles where the player WILL lose, you might consider using the "<Custom Defeat Effect>"" tag too).
What does this code do anyway? Basically, it will alter the number of Steps necessary for the State to wear off by taking the State's remaining turns before it expires and multiplies it by the value assigned in the "Remove After Tot Steps" option, which in our case is 20.
So, for example, if our State has 5 turns left, the player will have to take 100 steps before it wears off.
This was the first half. Pretty simple, right? Now we have to make it so that the Turn counter will drop down as you walk too!
PART 2: JAVASCRIPT AND COMMON EVENTS
Here comes the beefiest part. What we're going to do here is to write a Common Event that will check which States you party members have, and make those States Turn Counter drop down as you walk in the map.
First of all, set the Common Event of your choice to run on Parallel, and remember to turn ON the Switch that controls it!
Let's go and take a look at the code we're going to use step by step:
Code:
//These are the States you want to persist after battle and of which you want the turn counter to drop down as you walk$gameSystem.statesRemainAfterBattle = $gameSystem.statusesRemainAfterBattle || [18, 22];//These are your 4 party members. You can make it count more party members if you want by adding a ",[]" inside it. This variable will work as list for each party member, containing the States in "$gameSystem.statesRemainAfterBattle" they're currently affected from.$gameSystem.memberAfflictedRemain = [[],[],[],[]];
This is where we declare our main Variables: "statesRemainAfterBattle" and "memberAfflictedRemain". The first variable is an array containing the State that you want to be affected by the "remove by step" thingy we're working on. In the code's example, the States 18 and 22 will be affected.
The second variable is a 2d Array, that will contain from which of the States in the "statesRemainAfterBattle" variable your Party Members are affected from.
Here's the code that will check that:
Code:
for(var i = 0; i < $gameSystem.memberAfflictedRemain.length; i++){ for(var j = 0; j < $gameSystem.statesRemainAfterBattle.length; j++) { //If the party member you're checking exists, if he's afflicted by one of the States in "$gameSystem.statesRemainAfterBattle" and if the Turn counter for that State hasn't already reached 0, that State will be added in the Party Member's list if($gameParty.members()
!= null && $gameParty.members().isStateAffected($gameSystem.statesRemainAfterBattle[j])) { if($gameParty.members()._stateTurns[$gameSystem.statesRemainAfterBattle[j]] > 0) { $gameSystem.memberAfflictedRemain.push($gameSystem.statesRemainAfterBattle[j]); } else { //If the turn counter for this State has reached 0, the State is removed from the Party member $gameParty.members().removeState($gameSystem.statesRemainAfterBattle[j]); } } }}
This part of the code runs down checking your Party Members, and for each of them will look at the States they're affected from. If one of those States' ID equals to one of the values in "$gameSystem.statesRemainAfterBattle", then the code will add that ID in the "$gameSystem.memberAfflictedRemain" variable for that party member's instance. The code will also check if the Party's space it's checking isn't empty (like perhaps at the start of the game, when you don't have all your Party Members yet) before adding the State in that Party Member's instance, and if the Turn Counter for that Party Member’s State has already reached 0 it will automatically remove it.
Now, for the final part. Here's where the code manages the Steps taken by the player and converts them into Turns remaining for that State:
Code:
for(var j = 0; j < $gameSystem.memberAfflictedRemain.length; j++){ for(var i = 0; i < $gameSystem.memberAfflictedRemain[j].length; i++) { //Here it checks the States in the Party Member's list. For each of them, the game sets the remaining turns using the remaining steps to be taken to make it expire. //In this case, for each 20 steps you take, the State's Turn Counter will drop down by 1 $gameParty.members()[j]._stateTurns[$gameSystem.memberAfflictedRemain[j]] = Math.ceil($gameParty.members()[j]._stateSteps[$gameSystem.memberAfflictedRemain[j] / $dataStates[$gameSystem.memberAfflictedRemain[j]].stepsToRemove); }}
This part also runs down checking your Party Members, but in this case it checks the States stored in each of the members instances, and how many steps you must still take before the State expires. The code will proceed to divide that value by that State's n. steps you've chosen, and assign this new value to "_stateTurns", which contains the Turn Counter for that Party Member’s State.
Here's, for quick reference, the complete code to put in the Common Event:
Spoiler Code:
◆Controls Switches:#0010 MembersStatus? = OFF◆Script:$gameSystem.statesRemainAfterBattle = $gameSystem.statesRemainAfterBattle || [16, 18, 22];◆Script:$gameSystem.memberAfflictedRemain = [[],[],[],[]];:Script:for(var i = 0; i < 4; i++){:Script: for(var j = 0; j < $gameSystem.statesRemainAfterBattle.length; j++){:Script: if($gameParty.members() != null && $gameParty.members().isStateAffected($gameSystem.statesRemainAfterBattle[j])){:Script: if($gameParty.members()._stateTurns[$gameSystem.statesRemainAfterBattle[j]] > 0){:Script: $gameSystem.memberAfflictedRemain.push($gameSystem.statesRemainAfterBattle[j]);:Script: $gameSwitches.setValue(10, true);:Script: }else{ $gameParty.members().removeState($gameSystem.statesRemainAfterBattle[j]); }:Script: }:Script: else{:Script: $gameParty.members().removeState($gameSystem.statesRemainAfterBattle[j]);:Script: }}}◆If:MembersStatus? is ON ◆Script:var membAfflicted = $gameSystem.memberAfflictedRemain; :Script: :Script:for(var j = 0; j < membAfflicted.length; j++) :Script:{ :Script: for(var i = 0; i < membAfflicted[j].length; i++) :Script: { :Script: $gameParty.members()[j]._stateTurns[membAfflicted[j]] = Math.ceil($gameParty.members()[j]._stateSteps[membAfflicted[j]] / $dataStates[membAfflicted[j]].stepsToRemove); :Script: } :Script:} ◆:Else ◆:End
This code will keep running as you walk in the Overworld, constantly checking and updating the Turn Counter of the States of your choice if one or more of your Party Members are affected by it.
Hope that this can be of help!
EDIT: Quick Fix at the last section of the code: changed from "Math.round" to "Math.ceil".
EDIT: Added the complete Script at the end for quick reference. Also added a small conditional: the script won't alter the Members States Turn Counter if no one has at least one of the States you put in "$gameSystem.statesRemainAfterBattle". (Thanks to MushroomCake28 for this one!)
本贴来自国际rpgmaker官方论坛作者:Randor01处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/make-states-turn-counter-drop-down-for-every-tot-steps-taken.120967/