MV Notetag setting a turn for a state.

Hello again!

I have this notetag in a state that "sets a number of turn for a state". But it does not work, or I think it doesn't.
Maybe I misunderstood it's function. Anyway, I can setup the number of turns before the state fades away using the State Removal Conditions, but since I want to learn about scripting, I want to know if I just misunderstood the function of it or I made a mistake on using it.

Below is the notetag:

Code:
<Custom Confirm Effect>
// The State ID used for stage 1.
var stage1 = 14;
// The State ID used for stage 2.
var stage2 = 15;
// The State ID used for stage 3.
var stage3 = 16;
// The State ID used for stage 4.
var stage4 = 17;
// The State ID used for stage 5.
var stage5 = 18;
// The State ID used for stun.
var stun = 19;
// Check to see if the action is an attack.
if (this.isAttack()) {
  // Check if the target is affected by stage 1.
  if (target.isStateAffected(stage1)) {
    // Set the number of turns for stage 1 to 3.
    target.setStateTurns(stage1, 3);
  // Check if the target is affected by stage 2.
  } else if (target.isStateAffected(stage2)) {
    // Set the number of turns for stage 2 to 3.
    target.setStateTurns(stage2, 3);
  // Check if the target is affected by stage 3.
  } else if (target.isStateAffected(stage3)) {
    // Set the number of turns for stage 3 to 3.
    target.setStateTurns(stage3, 3);
 // Check if the target is affected by stage 4.
  } else if (target.isStateAffected(stage4)) {
    // Set the number of turns for stage 4 to 3.
    target.setStateTurns(stage4, 3);
 // Check if the target is affected by stage 5.
  } else if (target.isStateAffected(stage5)) {
    // Set the number of turns for stage 5 to 3.
    target.setStateTurns(stage5, 3);
  }
// Check if the action is a skill and if the target and user are on opposite teams.
} else if (this.isSkill() && target.isActor() !== user.isActor()) {
  // Check if the target is NOT affected by stages up to 5
  if (!target.isStateAffected(stage1) && !target.isStateAffected(stage2) && !target.isStateAffected(stage3) && !target.isStateAffected(stage4) && !target.isStateAffected(stage5)) {
    // Add stage 1 to the target.
    target.addState(stage1);
  // Check if the target is affected by stage 1.
  } else if (target.isStateAffected(stage1)) {
    // Remove stage 1.
    target.removeState(stage1);
    // Apply stage 2.
    target.addState(stage2);
  // Check if the target is affected by stage 2.
  } else if (target.isStateAffected(stage2)) {
    // Remove stage 2.
    target.removeState(stage2);
    // Apply stage 3.
    target.addState(stage3);
 // Check if the target is affected by stage 3.
  } else if (target.isStateAffected(stage3)) {
    // Remove stage 3.
    target.removeState(stage3);
    // Apply stage 4.
    target.addState(stage4);
 // Check if the target is affected by stage 4.
  } else if (target.isStateAffected(stage4)) {
    // Remove stage 4.
    target.removeState(stage4);
    // Apply stage 5.
    target.addState(stage5);
  // Check if the target is affected by stage 5.
  } else if (target.isStateAffected(stage5)) {
    // Remove stage 5.
    target.removeState(stage5);
    // Apply stun.
    target.addState(stun);
  }
}
</Custom Confirm Effect>

<Custom Conclude Effect>
// The State ID used for stun.
var stun = 19;
// Check if the target is affected by stun.
if (target.isStateAffected(stun)) {
    // Play animation 0002 on target.
    target.startAnimation(2);
}
</Custom Conclude Effect>

The way it's supposed to work is that every attack or skill, would add a stack of the state until the 5th stack in which it would trigger another state that actually stuns the enemy.

At it's current code, it works, the stacking works, but the state would not remove itself after 3 turns if I stopped attacking the enemy. I have to put the Removal Conditions manually instead of in this code.

Code:
  // Set the number of turns for stage 1 to 3.

    target.setStateTurns(stage1, 3);

So this is the code that confuses me, because I thought the function of this is to literally setup how many turns a state would last.
What do the states look like in your database? Do they have an Auto-removal Timing of Turn End selected? If that's not filled out, just giving them turns through the game engine won't make them expire.

Since you're trying to learn about coding, I would suggest using repeating code instead of extensive lists of if/else if conditionals.

You could use a for loop, and JavaScript also has a function built into arrays to do a for loop internally. Since your state IDs are consecutive, I think it's shorter to just do a for loop.
Code:
for (let i=14; i<=18; i++)
{
    if (target.isStateAffected(i))
        target.setStateTurns(i, 3);
}
I'll demonstrate the other method, one of JavaScript's built-in array functions, in the second half:
Code:
else if (this.isSkill() && target.isActor() !== user.isActor()) {
    // Check if the target is NOT affected by stages up to 5
    if (![14, 15, 16, 17, 18].some(state => target.isStateAffected(state)))
        target.addState(14);

    else
    {
        // Find the state target is affected by and apply the next
        for (i=14; i<=18; i++)
        {
            if (target.isStateAffected(i))
            {
                target.removeState(i);
                target.addState(i+1);
                break;
            }
        }
    }
What do the states look like in your database? Do they have an Auto-removal Timing of Turn End selected? If that's not filled out, just giving them turns through the game engine won't make them expire.

Since you're trying to learn about coding, I would suggest using repeating code instead of extensive lists of if/else if conditionals.

You could use a for loop, and JavaScript also has a function built into arrays to do a for loop internally. Since your state IDs are consecutive, I think it's shorter to just do a for loop.
Code:
for (let i=14; i<=18; i++)
{
    if (target.isStateAffected(i))
        target.setStateTurns(i, 3);
}
I'll demonstrate the other method, one of JavaScript's built-in array functions, in the second half:
Code:
else if (this.isSkill() && target.isActor() !== user.isActor()) {
    // Check if the target is NOT affected by stages up to 5
    if (![14, 15, 16, 17, 18].some(state => target.isStateAffected(state)))
        target.addState(14);

    else
    {
        // Find the state target is affected by and apply the next
        for (i=14; i<=18; i++)
        {
            if (target.isStateAffected(i))
            {
                target.removeState(i);
                target.addState(i+1);
                break;
            }
        }
    }

Attached are the States I'm using.

State 0 has the code I put in originally.
and from stage 1-5, the only difference is the icon change.
Lastly the stun state is the same thing as the stages but with only 1 turn expiration + cannot move restriction.

The code you demonstrated above simplified what I am trying to do.
I'd play around it and see how it actually works.
If you turn on the plugin parameter in Buffs & States to display the turn counter, what does it show?

Does it display correctly when the state is applied? When it's refreshed? Do you see it count down at the end of turns?

Are you using the default battle system or something else? A number of Yanfly's battle systems change how state durations work into timing using ticks, and that can screw with "turn end" settings.
If you turn on the plugin parameter in Buffs & States to display the turn counter, what does it show?

Does it display correctly when the state is applied? When it's refreshed? Do you see it count down at the end of turns?

Are you using the default battle system or something else? A number of Yanfly's battle systems change how state durations work into timing using ticks, and that can screw with "turn end" settings.
The turn counter is in false, I turned it on now.

I tried it, and Does it display correctly when the state is applied? Yes.
When it's refreshed? Yes.
Do you see it count down at the end of turns? Yes.

I am using a card battle system, but I formatted mine to look like teacups instead. It doesn't have any parameters that affects states as it uses skills as cards.

This is using the default Removal Conditions of 3 Turns.
I tried it, and Does it display correctly when the state is applied? Yes.
When it's refreshed? Yes.
Do you see it count down at the end of turns? Yes.
Okay, so use that to narrow it down - what's the bug?

Is the code for the attack not refreshing the counter? Do you see the turns count down to 0 and the state doesn't remove? What isn't working for you?
Okay, so use that to narrow it down - what's the bug?

Is the code for the attack not refreshing the counter? Do you see the turns count down to 0 and the state doesn't remove? What isn't working for you?
I made a mistake I'm very sorry.

If you look at the notetag I made the first time, it only says that ATTACKS will set the stateturns.
And the card plugin only uses skills... I'm so dumb. The state notetag does not set stateturns.. ofcourse it won't work. 😅

I will try to figure out how to use it using the example you gave me. I like it because it looks super clean and short.
Then I think you need to reexamine your entire logic.

It made a bit of sense how you originally posted it because you had it as "attack will reset the counter, any other skill will bump it up to the next state."

But if you don't want that divide...then how it is supposed to work? If you just want everything that hits the target to bump it up to the next state, what's the point of the section that sets the state turns? When is that supposed to happen, or is it irrelevant?
Then I think you need to reexamine your entire logic.

It made a bit of sense how you originally posted it because you had it as "attack will reset the counter, any other skill will bump it up to the next state."

But if you don't want that divide...then how it is supposed to work? If you just want everything that hits the target to bump it up to the next state, what's the point of the section that sets the state turns? When is that supposed to happen, or is it irrelevant?

Yup I reconfigure it by removing the if attack portion entirely.
Since I'll be using only skills, it would only detect skills that target the enemy now.

The point of the state turn is to give the player a time limit.
If you engage an enemy by attacking it, you have 3 turns to advance to the next state, and then another 3 turns until you reach the end of the state stages, at which point the enemy will get stunned. If you do not attack the enemy, the state will expire and reset to 0, losing the opportunity to stun the enemy.

The card system allows for the player to do several skills per turn as long as the player can afford it.
The point of the state turn is to give the player a time limit.
If you engage an enemy by attacking it, you have 3 turns to advance to the next state, and then another 3 turns until you reach the end of the state stages, at which point the enemy will get stunned. If you do not attack the enemy, the state will expire and reset to 0, losing the opportunity to stun the enemy.
I still don't understand.

The state is initially set to 3 turns by the database setting.

If you hit the enemy while the state is on, it is removed and advances to a completely different state.

So under what condition would you reset the turns to 3 by code in the notetag?
I still don't understand.

The state is initially set to 3 turns by the database setting.

If you hit the enemy while the state is on, it is removed and advances to a completely different state.

So under what condition would you reset the turns to 3 by code in the notetag?

That's what I'm trying to figure out.

The reset part works if I just use the database itself and not notetags to set the stateturns.

Using the database, advancing the states will always reset the counter to 3 because all stages from 1-5 all have the same 3 turns in the database.
Yes, but you never said you don't want that.

If you're saying you want to apply the next state and make it start at the number of turns the old state was on...your original code did not try to do that, and you never said it 😛

Why is it necessary to use a series of states at all? Why not just use the Buffs & States state counters?
Yes, but you never said you don't want that.

If you're saying you want to apply the next state and make it start at the number of turns the old state was on...your original code did not try to do that, and you never said it 😛

Why is it necessary to use a series of states at all? Why not just use the Buffs & States state counters?

The original code does advance your through the stages, the only thing is that I do not understand how to use the stateTurns in the code itself, without using the Removal Conditions in the database because I want to learn how to do it that way.

And then I realized I wasn't really using any "attacks" since the cards themselves are all skills, so the first part of the if code does not make sense, and I removed it. What was left was the if skills, which works, but I can't make the stateTurns work with it.

Why not just use the Buffs & States state counters?
I don't actually know what you mean by this, sorry. Is there an even easier way to do it and I missed it somehow?
I'm sorry, but your last post did not really address what I said/asked.

We're 14 posts in and I still don't clearly understand how you want the states to work. You keep talking about the code you're trying to write, but not what it's supposed to do in the game.

There is a description of and notetags/script calls for state counters in the plugin documentation.

Good luck!
I thought I already addressed it on the prior posts.

It's supposed to stack until it reaches the final stage, the fifth time you use a skill on an enemy, and then it resets the stacks to 0, and triggering another state after that that stuns the enemy.

So enemy no state, use skill on said enemy, enemy state stage1, so on and so forth.
Okay, but you're using terminology that doesn't mean anything.

What does "stack" mean? You are explicitly removing the old state and adding a new one, so you clearly don't mean you're stacking the states together. So I don't know what you mean by stacks at all.

I asked you specifically in post #12 if you want the new states to be applied with the same duration as the previous state had, and you did not address it.

I'm not ragging on you, but you just haven't given a clear description, using RPG Maker terminology, of what you want to do.

As an example:
"I want to inflict a state on the target. That state expires in 3 turns. If the target is hit 5 more times before the state expires, they will be stunned."

Is that single-line description accurately saying what you're trying to accomplish?
Okay, but you're using terminology that doesn't mean anything.

What does "stack" mean? You are explicitly removing the old state and adding a new one, so you clearly don't mean you're stacking the states together. So I don't know what you mean by stacks at all.

I asked you specifically in post #12 if you want the new states to be applied with the same duration as the previous state had, and you did not address it.

I'm not ragging on you, but you just haven't given a clear description, using RPG Maker terminology, of what you want to do.

As an example:
"I want to inflict a state on the target. That state expires in 3 turns. If the target is hit 5 more times before the state expires, they will be stunned."

Is that single-line description accurately saying what you're trying to accomplish?

Okay sorry I worded it wrongly.

What does "stack" mean?
I thought what I meant was stacking, but as you pointed out, it's not really stacking because it removes the previous state. In which case, I think the better terminology is a state that advances the more you use a skill on an enemy, it resets it's Auto Turn Removal Duration to 3 everytime you trigger it to advance.

I asked you specifically in post #12 if you want the new states to be applied with the same duration as the previous state had, and you did not address it.
I really thought I addressed it because I called it stacks, my lack of English vocabulary is at fault.

"I want to inflict a state on the target. That state expires in 3 turns. If the target is hit 5 more times before the state expires, they will be stunned."
Yes, precisely this.

I know you're just trying to help, I take no offense even if it felt that way, I want to learn first and foremost.
I really thought I addressed it because I called it stacks, my lack of English vocabulary is at fault.
You may want to edit your profile to reflect that your first language is not English - other posters will often try to use simpler sentences and pictures to accommodate you.

I thought what I meant was stacking, but as you pointed out, it's not really stacking because it removes the previous state. In which case, I think the better terminology is a state that advances the more you use a skill on an enemy, it resets it's Auto Turn Removal Duration to 3 everytime you trigger it to advance.
Then I still don't understand - that is what it does. Every time you put on the next state, it will start at 3 because that's what you have in the database. So I don't know what you're saying works incorrectly in your game.

What does your complete notetag look like now? You said you were going to get rid of the if/else if stuff that wasn't right.

Just for the sake of offering an alternative, as I said above I wouldn't use multiple states for this at all. I would use the State Counters from Yanfly's Buffs & States plugin.
Code:
<Custom Confirm Effect>
if (!target.isDead() && target.isActor() != user.isActor())
{
    // Give them the state if they don't have it
    if (!target.isStateAffected(14))
    {
        target.addState(14);
        target.setStateCounter(14, 1);
    }
    else
    {
        // Add to the counter
        target.addStateCounter(14, 1);
        // If it's 5, stun them
        if (target.getStateCounter(14)==5)
        {
            target.removeState(14);
            target.addState(19);
        }
    }
}
</Custom Confirm Effect>
You may want to edit your profile to reflect that your first language is not English - other posters will often try to use simpler sentences and pictures to accommodate you.


Then I still don't understand - that is what it does. Every time you put on the next state, it will start at 3 because that's what you have in the database. So I don't know what you're saying works incorrectly in your game.

What does your complete notetag look like now? You said you were going to get rid of the if/else if stuff that wasn't right.

Just for the sake of offering an alternative, as I said above I wouldn't use multiple states for this at all. I would use the State Counters from Yanfly's Buffs & States plugin.
Code:
<Custom Confirm Effect>
if (!target.isDead() && target.isActor() != user.isActor())
{
    // Give them the state if they don't have it
    if (!target.isStateAffected(14))
    {
        target.addState(14);
        target.setStateCounter(14, 1);
    }
    else
    {
        // Add to the counter
        target.addStateCounter(14, 1);
        // If it's 5, stun them
        if (target.getStateCounter(14)==5)
        {
            target.removeState(14);
            target.addState(19);
        }
    }
}
</Custom Confirm Effect>

You may want to edit your profile to reflect that your first language is not English - other posters will often try to use simpler sentences and pictures to accommodate you.
Will do!

Then I still don't understand - that is what it does. Every time you put on the next state, it will start at 3 because that's what you have in the database. So I don't know what you're saying works incorrectly in your game.
The thing is that it works as it is now. There's no error whatsoever.
The reason I posted in the first place is to figure out why the setStateTurn doesn't work.
I can simply just use the Database's Auto Turn Removal Duration, which is fine, but for me to learn I really wanted to figure out what I was doing wrong there.

What does your complete notetag look like now? You said you were going to get rid of the if/else if stuff that wasn't right.
Code:
<Custom Confirm Effect>
// The State ID used for stage 1.
var stage1 = 14;
// The State ID used for stage 2.
var stage2 = 15;
// The State ID used for stage 3.
var stage3 = 16;
// The State ID used for stage 4.
var stage4 = 17;
// The State ID used for stage 5.
var stage5 = 18;
// The State ID used for stun.
var stun = 19;
// Check if the action is a skill and if the target and user are on opposite teams.
  if (this.isSkill() && target.isActor() !== user.isActor()) {
  // Check if the target is NOT affected by stages up to 5
  if (!target.isStateAffected(stage1) && !target.isStateAffected(stage2) && !target.isStateAffected(stage3) && !target.isStateAffected(stage4) && !target.isStateAffected(stage5)) {
    // Add stage 1 to the target.
    target.addState(stage1);
  // Check if the target is affected by stage 1.
  } else if (target.isStateAffected(stage1)) {
    // Remove stage 1.
    target.removeState(stage1);
    // Apply stage 2.
    target.addState(stage2);
  // Check if the target is affected by stage 2.
  } else if (target.isStateAffected(stage2)) {
    // Remove stage 2.
    target.removeState(stage2);
    // Apply stage 3.
    target.addState(stage3);
 // Check if the target is affected by stage 3.
  } else if (target.isStateAffected(stage3)) {
    // Remove stage 3.
    target.removeState(stage3);
    // Apply stage 4.
    target.addState(stage4);
 // Check if the target is affected by stage 4.
  } else if (target.isStateAffected(stage4)) {
    // Remove stage 4.
    target.removeState(stage4);
    // Apply stage 5.
    target.addState(stage5);
  // Check if the target is affected by stage 5.
  } else if (target.isStateAffected(stage5)) {
    // Remove stage 5.
    target.removeState(stage5);
    // Apply stun.
    target.addState(stun);
  }
}
</Custom Confirm Effect>

<Custom Conclude Effect>
 // The State ID used for stun.
var stun = 19;
 // Check if the target is affected by stun.
if (target.isStateAffected(stun)) {
    // Play animation 0002 on target.
    target.startAnimation(2);
}
</Custom Conclude Effect>

The notetag currently. I was trying to play around the first notetag you shared, I just couldn't figure out yet how to properly use it.

The one you shared now seems more straight forward. I didn't know I can use counters instead of making multiple states.
The thing is that it works as it is now. There's no error whatsoever.
😅

The reason I posted in the first place is to figure out why the setStateTurn doesn't work.
You figured that out in post #7 - you were using setStateTurn() inside of isAttack(). But you weren't using the Attack skill, so it never called that function at all.

Original thread: https://forums.rpgmakerweb.com/threads/notetag-setting-a-turn-for-a-state.157746/