Removing 1 instance of a state? (state stack)

Hellu~
So I'm using TheoAllen - Stacking States
Ruby:
# =============================================================================
# TheoAllen - Stacking States
# Version : 1.0
# Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
# (This script documentation is written in informal indonesian language)
# =============================================================================
($imported ||= {})[:Theo_StackingState] = true
# =============================================================================
# Change Logs:
# -----------------------------------------------------------------------------
# 2013.10.24 - Finished Script
# =============================================================================
=begin

  Introduction:
  This script allows you to assign statuses to enemies in batches.
  For example, the enemy gets 3 poison statuses and the like.
 
  How to use :
  Install this script under the material but above the main
  Use the <stack: n> tag on the notebox states in the database. Where n is
  number for max for stack state
 
  Terms of use :
  My credit, TheoAllen. For example, if you can edit my scripts then it becomes more
  cool, whatever. I free. Just don't claim it. If you want
  used for commercial, don't forget, I share it for free.

=end
# =============================================================================
# Tidak ada konfigurasi
# =============================================================================
class RPG::State < RPG::BaseItem
  attr_accessor :max_stack
 
  def load_stack
    @max_stack = 1
    note.split(/[\r\n]+/).each do |line|
      if line =~ /<stack:[ ]*(\d+)>/i
        @max_stack = $1.to_i
      end
    end
  end
 
end

class << DataManager
 
  alias theo_stackstate_load_db load_database
  def load_database
    theo_stackstate_load_db
    load_stackstate_db
  end
 
  def load_stackstate_db
    $data_states.compact.each do |state|
      state.load_stack
    end
  end
 
end

class Game_Battler < Game_BattlerBase
  # ---------------------------------------------------------------------------
  # Overwrite add state
  # ---------------------------------------------------------------------------
  def add_state(state_id)
    if state_addable?(state_id)
      add_new_state(state_id) unless state?(state_id) && state_maxed?(state_id)
      reset_state_counts(state_id)
      @result.added_states.push(state_id).uniq!
    end
  end
 
  def state_stack(state_id)
    (@states.count(state_id) || 0)
end
 
  def state_maxed?(state_id)
    @states.select {|id| id == state_id}.size ==
      $data_states[state_id].max_stack
  end
end

(Sorry, there's not link, I had to download it game to get the script)

I wanted to create a def that when the state is gonna be removed, it only remove 1 instance of said state.
I tried with several options, but as in most of my other post, I claimed, I'm not a scripter...

I tried to re-write the Game Battlerbase same as this script already do when you add a state, but couldn't figure out how to do it when erasing.

Code:
  def erase_state(state_id)
      if stack_state(state_id) > 1
    @states.delete(state_id).uniq!
    @state_turns.delete(state_id)
    @state_steps.delete(state_id)
    else
    @states.delete(state_id)
    @state_turns.delete(state_id)
    @state_steps.delete(state_id)
  end

But as you would guess, my try was useless. (and I tried several other options too).

I also tried this def from another state stack script but doesn't seem to work neither:

Code:
 def erase_state(state_id)
    @states -= [state_id]      ## Line added to remove only one instance.
    return if state?(state_id) ## Line added to prevent a stack from bugging.
    @state_turns.delete(state_id)
    @state_steps.delete(state_id)
  end
I would have modified Game_Battler not BattlerBase
Ruby:
  def remove_state(state_id)
    if state?(state_id)
      revive if state_id == death_state_id
      erase_state(state_id) if state_stack(state_id) == 1
      refresh
      @result.removed_states.push(state_id).uniq!
    end
  end
def remove_state(state_id) if state?(state_id) revive if state_id == death_state_id erase_state(state_id) if state_stack(state_id) == 1 refresh @result.removed_states.push(state_id).uniq! end end
Thanks for always answering!
I tested it, and its not erasing states if I have 2 or higher stacks of the states.
But it erase it if its only one.
It still shows a pop up as if it was removing the state, but the stack of states still shows.

1658275599752.png

(Red text is show when a state is being removed. But the states are still there)

EDIT: Shouldn't be there an "unless state_stack is >= 2" or something like that to determine the amount of state? As I read it, it only erase a state when is exactly 1 stack.
You could try this modification then maybe (haven't tested it, so...)
Code:
def remove_state(state_id)
  if state?(state_id)
    if state_stack(state_id) == 1
      revive if state_id == death_state_id
      erase_state(state_id)
    else
      removal_index = @states.index(state_id)
      @states.delete_at(removal_index)  #Make sure an index was actually found when using this
    end
    refresh
    @result.removed_states.push(state_id).uniq!
  end
end

Both the forms
array - [x]
and
array.delete(x)
remove all occurrences of X.
Not directly related, but you can find a helpful documentation of Rubys standard library classes here https://ruby-doc.org/core-1.9.2/ if you want to look up a method.

The erase_state method is also used when a character becomes immune to a state, so having that method continue to clear all remaining stacks is probably a good call.
I tested it, and its not erasing states if I have 2 or higher stacks of the states.
But it erase it if its only one.
I thought about that, but wasn't sure how it stacked states and how to remove one.
You could try this modification then maybe (haven't tested it, so...)
Code:
def remove_state(state_id)
  if state?(state_id)
    if state_stack(state_id) == 1
      revive if state_id == death_state_id
      erase_state(state_id)
    else
      removal_index = @states.index(state_id)
      @states.delete_at(removal_index)  #Make sure an index was actually found when using this
    end
    refresh
    @result.removed_states.push(state_id).uniq!
  end
end

Both the forms
array - [x]
and
array.delete(x)
remove all occurrences of X.
Not directly related, but you can find a helpful documentation of Rubys standard library classes here https://ruby-doc.org/core-1.9.2/ if you want to look up a method.

The erase_state method is also used when a character becomes immune to a state, so having that method continue to clear all remaining stacks is probably a good call.
Thanks! It worked perfectly!

I thought about that, but wasn't sure how it stacked states and how to remove one.
Don't worry mate, you helped me so much already!
I find it weird that the script mod is working as intended because it isn't. It's still deleting the Whole State at once. Unless you can prove that the mod is creating state copies, it should simply delete the only one already present in the @states array.
I find it weird that the script mod is working as intended because it isn't. It's still deleting the Whole State at once. Unless you can prove that the mod is creating state copies, it should simply delete the only one already present in the @states array.
1658322073861.png

Welp, I added 3 instances of said state to 2 actors, and my enemy have only 1 skill that do a little damage and remove the state and this is the result after it use it skill:

1658322142036.png
It does work, but not well.
The stacking states script itself puts the states in an array and the removal removes the first index position of the state in the array.
The script doesn't account for state removal by expiration. So if you have a state that only lasts 5 turns, then when turn 5 comes up it removes all the same states, not just one.
If you have it so that the state is removed at the end of battle, then it should work fine.
Perhaps a better approach would be to simply add turns to an existing state.

Original thread: https://forums.rpgmakerweb.com/threads/removing-1-instance-of-a-state-state-stack.149642/