Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Changing the class through script

dre788

Member

So I've been fooling around with RGSS, doing and learning things I originally thought was impossible for me. Long story short, I've been trying to figure out how to change a character's class based on the person the player picks via item. I know this can be done by using conditional branches but the whole process would be very lengthy and I want to learn how to operate through RGSS better.

I'm sorry if this question doesn't qualify for it's own topic. I've used the search function and probed the help files but I wasn't able to find a answer.
 

dre788

Member

Thanks for the reply, I was hoping it would be you Sephiroth. :)

Odd, I couldn't get it to work with $game_actors... I had to use $game_party.actors... But any who, is there a way to change the class based on the person that is chosen for the item in the item menu? I figure it would have something to do with editing Scene_Item but I just burnt myself out trying to figure it out.
 

dre788

Member

Well, I placed the code into a common event, which is linked to a item. When the item is used on an actor via the item menu, I want the item to change the class of the actor that is picked.

This code  $game_actors[actor_id].class_id = id only changes a actor based on what number is placed in the []. I want the class to change base on what actor is chosen via the item menu.
 
Ok.

Try this:
Code:
#==============================================================================
# ** Items Effects - Class Change
#------------------------------------------------------------------------------
# SephirothSpawn
# Version 0.1
# 2008-12-09
#==============================================================================

class RPG::Item
  Class_Change_Effect = {}
  #--------------------------------------------------------------------------
  # * Setup
  #
  #  Step 1: Making an item change actor's class
  #
  #   Below # Start Mods Here, add
  #    - Class_Change_Effect[item_id] = {}
  #
  #    Replace item_id with the idem id from your database
  #
  #  Step 2: Making item change actor x's class to class y
  #
  #   Below your added line, add
  #    - Class_Change_Effect[item_id][x] = y
  #
  #    Replace x with your actor id and y with item id from your database
  #
  #  Repeat step 2 for desired actors.
  #
  #  Step 3: Default for actors (for non-defined actors from step 2)
  #
  #   Below your lines you added from step 2, add
  #    - Class_Change_Effect[item_id].default = y
  #
  #    Replace y with item id from your database
  #
  #  Repeat steps 1-3 for all items
  #
  # * Example:
  #   Item 1
  #    Actor 5's class to 6
  #    Actor 7's class to 1
  #    All other actors' class to 3
  #   Item 3
  #    Only actor 1's class to 4
  #
  #   Class_Change_Effect[1] = {}
  #   Class_Change_Effect[1][5] = 6
  #   Class_Change_Effect[1][7] = 1
  #   Class_Change_Effect[1].default = 3
  #   Class_Change_Effect[3] = {}
  #   Class_Change_Effect[3][1] = 4
  #--------------------------------------------------------------------------
  # Start Mods Here
  
  # End Mods Here
  Class_Change_Effect.default = {}
  def class_changes
    return Class_Change_Effect[@id]
  end
end

class Game_Battler
  alias_method :seph_itemeffectsclasschange_gmbtlr_ie, :item_effect
  def item_effect(item)
    result = seph_itemeffectsclasschange_gmbtlr_ie(item)
    class_changed = false
    if self.is_a?(Game_Actor)
      if self.damage != 'Miss'
        class_mods = item.class_changes
        unless [0, nil].include?(class_mods[@actor_id])
          self.class_id = class_mods[@actor_id]
          class_changed = true
        end
      end
    end
    return result || class_changed
  end
end

Instructions are in the script. Let me know if you have any difficulties or it doesn't work.
 

dre788

Member

Wow, it's works perfectly. Thank you so very much!! If you don't mind, do you think you can break down the script so I can understand it better? I want to try and expand on it myself so I can maybe one day become as awesome as you are.
 
Sure.

Code:
# Selects our RPG::Item object class so we can add to it
class RPG::Item
  # Creates a constant that holds our item data
  # each addition adds an item data, which is composed of actor ids and class ids.
  # final structure is CCE = item_id => {actor_id => class_id, ...}
  Class_Change_Effect = {}
  #--------------------------------------------------------------------------
  # * Setup
  #
  #  Step 1: Making an item change actor's class
  #
  #   Below # Start Mods Here, add
  #    - Class_Change_Effect[item_id] = {}
  #
  #    Replace item_id with the idem id from your database
  #
  #  Step 2: Making item change actor x's class to class y
  #
  #   Below your added line, add
  #    - Class_Change_Effect[item_id][x] = y
  #
  #    Replace x with your actor id and y with item id from your database
  #
  #  Repeat step 2 for desired actors.
  #
  #  Step 3: Default for actors (for non-defined actors from step 2)
  #
  #   Below your lines you added from step 2, add
  #    - Class_Change_Effect[item_id].default = y
  #
  #    Replace y with item id from your database
  #
  #  Repeat steps 1-3 for all items
  #
  # * Example:
  #   Item 1
  #    Actor 5's class to 6
  #    Actor 7's class to 1
  #    All other actors' class to 3
  #   Item 3
  #    Only actor 1's class to 4
  #
  #   Class_Change_Effect[1] = {}
  #   Class_Change_Effect[1][5] = 6
  #   Class_Change_Effect[1][7] = 1
  #   Class_Change_Effect[1].default = 3
  #   Class_Change_Effect[3] = {}
  #   Class_Change_Effect[3][1] = 4
  #--------------------------------------------------------------------------
  # Start Mods Here
  
  # End Mods Here
  # This just makes all items not defined have an empty list of actor and classes
  Class_Change_Effect.default = {}
  # This creates a method for retrieving data from our constant
  def class_changes
    # Returns a list of actor and class based off item id
    return Class_Change_Effect[@id]
  end
end

# This selects our battler class so we can add to it
class Game_Battler
  # This renames the item_effect method, a method called when using an item on an actor
  alias_method :seph_itemeffectsclasschange_gmbtlr_ie, :item_effect
  # Selects our item_effect method, which we pass an RPG::Item to. item is a local variable in your method
  # pointing to our RPG::Item
  def item_effect(item)
    # This calls our original item_effect method that we renamed. The original method
    # returns a result if the item is effective or not. We save the result
    result = seph_itemeffectsclasschange_gmbtlr_ie(item)
    # This makes a flag to check if class is changed (we use later)
    class_changed = false
    # If current battler is an actor (not enemy)
    if self.is_a?(Game_Actor)
      # If the item hits and doesn't miss
      if self.damage != 'Miss'
        # Calls our method we added in RPG::Item, retrieve our items class changes
        class_mods = item.class_changes
        # Here we check that list based off our actor_id. If nothing is found, we don't
        # do the next line
        unless [0, nil].include?(class_mods[@actor_id])
          # Change our class id
          self.class_id = class_mods[@actor_id]
          # Sets our class change flag to true
          class_changed = true
        end
      end
    end
    # Returns true if result is true or class_changed is true
    return result || class_changed
  end
end

That's pretty much all there is to it. :)
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top