Adding a new Bar for Charging up to cast spells

So I have this script I'm trying to get working. Everything is working except 2 things. I've add a new bar to make you wait 1 to 3 turns before you can cast spells depending on the charge points associated with the skill_id. I got the bar added its going up each turn but after I cast the spell its not reducing the charge point system by the charge point cost.

the other problem isn't a big deal but I'd still like to get it working if possible. might be a user error on my part. I'm trying to add a tag to the name to show the charge point cost if it applies. haven't messed with it to much. I may figure this one out soon but if anyone could point me in the right direction I'd appreciate it.



Code:
module CP
  MAX_CP        = 3     # maximum CP any battler can hold
  GAIN_PER_TURN = 1     # CP gained per turn end (per battler)

  # CP cost per skill id. If a skill id isn't here, it costs 0 CP.
  COST = {87 => 3
    # skill_id => cp_cost
    # Example entries: change to match your database
    # 1 => 1,
    # 17 => 2,
    # 28 => 3,
  }

  # Color for the CP gauge bar (left to right; two passes like HP/SP do)
  GAUGE_COLOR1 = Color.new( 80, 200, 255, 255)
  GAUGE_COLOR2 = Color.new( 20, 120, 200, 255)
  # Text color for CP number
  TEXT_COLOR   = Color.new( 80, 200, 255, 255)
end

#------------------------------------------------------------------------------
#  Game_Battler: add CP resource to actors & enemies
#------------------------------------------------------------------------------
class Game_Battler
  attr_accessor :cp

  alias cp_init_initialize initialize
  def initialize
    cp_init_initialize
    @cp = 0
  end

  def cp_max
    return CP::MAX_CP
  end

  def cp_gain(value)
    @cp = [[@cp + value, 0].max, cp_max].min
  end

  def cp_cost_for(skill_id)
    return CP::COST[skill_id] || 0
  end

  # Extra requirement check for CP on top of stock SP checks
  alias cp_skill_can_use? skill_can_use?
  def skill_can_use?(skill_id)
    return false unless cp_skill_can_use?(skill_id)
    return @cp >= cp_cost_for(skill_id)
  end
end

#------------------------------------------------------------------------------
#  Game_Actor / Game_Enemy: (inherits @cp from Game_Battler)
#   Nothing else needed here, but kept for clarity/extension.
#------------------------------------------------------------------------------
class Game_Actor < Game_Battler; end
class Game_Enemy < Game_Battler; end

#------------------------------------------------------------------------------
#  Scene_Battle: turn tick, battle start reset, and CP consumption on skill use
#------------------------------------------------------------------------------
class Scene_Battle
  # Reset all party actors' CP to 0 at battle start
  alias cp_start_phase1 start_phase1
  def start_phase1
    $game_party.actors.each { |a| a.cp = 0 if a }
    cp_start_phase1
  end

  # At turn end (phase 4), everyone gains CP
  alias cp_start_phase4 start_phase4
  def start_phase4
    # All living battlers (actors + enemies) gain CP
    for actor in $game_party.actors
      next if actor.nil?
      actor.cp_gain(CP::GAIN_PER_TURN)
    end
    for enemy in $game_troop.enemies
      next if enemy.nil?
      enemy.cp_gain(CP::GAIN_PER_TURN)
    end
    cp_start_phase4
  end

  # When executing a skill action, also deduct CP cost
  alias cp_make_skill_action_result make_skill_action_result
  def make_skill_action_result
    cp_make_skill_action_result
    # If a skill was actually executed, deduct CP (matching SP deduction timing)
    if @skill != nil && @active_battler != nil
      cost = @active_battler.cp_cost_for(@skill)
      @active_battler.cp = [@active_battler.cp - cost, 0].max
    end
  end
end

#------------------------------------------------------------------------------
#  Window_Base: helper to draw CP gauge & number
#------------------------------------------------------------------------------
class Window_Base < Window
  # Simple gauge drawer similar to HP/SP bar (integer steps, 0..MAX_CP)
  def draw_actor_cp_gauge(actor, x, y, width = 150)
    # Background gauge box
    self.contents.fill_rect(x, y + 12, width, 6, Color.new(0,0,0,64))
    # Fill proportional to CP
    rate = (actor.cp_max == 0) ? 0.0 : actor.cp.to_f / actor.cp_max
    w = (width * rate).to_i
    # Two-pass fill for a subtle gradient
    self.contents.fill_rect(x, y + 12, w, 6, CP::GAUGE_COLOR2)
    self.contents.fill_rect(x, y + 12, w, 3, CP::GAUGE_COLOR1)
    # Label + value (e.g., "CP 2/3")
    self.contents.font.color = system_color
    self.contents.draw_text(x - 16, y - 10, 28, 24, "Charge")
    self.contents.font.color = CP::TEXT_COLOR
    text = "#{actor.cp}/#{actor.cp_max}"
    self.contents.draw_text(x + width - 48, y - 10, 48, 24, text, 2)
    self.contents.font.color = normal_color
  end
end

#------------------------------------------------------------------------------
#  Window_BattleStatus: add CP line under HP/SP
#   (Keeps layout tight for RMXP default window width)
#------------------------------------------------------------------------------
class Window_BattleStatus < Window_Base
  alias cp_refresh refresh
  def refresh
    cp_refresh
    # Draw CP for each actor in their block
    @item_max = $game_party.actors.size
    for i in 0...@item_max
      actor = $game_party.actors[i]
      next if actor.nil?
      x = i * 160 + 280  # default RMXP layout width per battler block
      y = 6           # place CP below HP/SP lines
      draw_actor_cp_gauge(actor, x, y, 120)
    end
  end
end

#------------------------------------------------------------------------------
#  Window_Skill: gray out skills you can't afford in CP (as well as SP)
#------------------------------------------------------------------------------
class Window_Skill < Window_Selectable
  alias cp_draw_item draw_item
  def draw_item(index)
    skill = @data[index]
    if skill == nil
      return
    end
    actor = @actor
    enabled = actor.skill_can_use?(skill)
    # Temporarily dim text if not usable due to CP/SP
    self.contents.font.color = enabled ? normal_color : disabled_color
    cp_draw_item(index)
    self.contents.font.color = normal_color
  end
end

#------------------------------------------------------------------------------
#  UI hint: show CP cost on skill list (optional). Uncomment to append [CPx].
#------------------------------------------------------------------------------
class Window_Skill < Window_Selectable
   alias cp_draw_item_text draw_item
   def draw_item(index)
     skill = @data[index]
     return if skill.nil?
     # Build name with CP tag
     cost = @actor.cp_cost_for(skill)
     name = skill.name.dup
     name += " [CP#{cost}]" if cost > 0
     # Draw with augmented name (temporarily swap)
     original = skill.name
     skill.name = name
     cp_draw_item_text(index)
     skill.name = original
   end
end
Issue #1

Here's a working version of your script.

Ruby:
module CP
  MAX_CP        = 3     # maximum CP any battler can hold
  GAIN_PER_TURN = 1     # CP gained per turn end (per battler)

  # CP cost per skill id. If a skill id isn't here, it costs 0 CP.
  COST = {87 => 3
    # skill_id => cp_cost
    # Example entries: change to match your database
    # 1 => 1,
    # 17 => 2,
    # 28 => 3,
  }

  # Color for the CP gauge bar (left to right; two passes like HP/SP do)
  GAUGE_COLOR1 = Color.new( 80, 200, 255, 255)
  GAUGE_COLOR2 = Color.new( 20, 120, 200, 255)
  # Text color for CP number
  TEXT_COLOR   = Color.new( 80, 200, 255, 255)
end

#------------------------------------------------------------------------------
#  Game_Battler: add CP resource to actors & enemies
#------------------------------------------------------------------------------
class Game_Battler
  attr_accessor :cp
  alias cp_init_initialize initialize
  def initialize
    cp_init_initialize
    @cp = 0
  end

  def cp_max
    return CP::MAX_CP
  end

  def cp_gain(value)
    @cp = [[@cp + value, 0].max, cp_max].min
  end

  def cp_cost_for(skill_id)
    return CP::COST[skill_id] || 0
  end

  def deduct_cp_cost(skill_id)
    cost = cp_cost_for(skill_id)
    @cp = [@cp - cost, 0].max
  end

  # Extra requirement check for CP on top of stock SP checks
  alias cp_skill_can_use? skill_can_use?
  def skill_can_use?(skill_id)
    return false unless cp_skill_can_use?(skill_id)
    return @cp >= cp_cost_for(skill_id)
  end
end

#------------------------------------------------------------------------------
#  Scene_Battle: turn tick, battle start reset, and CP consumption on skill use
#------------------------------------------------------------------------------
class Scene_Battle
  # Reset all party actors' CP to 0 at battle start
  alias cp_start_phase1 start_phase1
  def start_phase1
    $game_party.actors.each { |a| a.cp = 0 if a }
    cp_start_phase1
  end

  # At turn end (phase 4), everyone gains CP
  alias cp_start_phase4 start_phase4
  def start_phase4
    # All living battlers (actors + enemies) gain CP
    for actor in $game_party.actors
      next if actor.nil?
      actor.cp_gain(CP::GAIN_PER_TURN)
    end
    for enemy in $game_troop.enemies
      next if enemy.nil?
      enemy.cp_gain(CP::GAIN_PER_TURN)
    end
    cp_start_phase4
  end

  # When executing a skill action, also deduct CP cost
  alias cp_make_skill_action_result make_skill_action_result
  def make_skill_action_result
    old_sp = @active_battler.sp
    cp_make_skill_action_result
    # Deduct CP (matching SP deduction timing)
    @active_battler.deduct_cp_cost(@skill.id) if @active_battler.sp < old_sp
  end
end

#------------------------------------------------------------------------------
#  Window_Base: helper to draw CP gauge & number
#------------------------------------------------------------------------------
class Window_Base < Window
  # Simple gauge drawer similar to HP/SP bar (integer steps, 0..MAX_CP)
  def draw_actor_cp_gauge(actor, x, y, width = 150)
    # Background gauge box
    self.contents.fill_rect(x, y + 12, width, 6, Color.new(0,0,0,64))
    # Fill proportional to CP
    rate = (actor.cp_max == 0) ? 0.0 : actor.cp.to_f / actor.cp_max
    w = (width * rate).to_i
    # Two-pass fill for a subtle gradient
    self.contents.fill_rect(x, y + 12, w, 6, CP::GAUGE_COLOR2)
    self.contents.fill_rect(x, y + 12, w, 3, CP::GAUGE_COLOR1)
    # Label + value (e.g., "CP 2/3")
    self.contents.font.color = system_color
    self.contents.draw_text(x - 16, y - 10, 28, 24, "Charge")
    self.contents.font.color = CP::TEXT_COLOR
    text = "#{actor.cp}/#{actor.cp_max}"
    self.contents.draw_text(x + width - 48, y - 10, 48, 24, text, 2)
    self.contents.font.color = normal_color
  end
end

#------------------------------------------------------------------------------
#  Window_BattleStatus: add CP line under HP/SP
#   (Keeps layout tight for RMXP default window width)
#------------------------------------------------------------------------------
class Window_BattleStatus < Window_Base
  alias cp_refresh refresh
  def refresh
    cp_refresh
    # Draw CP for each actor in their block
    @item_max = $game_party.actors.size
    for i in 0...@item_max
      actor = $game_party.actors[i]
      next if actor.nil?
      x = i * 160 + 280  # default RMXP layout width per battler block
      y = 6           # place CP below HP/SP lines
      draw_actor_cp_gauge(actor, x, y, 120)
    end
  end
end

#------------------------------------------------------------------------------
#  Window_Skill: gray out skills you can't afford in CP (as well as SP)
#------------------------------------------------------------------------------
class Window_Skill < Window_Selectable
  alias cp_draw_item draw_item
  def draw_item(index)
    skill = @data[index]
    if skill == nil
      return
    end
    actor = @actor
    enabled = actor.skill_can_use?(skill)
    # Temporarily dim text if not usable due to CP/SP
    self.contents.font.color = enabled ? normal_color : disabled_color
    cp_draw_item(index)
    self.contents.font.color = normal_color
  end
end

#------------------------------------------------------------------------------
#  UI hint: show CP cost on skill list (optional). Uncomment to append [CPx].
#------------------------------------------------------------------------------
class Window_Skill < Window_Selectable
   alias cp_draw_item_text draw_item
   def draw_item(index)
     skill = @data[index]
     return if skill.nil?
     # Build name with CP tag
     cost = @actor.cp_cost_for(skill)
     name = skill.name.dup
     name += " [CP#{cost}]" if cost > 0
     # Draw with augmented name (temporarily swap)
     original = skill.name
     skill.name = name
     cp_draw_item_text(index)
     skill.name = original
   end
end

But I still ask you to remove one of the draw_item method's definitions. It doesn't make any sense to keep them both.

EDIT: You could simply merge them.

The following part has been removed. There's no point in leaving those lines there to do nothing. Not even for keeping in mind what you were doing. You altered Game_Battler class for a reason, and it's obvious that its child classes will inherit those changes.

Ruby:
class Game_Actor < Game_Battler; end
class Game_Enemy < Game_Battler; end

When you make many calls to a single object, it's better to reduce the external calls to 1 and let that method call take care of the rest. Here I'm leaving an extract so you can take a quick look at those changes.

Ruby:
class Game_Battler
  def deduct_cp_cost(skill)
    cost = cp_cost_for(skill)
    @cp = [@cp - cost, 0].max
  end
end

class Scene_Battle
  def make_skill_action_result
    old_sp = @active_battler.sp
    cp_make_skill_action_result
    # Deduct CP (matching SP deduction timing)
    @active_battler.deduct_cp_cost(@skill.id) if @active_battler.sp < old_sp
    @status_window.refresh
  end
end

And here comes the main issue with your original code: you were passing the whole @skill instead of its @skill.id in the make_skill_action_result method. It was impossible for the script to find a skill because the COST constant was depending on skill ID's to define the skill's CP cost instead.

Actually, the call to @status_window.refresh seems to be optional here. You can remove it after testing the modified script a couple of times.

SIDE NOTE

Did you download the KLastDefeat XP recently? I had to upload a new demo on August 24th to fix a bug.
You have got to be my favorite person on here. I'm defiantly a noob when it comes to this stuff and it shows. I swear you have the answer to every little problem I have. That solved my 3 hour problem lol. I try my hardest not to bother people on here till I give it my best shot.

I did try to use KLastDefeat XP but something was happening where the event only popped up if I won the fight. It did move the event from another map to the new map but it didn't retain the pages of the event. It could have been something I was doing so I was going to download the demo to check it out but my computer said no. In my defense I've never download a demo off of here. it said something along the lines of it could have a virus so I left it alone. not saying it did but that is the message I got. I don't think I tried the updated version though so I still need to go back and give it a go. I work a lot so just messing with this in my free time.

I got to ask man how long have you been coding in ruby for?

Original thread: https://forums.rpgmakerweb.com/threads/adding-a-new-bar-for-charging-up-to-cast-spells.179917/