Randomized Stats on Level Up 2.20 (Updated 03/16/11!)

Randomized Stats on Level Up Version: 2.20
By: Glitchfinder


Introduction

Well, I finished an interesting script for a request a couple of hours ago, and the concept really intrigued me. So, I took that script, and I simplified it so that it is easier to use with the default database. (although it no longer fits the request) It is actually a script that will partially randomize a character's stat gains once they level up, or if they must level down. All of the instructions are included in the header. Anyway, on to the script!

Features

  • Adds variation to an otherwise dull portion of the game!
  • Allows the programmer to set how much the stats can vary!
  • takes the base values from the database!

Screenshots

Since this script has no visual elements, a screenshot cannot be taken.

Script

RMXP Edition
  1. #≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
  2. # ** Glitchfinder's Randomized Stats on Level Up                 [RPG Maker XP]
  3. #    Version 2.20
  4. #------------------------------------------------------------------------------
  5. #  This script is meant to help randomize an actor's stats upon leveling up.
  6. #==============================================================================
  7. # * Version History
  8. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  9. #   Version 1.00 ------------------------------------------------- (2008-07-11)
  10. #     - Initial version
  11. #     - Author: Glitchfinder
  12. #   Version 2.00 ------------------------------------------------- (2008-07-13)
  13. #     - Script completely rewritten to use hashes
  14. #     - Author: Glitchfinder
  15. #    Version 2.10 ------------------------------------------------ (2008-07-14)
  16. #      - Fixed various errors
  17. #      - Cleaned code
  18. #      - Author: Glitchfinder
  19. #    Version 2.20 ----=------------------------------------------- (2011-03-16)
  20. #      - Added better documentation
  21. #      - Fixed crash on F12
  22. #      - Added missing return value to base_sp()
  23. #      - Modified stat calculations for clarity
  24. #      - Author: Glitchfinder
  25. #==============================================================================
  26. # * Instructions
  27. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  28. #  Place this script above Main, and below the default scripts. (I realize this
  29. #  is obvious to most, but some people don't get it.)
  30. #
  31. #  To modify the basic starting stats of a character, you must edit the
  32. #  Actor_Stats array in the Stats module, using the following rules:
  33. #    * Line 1 is the stats of the first actor in the database. (Aluxes is the
  34. #      default) Line 2 is the second actor (Basil), etc.
  35. #    * The statistics are in order on each line, where the first entry is
  36. #      maximum HP, entry 2 is maximum MP, entry 3 is Strength, entry 4 is
  37. #      Dexterity, entry 5 is Agility, and entry 6 is Intelligence.
  38. #
  39. #  To modify the variation of an actor's statistics upon changing levels, you
  40. #  must edit the Stats hash in the Stats module, using the following rules:
  41. #    * The value in each entry is the maximum that the new stat may vary from
  42. #      the default increase or decrease defined for that stat in the Database.
  43. #      (Calculated by subtracting the default value of the new level from the
  44. #      default value of the original level) In other words, if the default
  45. #      change is three, and you have the variation set to two, then the stat
  46. #      can go up anywhere between one and five points.
  47. #    * When leveling up, a stat cannot go down, although it may also remain
  48. #      unchanged. This is because if the random number chosen by the script is
  49. #      negative when levelling up, the stat is left unchaged instead of being
  50. #      changed in the opposite direction of that intended.
  51. #
  52. #  If you modify the default actors within the game, you must also modify this
  53. #  script, so that the actors will all have their correct stats. If you add
  54. #  actors, you must add new lines to this script, and vice-versa if you delete
  55. #  actors. These new lines should be in the Actor_Stats array and the Stats
  56. #  hash within the Stats module.
  57. #==============================================================================
  58. # * Advice
  59. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  60. #  This script is meant for people with at least basic scripting knowledge. It
  61. #  may be difficult to use, and thus, if you are unsure of your abilities, it
  62. #  may be simpler to avoid using this script.
  63. #
  64. #  Do not make the values in the Stats hash too high. This can result in a game
  65. #  becoming too easy or difficult for the player, due to stats that are either
  66. #  too high or too low. For most stats, 2 or 3 should be more than enough
  67. #  variance.
  68. #
  69. #  Edit the default values that I have placed in this script. Although it may
  70. #  be difficult to properly balance a character, it is certain that at least
  71. #  some of the characters will be unbalanced if they are using the default
  72. #  values I placed within the Stats hashes.
  73. #==============================================================================
  74. # * Contact
  75. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  76. #  Glitchfinder, the author of this script, may be contacted through his
  77. #  website, found at http://www.glitchkey.com
  78. #
  79. #  You may also find Glitchfinder at http://www.hbgames.org
  80. #==============================================================================
  81. # * Usage
  82. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  83. #  This script may be used with the following terms and conditions:
  84. #
  85. #    1. This script is free to use in any noncommercial project. If you wish to
  86. #       use this script in a commercial (paid) project, please contact
  87. #       Glitchfinder at his website.
  88. #    2. This script may only be hosted at the following domains:
  89. #         http://www.glitchkey.com
  90. #         http://www.hbgames.org
  91. #    3. If you wish to host this script elsewhere, please contact Glitchfinder.
  92. #    4. If you wish to translate this script, please contact Glitchfinder. He
  93. #       will need the web address that you plan to host the script at, as well
  94. #       as the language this script is being translated to.
  95. #    5. This header must remain intact at all times.
  96. #    6. Glitchfinder remains the sole owner of this code. He may modify or
  97. #       revoke this license at any time, for any reason.
  98. #    7. Any code derived from code within this script is owned by Glitchfinder,
  99. #       and you must have his permission to publish, host, or distribute his
  100. #       code.
  101. #    8. This license applies to all code derived from the code within this
  102. #       script.
  103. #    9. If you use this script within your project, you must include visible
  104. #       credit to Glitchfinder, within reason.
  105. #≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
  106.  
  107. #==============================================================================
  108. # ** Stats
  109. #------------------------------------------------------------------------------
  110. #  This module handles actors' stats and their changes. Its used
  111. # within the Game_Actor class.
  112. #==============================================================================
  113.  
  114. module Stats
  115.   #--------------------------------------------------------------------------
  116.   # * Store Actor Stats
  117.   #--------------------------------------------------------------------------
  118.   Actor_Stats = [
  119.     # HP,  SP,  STR, DEX, AGI, INT
  120.       741, 534, 67,  54,  58,  36,
  121.       695, 591, 60,  65,  62,  31,
  122.       747, 435, 76,  56,  52,  37,
  123.       546, 590, 41,  73,  72,  32,
  124.       590, 570, 50,  79,  62,  48,
  125.       656, 531, 51,  62,  50,  33,
  126.       486, 661, 43,  55,  55,  64,
  127.       422, 606, 31,  30,  46,  71
  128.   ]
  129.   #--------------------------------------------------------------------------
  130.   # * Name Actor Stats
  131.   #--------------------------------------------------------------------------
  132.   Stat_Names = ['hp', 'sp', 'str', 'dex', 'agi', 'int']
  133.   #--------------------------------------------------------------------------
  134.   # * Set Actor Stat Variance
  135.   #--------------------------------------------------------------------------
  136.   Stats = {}
  137.   # Actor 1 setup
  138.   Stats[1] = {
  139.     'hp' => 26,
  140.     'sp' => 4,
  141.     'str' => 3,
  142.     'dex' => 3,
  143.     'agi' => 2,
  144.     'int' => 2
  145.   }
  146.   # Default actor setup
  147.   Stats.default = {
  148.     'hp' => 24,
  149.     'sp' => 3,
  150.     'str' => 2,
  151.     'dex' => 2,
  152.     'agi' => 2,
  153.     'int' => 2
  154.   }
  155. end
  156.  
  157. #==============================================================================
  158. # ** Game_Actor
  159. #------------------------------------------------------------------------------
  160. #  This class handles the actor. Its used within the Game_Actors class
  161. #  ($game_actors) and refers to the Game_Party class ($game_party).
  162. #==============================================================================
  163. class Game_Actor < Game_Battler
  164.   #--------------------------------------------------------------------------
  165.   # * Convert Stat Names to their System Numbers
  166.   #--------------------------------------------------------------------------
  167.   Stat_Numbers = {
  168.     'hp' => 0,
  169.     'sp' => 1,
  170.     'str' => 2,
  171.     'dex' => 3,
  172.     'agi' => 4,
  173.     'int' => 5
  174.   }
  175.   #--------------------------------------------------------------------------
  176.   # * Alias Methods
  177.   #--------------------------------------------------------------------------
  178.   # If the setup method has not been aliased
  179.   unless method_defined?(:randomstatgains_setup)
  180.     # Alias the enemy selection method
  181.     alias randomstatgains_setup setup
  182.   end
  183.   #--------------------------------------------------------------------------
  184.   # * Setup
  185.   #     actor_id : actor ID
  186.   #--------------------------------------------------------------------------
  187.   def setup(actor_id)
  188.     # Create an empty stat hash
  189.     @stat_gains = {}
  190.     # Iterate through all stats
  191.     for stat in Stats::Stat_Names
  192.       # Add the stat gain to the hash
  193.       @stat_gains[stat] = []
  194.     end
  195.     # Setup stat gains
  196.     randomstatgains_setup(actor_id)
  197.   end
  198.   #--------------------------------------------------------------------------
  199.   # * Get Basic Maximum HP
  200.   #--------------------------------------------------------------------------
  201.   def base_maxhp
  202.     # Calculate the array index of the stat
  203.     stat = (4 * @actor_id) + ((2 * (@actor_id - 2)) - 2)
  204.     # Get the actor's stat
  205.     n =  Stats::Actor_Stats[stat.to_i].to_i
  206.     # Add the stat gains to the value
  207.     n += @stat_gains['hp'].sum
  208.     return n
  209.   end
  210.   #--------------------------------------------------------------------------
  211.   # * Get basic Maximum MP
  212.   #--------------------------------------------------------------------------
  213.   def base_maxmp
  214.     # Calculate the array index of the stat
  215.     stat = (4 * @actor_id) + ((2 * (@actor_id - 2)) - 1)
  216.     # Get the actor's stat
  217.     n = Stats::Actor_Stats[stat.to_i].to_i
  218.     # Add the stat gains to the value
  219.     n += @stat_gains['sp'].sum
  220.     return n
  221.   end
  222.   #--------------------------------------------------------------------------
  223.   # * Get Basic Strength
  224.   #--------------------------------------------------------------------------
  225.   def base_str
  226.     # Calculate the array index of the stat
  227.     stat = (4 * @actor_id) + (2 * (@actor_id - 2))
  228.     # Get the actor's stat
  229.     n = Stats::Actor_Stats[stat.to_i].to_i
  230.     # Add the stat gains to the value
  231.     n += @stat_gains['str'].sum
  232.     # Get the current weapon and armor data
  233.     weapon = $data_weapons[@weapon_id]
  234.     armor1 = $data_armors[@armor1_id]
  235.     armor2 = $data_armors[@armor2_id]
  236.     armor3 = $data_armors[@armor3_id]
  237.     armor4 = $data_armors[@armor4_id]
  238.     # Add the weapon and armor stat boosts
  239.     n += weapon != nil ? weapon.str_plus : 0
  240.     n += armor1 != nil ? armor1.str_plus : 0
  241.     n += armor2 != nil ? armor2.str_plus : 0
  242.     n += armor3 != nil ? armor3.str_plus : 0
  243.     n += armor4 != nil ? armor4.str_plus : 0
  244.     # Return the stat value, with a maximum of 999 and a minimum of 1
  245.     return [[n, 1].max, 999].min
  246.   end
  247.   #--------------------------------------------------------------------------
  248.   # * Get Basic Dexterity
  249.   #--------------------------------------------------------------------------
  250.   def base_dex
  251.     # Calculate the array index of the stat
  252.     stat = (4 * @actor_id) + ((2 * (@actor_id - 2)) + 1)
  253.     # Get the actor's stat
  254.     n = Stats::Actor_Stats[stat.to_i].to_i
  255.     # Add the stat gains to the value
  256.     n += @stat_gains['dex'].sum
  257.     # Get the current weapon and armor data
  258.     weapon = $data_weapons[@weapon_id]
  259.     armor1 = $data_armors[@armor1_id]
  260.     armor2 = $data_armors[@armor2_id]
  261.     armor3 = $data_armors[@armor3_id]
  262.     armor4 = $data_armors[@armor4_id]
  263.     # Add the weapon and armor stat boosts
  264.     n += weapon != nil ? weapon.dex_plus : 0
  265.     n += armor1 != nil ? armor1.dex_plus : 0
  266.     n += armor2 != nil ? armor2.dex_plus : 0
  267.     n += armor3 != nil ? armor3.dex_plus : 0
  268.     n += armor4 != nil ? armor4.dex_plus : 0
  269.     # Return the stat value, with a maximum of 999 and a minimum of 1
  270.     return [[n, 1].max, 999].min
  271.   end
  272.   #--------------------------------------------------------------------------
  273.   # * Get Basic Agility
  274.   #--------------------------------------------------------------------------
  275.   def base_agi
  276.     # Calculate the array index of the stat
  277.     stat = (4 * @actor_id) + ((2 * (@actor_id - 2)) + 2)
  278.     # Get the actor's stat
  279.     n = Stats::Actor_Stats[stat.to_i].to_i
  280.     # Add the stat gains to the value
  281.     n += @stat_gains['agi'].sum
  282.     # Get the current weapon and armor data
  283.     weapon = $data_weapons[@weapon_id]
  284.     armor1 = $data_armors[@armor1_id]
  285.     armor2 = $data_armors[@armor2_id]
  286.     armor3 = $data_armors[@armor3_id]
  287.     armor4 = $data_armors[@armor4_id]
  288.     # Add the weapon and armor stat boosts
  289.     n += weapon != nil ? weapon.agi_plus : 0
  290.     n += armor1 != nil ? armor1.agi_plus : 0
  291.     n += armor2 != nil ? armor2.agi_plus : 0
  292.     n += armor3 != nil ? armor3.agi_plus : 0
  293.     n += armor4 != nil ? armor4.agi_plus : 0
  294.     # Return the stat value, with a maximum of 999 and a minimum of 1
  295.     return [[n, 1].max, 999].min
  296.   end
  297.   #--------------------------------------------------------------------------
  298.   # * Get Basic Intelligence
  299.   #--------------------------------------------------------------------------
  300.   def base_int
  301.     # Calculate the array index of the stat
  302.     stat = (4 * @actor_id) + ((2 * (@actor_id - 2)) + 3)
  303.     # Get the actor's stat
  304.     n = Stats::Actor_Stats[stat.to_i].to_i
  305.     # Add the stat gains to the value
  306.     n += @stat_gains['int'].sum
  307.     # Get the current weapon and armor data
  308.     weapon = $data_weapons[@weapon_id]
  309.     armor1 = $data_armors[@armor1_id]
  310.     armor2 = $data_armors[@armor2_id]
  311.     armor3 = $data_armors[@armor3_id]
  312.     armor4 = $data_armors[@armor4_id]
  313.     # Add the weapon and armor stat boosts
  314.     n += weapon != nil ? weapon.int_plus : 0
  315.     n += armor1 != nil ? armor1.int_plus : 0
  316.     n += armor2 != nil ? armor2.int_plus : 0
  317.     n += armor3 != nil ? armor3.int_plus : 0
  318.     n += armor4 != nil ? armor4.int_plus : 0
  319.     # Return the stat value, with a maximum of 999 and a minimum of 1
  320.     return [[n, 1].max, 999].min
  321.   end
  322.   #--------------------------------------------------------------------------
  323.   # * Change EXP
  324.   #     exp : new EXP
  325.   #--------------------------------------------------------------------------
  326.   def exp=(exp)
  327.     # Force experience within bounds
  328.     @exp = [[exp, 9999999].min, 0].max
  329.     # Level up
  330.     while @exp >= @exp_list[@level+1] and @exp_list[@level+1] > 0
  331.       # Increase the actor level
  332.       @level += 1
  333.       # Iterate through each stat
  334.       Stats::Stat_Names.each do |stat_name|
  335.         # Calculate the new stat gain
  336.         n = random_gain(@actor_id, stat_name)
  337.         # Add the new gain to the actor's stat gains
  338.         @stat_gains[stat_name] << n
  339.       end
  340.       # Iterate through the class skill list
  341.       for skill in $data_classes[@class_id].learnings
  342.         # If the actor learned a new skill
  343.         if skill.level == @level
  344.           # Teach the actor the new skill
  345.           learn_skill(skill.skill_id)
  346.         end
  347.       end
  348.     end
  349.     # Level down
  350.     while @exp < @exp_list[@level]
  351.       # Decrease the actor level
  352.       @level -= 1
  353.       # Iterate through each stat
  354.       Stats::Stat_Names.each do |stat_name|
  355.         # Remove the latest gains from the stat
  356.         @stat_gains[stat_name].pop
  357.       end
  358.     end
  359.     # Correct the actor's maximum HP and maximum SP
  360.     @hp = [@hp, self.maxhp].min
  361.     @sp = [@sp, self.maxsp].min
  362.   end
  363.   #--------------------------------------------------------------------------
  364.   # * Determine Random Stat Gain
  365.   #--------------------------------------------------------------------------
  366.   def random_gain(actor_id, stat_name)
  367.     # Begin error checking
  368.     begin
  369.       # Get the stat number
  370.       s_number = Stat_Numbers[stat_name].to_i
  371.       # Get the default stat change
  372.       default_change = ($data_actors[actor_id].parameters[s_number, @level] -
  373.         $data_actors[actor_id].parameters[s_number, @level - 1])
  374.       # Calculate the stat changes
  375.       change = rand(Stats::Stats[actor_id][stat_name])
  376.       # Calculate a random number
  377.       random = rand(2)
  378.       # If the random number is even
  379.       if (1 != (random % 2))
  380.         # Set the random change to a negative value
  381.         change *= -1
  382.       end
  383.       # Add the default change to the random change
  384.       change += default_change
  385.       # If the change is negative
  386.       if change <= 0
  387.         # Set the change to 0
  388.         return 0
  389.       # If the change is positive
  390.       else
  391.         # Return the newly calculated stat change
  392.         return change
  393.       end
  394.     # If an error occurs
  395.     rescue
  396.       # Print an error message
  397.       p 'Undefined stat_name: ' + stat_name + ' for actor: ' + actor_id.to_s
  398.       # Return that no stat change has occurred
  399.       return 0
  400.     end
  401.   end
  402. end
  403.  
  404. #==============================================================================
  405. # ** Array
  406. #------------------------------------------------------------------------------
  407. #  This hidden class handles arrays. It is used within many other
  408. # classes to store data for future retrieval.
  409. #==============================================================================
  410.  
  411. class Array
  412.   #---------------------------------------------------------------------------
  413.   # * Name      : Sum
  414.   #   Info      : Sums all values in the array
  415.   #   Author    : Trickster
  416.   #   Call Info : No Arguments
  417.   #---------------------------------------------------------------------------
  418.   def sum
  419.     # Initialize local variable n
  420.     n = 0
  421.     # Sum Up Values in Array
  422.     each {|num| n += num}
  423.     # Return number
  424.     return n
  425.   end
  426. end
Expand to see the code.


RMVX Edition
  1. #≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
  2. # ** Glitchfinder's Randomized Stats on Level Up                 [RPG Maker VX]
  3. #    Version 2.20
  4. #------------------------------------------------------------------------------
  5. #  This script is meant to help randomize an actor's stats upon levelling up.
  6. #==============================================================================
  7. # * Version History
  8. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  9. #   Version 1.00 ------------------------------------------------- (2008-07-11)
  10. #     - Initial version
  11. #     - Author: Glitchfinder
  12. #   Version 2.00 ------------------------------------------------- (2008-07-13)
  13. #     - Script completely rewritten to use hashes
  14. #     - Author: Glitchfinder
  15. #    Version 2.10 ------------------------------------------------ (2008-07-14)
  16. #      - Fixed various errors
  17. #      - Cleaned code
  18. #      - Author: Glitchfinder
  19. #    Version 2.20 ----=------------------------------------------- (2011-03-16)
  20. #      - Added better documentation
  21. #      - Fixed crash on F12
  22. #      - Added missing return value to base_mp()
  23. #      - Modified stat calculations for clarity
  24. #      - Author: Glitchfinder
  25. #==============================================================================
  26. # * Instructions
  27. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  28. #  Place this script above Main, and below the default scripts. (I realize this
  29. #  is obvious to most, but some people don't get it.)
  30. #
  31. #  To modify the basic starting stats of a character, you must edit the
  32. #  Actor_Stats array in the Stats module, using the following rules:
  33. #    * Line 1 is the stats of the first actor in the database. (Ralph is the
  34. #      default) Line 2 is the second actor (Ulrika), etc.
  35. #    * The statistics are in order on each line, where the first entry is
  36. #      maximum HP, entry 2 is maximum MP, entry 3 is Attack, entry 4 is
  37. #      Defense, entry 5 is Spirit, and entry 6 is Agility.
  38. #
  39. #  To modify the variation of an actor's statistics upon changing levels, you
  40. #  must edit the Stats hash in the Stats module, using the following rules:
  41. #    * The value in each entry is the maximum that the new stat may vary from
  42. #      the default increase or decrease defined for that stat in the Database.
  43. #      (Calculated by subtracting the default value of the new level from the
  44. #      default value of the original level) In other words, if the default
  45. #      change is three, and you have the variation set to two, then the stat
  46. #      can go up anywhere between one and five points.
  47. #    * When leveling up, a stat cannot go down, although it may also remain
  48. #      unchanged. This is because if the random number chosen by the script is
  49. #      negative when leveling up, the stat is left unchaged instead of being
  50. #      changed in the opposite direction of that intended.
  51. #
  52. #  If you modify the default actors within the game, you must also modify this
  53. #  script, so that the actors will all have their correct stats. If you add
  54. #  actors, you must add new lines to this script, and vice-versa if you delete
  55. #  actors. These new lines should be in the Stats hash within the Stats module.
  56. #==============================================================================
  57. # * Glitchfinder's Advice
  58. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  59. #  Do not make the values in the Stats hash too high. This can result in a game
  60. #  becoming too easy or difficult for the player, due to stats that are either
  61. #  too high or too low. For most stats, 2 or 3 should be more than enough
  62. #  variance.
  63. #
  64. #  Edit the default values that I have placed in this script. Although it may
  65. #  be difficult to properly balance a character, it is certain that at least
  66. #  some of the characters will be unbalanced if they are using the default
  67. #  values I placed within the Stat_Change_Variance array.
  68. #==============================================================================
  69. # * Contact
  70. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  71. #  Glitchfinder, the author of this script, may be contacted through his
  72. #  website, found at http://www.glitchkey.com
  73. #
  74. #  You may also find Glitchfinder at http://www.hbgames.org
  75. #==============================================================================
  76. # * Usage
  77. #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  78. #  This script may be used with the following terms and conditions:
  79. #
  80. #    1. This script is free to use in any noncommercial project. If you wish to
  81. #       use this script in a commercial (paid) project, please contact
  82. #       Glitchfinder at his website.
  83. #    2. This script may only be hosted at the following domains:
  84. #         http://www.glitchkey.com
  85. #         http://www.hbgames.org
  86. #    3. If you wish to host this script elsewhere, please contact Glitchfinder.
  87. #    4. If you wish to translate this script, please contact Glitchfinder. He
  88. #       will need the web address that you plan to host the script at, as well
  89. #       as the language this script is being translated to.
  90. #    5. This header must remain intact at all times.
  91. #    6. Glitchfinder remains the sole owner of this code. He may modify or
  92. #       revoke this license at any time, for any reason.
  93. #    7. Any code derived from code within this script is owned by Glitchfinder,
  94. #       and you must have his permission to publish, host, or distribute his
  95. #       code.
  96. #    8. This license applies to all code derived from the code within this
  97. #       script.
  98. #    9. If you use this script within your project, you must include visible
  99. #       credit to Glitchfinder, within reason.
  100. #≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
  101.  
  102. #==============================================================================
  103. # ** Stats
  104. #------------------------------------------------------------------------------
  105. #  This module handles actors' stats and their changes. It's used within the
  106. #  Game_Actor class.
  107. #==============================================================================
  108.  
  109. module Stats
  110.   #--------------------------------------------------------------------------
  111.   # * Store Actor Stats
  112.   #--------------------------------------------------------------------------
  113.   Actor_Stats = [
  114.     # HP,  MP,  ATK, DEF, SPI, AGI
  115.       466, 70,  17,  17,  21,  23,
  116.       594, 70,  22,  16,  11,  20,
  117.       391, 98,  12,  17,  20,  15,
  118.       275, 107, 14,  13,  42,  16,
  119.       466, 70,  13,  19,  15,  13,
  120.       444, 59,  16,  12,  15,  23,
  121.       523, 63,  17,  14,  15,  21,
  122.       362, 56,  13,  12,  15,  27
  123.   ]
  124.   #--------------------------------------------------------------------------
  125.   # * Name Actor Stats
  126.   #--------------------------------------------------------------------------
  127.   Stat_Names = ['hp', 'mp', 'atk', 'defense', 'spi', 'agi']
  128.   #--------------------------------------------------------------------------
  129.   # * Set Actor Stat Variance
  130.   #--------------------------------------------------------------------------
  131.   Stats = {}
  132.   # Actor 1 setup
  133.   Stats[1] = {
  134.     'hp' => 26,
  135.     'mp' => 4,
  136.     'atk' => 3,
  137.     'defense' => 3,
  138.     'spi' => 2,
  139.     'agi' => 2
  140.   }
  141.   # Default actor setup
  142.   Stats.default = {
  143.     'hp' => 24,
  144.     'mp' => 3,
  145.     'atk' => 2,
  146.     'defense' => 2,
  147.     'spi' => 2,
  148.     'agi' => 2
  149.   }
  150. end
  151.  
  152. #==============================================================================
  153. # ** Game_Actor
  154. #------------------------------------------------------------------------------
  155. #   This class handles actors. Its used within the Game_Actors class
  156. # ($game_actors) and referenced by the Game_Party class ($game_party).
  157. #==============================================================================
  158.  
  159. class Game_Actor < Game_Battler
  160.   #--------------------------------------------------------------------------
  161.   # * Convert Stat Names to their System Numbers
  162.   #--------------------------------------------------------------------------
  163.   Stat_Numbers = {
  164.     'hp' => 0,
  165.     'mp' => 1,
  166.     'atk' => 2,
  167.     'defense' => 3,
  168.     'spi' => 4,
  169.     'agi' => 5
  170.   }
  171.   #--------------------------------------------------------------------------
  172.   # * Alias Methods
  173.   #--------------------------------------------------------------------------
  174.   # If the setup method has not been aliased
  175.   unless method_defined?(:randomstatgains_setup)
  176.     # Alias the enemy selection method
  177.     alias randomstatgains_setup setup
  178.   end
  179.   #--------------------------------------------------------------------------
  180.   # * Setup
  181.   #     actor_id : actor ID
  182.   #--------------------------------------------------------------------------
  183.   def setup(actor_id)
  184.     # Create an empty stat hash
  185.     @stat_gains = {}
  186.     # Iterate through all stats
  187.     for stat in Stats::Stat_Names
  188.       # Add the stat gain to the hash
  189.       @stat_gains[stat] = []
  190.     end
  191.     # Setup stat gains
  192.     randomstatgains_setup(actor_id)
  193.   end
  194.   #--------------------------------------------------------------------------
  195.   # * Get Basic Maximum HP
  196.   #--------------------------------------------------------------------------
  197.   def base_maxhp
  198.     # Calculate the array index of the stat
  199.     stat = (4 * @actor_id) + ((2 * (@actor_id - 2)) - 2)
  200.     # Get the actor's stat
  201.     n =   Stats::Actor_Stats[stat.to_i].to_i
  202.     # Add the stat gains to the value
  203.     n += @stat_gains['hp'].sum
  204.     return n
  205.   end
  206.   #--------------------------------------------------------------------------
  207.   # * Get basic Maximum MP
  208.   #--------------------------------------------------------------------------
  209.   def base_maxmp
  210.     # Calculate the array index of the stat
  211.     stat = (4 * @actor_id) + ((2 * (@actor_id - 2)) - 1)
  212.     # Get the actor's stat
  213.     n = Stats::Actor_Stats[stat.to_i].to_i
  214.     # Add the stat gains to the value
  215.     n += @stat_gains['mp'].sum
  216.     return n
  217.   end
  218.   #--------------------------------------------------------------------------
  219.   # * Get Basic Attack
  220.   #--------------------------------------------------------------------------
  221.   def base_atk
  222.     # Calculate the array index of the stat
  223.     stat = (4 * @actor_id) + (2 * (@actor_id - 2))
  224.     # Get the actor's stat
  225.     n = Stats::Actor_Stats[stat.to_i].to_i
  226.     # Add the stat gains to the value
  227.     n += @stat_gains['atk'].sum
  228.     # Add item attack values to the stat
  229.     for item in equips.compact do n += item.atk end
  230.     return n
  231.   end
  232.   #--------------------------------------------------------------------------
  233.   # * Get Basic Defense
  234.   #--------------------------------------------------------------------------
  235.   def base_def
  236.     # Calculate the array index of the stat
  237.     stat = (4 * @actor_id) + ((2 * (@actor_id - 2)) + 1)
  238.     # Get the actor's stat
  239.     n = Stats::Actor_Stats[stat.to_i].to_i
  240.     # Add the stat gains to the value
  241.     n += @stat_gains['defense'].sum
  242.     # Add item defense values to the stat
  243.     for item in equips.compact do n += item.def end
  244.     return n
  245.   end
  246.   #--------------------------------------------------------------------------
  247.   # * Get Basic Spirit
  248.   #--------------------------------------------------------------------------
  249.   def base_spi
  250.     # Calculate the array index of the stat
  251.     stat = (4 * @actor_id) + ((2 * (@actor_id - 2)) + 2)
  252.     # Get the actor's stat
  253.     n = Stats::Actor_Stats[stat.to_i].to_i
  254.     # Add the stat gains to the value
  255.     n += @stat_gains['spi'].sum
  256.     # Add item spirit values to the stat
  257.     for item in equips.compact do n += item.spi end
  258.     return n
  259.   end
  260.   #--------------------------------------------------------------------------
  261.   # * Get Basic Agility
  262.   #--------------------------------------------------------------------------
  263.   def base_agi
  264.     # Calculate the array index of the stat
  265.     stat = (4 * @actor_id) + ((2 * (@actor_id - 2)) + 3)
  266.     # Get the actor's stat
  267.     n = Stats::Actor_Stats[stat.to_i].to_i
  268.     # Add the stat gains to the value
  269.     n += @stat_gains['agi'].sum
  270.     # Add item agility values to the stat
  271.     for item in equips.compact do n += item.agi end
  272.     return n
  273.   end
  274.   #--------------------------------------------------------------------------
  275.   # * Level Up
  276.   #--------------------------------------------------------------------------
  277.   def level_up
  278.     # Increase the level by one
  279.     @level += 1
  280.     # Iterate through each stat
  281.     Stats::Stat_Names.each do |stat_name|
  282.       # Calculate the new stat gain
  283.       n = random_gain(@actor_id, stat_name)
  284.       # Add the new gain to the actor's stat gains
  285.       @stat_gains[stat_name] << n
  286.     end
  287.     # Iterate through the class skill list
  288.     for learning in self.class.learnings
  289.       # Teach the actor new skills they have learned
  290.       learn_skill(learning.skill_id) if learning.level == @level
  291.     end
  292.   end
  293.   #--------------------------------------------------------------------------
  294.   # * Level Down
  295.   #--------------------------------------------------------------------------
  296.   def level_down
  297.     # Subtract one from the actor level
  298.     @level-= 1
  299.     # Iterate through each stat
  300.     Stats::Stat_Names.each do |stat_name|
  301.       # Remove the latest gains from the stat
  302.       @stat_gains[stat_name].pop
  303.     end
  304.   end
  305.   #--------------------------------------------------------------------------
  306.   # * Determine Random Stat Gain
  307.   #--------------------------------------------------------------------------
  308.   def random_gain(actor_id, stat_name)
  309.     # Begin error checking
  310.     begin
  311.       # Get the stat number
  312.       s_number = Stat_Numbers[stat_name].to_i
  313.       # Get the default stat change
  314.       default_change = (actor.parameters[s_number, @level] -
  315.         actor.parameters[s_number, @level - 1])
  316.       # Calculate the stat changes
  317.       change = rand(Stats::Stats[actor_id][stat_name])
  318.       # Calculate a random number
  319.       random = rand(2)
  320.       # If the random number is even
  321.       if (1 != (random % 2))
  322.         # Set the random change to a negative value
  323.         change *= -1
  324.       end
  325.       # Add the default change to the random change
  326.       change += default_change
  327.       # If the change is negative
  328.       if change <= 0
  329.         # Set the change to 0
  330.         return 0
  331.       # If the change is positive
  332.       else
  333.         # Return the newly calculated stat change
  334.         return change
  335.       end
  336.     # If an error occurs
  337.     rescue
  338.       # Print an error message
  339.       p 'Undefined stat_name: ' + stat_name + ' for actor: ' + actor_id.to_s
  340.       # Return that no stat change has occurred
  341.       return 0
  342.     end
  343.   end
  344. end
  345.  
  346. #==============================================================================
  347. # ** Array
  348. #------------------------------------------------------------------------------
  349. #   This hidden class handles arrays. It is used within many other
  350. # classes to store data for future retrieval.
  351. #==============================================================================
  352.  
  353. class Array
  354.   #---------------------------------------------------------------------------
  355.   # * Name        : Sum
  356.   #     Info      : Sums all values in the array
  357.   #     Author    : Trickster
  358.   #     Call Info : No Arguments
  359.   #---------------------------------------------------------------------------
  360.   def sum
  361.     # Initialize local variable n
  362.     n = 0
  363.     # Sum Up Values in Array
  364.     each {|num| n += num}
  365.     # Return number
  366.     return n
  367.   end
  368. end
Expand to see the code.


Instructions
(These are taken from the VX Version. Stat and Character names vary in the XP version.)

Place this script above Main, and below the default scripts. (I realize this is obvious to most, but some people don't get it.)

To modify the basic starting stats of a character, you must edit the Actor_Stats array in the Stats module, using the following rules:
  • Line 1 is the stats of the first actor in the database. (Ralph is the default) Line 2 is the second actor (Ulrika), etc.
  • The statistics are in order on each line, where the first entry is maximum HP, entry 2 is maximum MP, entry 3 is Attack, entry 4 is Defense, entry 5 is Spirit, and entry 6 is Agility.

To modify the variation of an actor's statistics upon changing levels, you must edit the Stats hash in the Stats module, using the following rules:
  • The value in each entry is the maximum that the new stat may vary from the default increase or decrease defined for that stat in the Database. (Calculated by subtracting the default value of the new level from the default value of the original level) In other words, if the default change is three, and you have the variation set to two, then the stat can go up anywhere between one and five points.
  • When leveling up, a stat cannot go down, although it may also remain unchanged. This is because if the random number chosen by the script is negative when leveling up, the stat is left unchaged instead of being changed in the opposite direction of that intended.

If you modify the default actors within the game, you must also modify this script, so that the actors will all have their correct stats. If you add actors, you must add new lines to this script, and vice-versa if you delete actors. These new lines should be in the Stats hash within the Stats module.

FAQ

Due to some misunderstandings on what I meant by "partially randomize", I will attempt to explain in a little more detail. What I meant is that this script will take the default amount that a stat will go up when you level up (as defined in the database), and it will then use another value (defined by the user within the script) to change the amount that stat will go up. To give an example, if the default increase for Ralph's HP when going to level 2 is 67, and you set the amount it can vary to 26, then his HP can go up by anywhere between 41 and 93 points. It can be anything between the default increase minus the variance to the default increase plus the variance.

Credits and Thanks

Thanks to psiclone for making the original request, because otherwise I wouldn't have a script to share.

Also, thanks to SephirothSpawn for helping me clean up this script and make it much more efficient. (Although a bit more complex...)

Author's Notes

Version 2.0 was released to make the script more efficient. It features some changes in the operation of the script, and thus is a little more complex. If anyone would like to see the original script for the request, they can look here. Be warned, as this version is actually more complex, and much less efficient.

Terms and Conditions

  • This script is free to use in any noncommercial project. If you wish to use this script in a commercial (paid) project, please contact Glitchfinder at his website.
  • This script may only be hosted at the following domains:
    http://www.glitchkey.com
    http://www.hbgames.org
  • If you wish to host this script elsewhere, please contact Glitchfinder.
  • If you wish to translate this script, please contact Glitchfinder. He will need the web address that you plan to host the script at, as well as the language this script is being translated to.
  • The script header must remain intact at all times.
  • Glitchfinder remains the sole owner of this code. He may modify or revoke this license at any time, for any reason.
  • Any code derived from code within this script is owned by Glitchfinder, and you must have his permission to publish, host, or distribute his code.
  • This license applies to all code derived from the code within this script.
  • If you use this script within your project, you must include visible credit to Glitchfinder, within reason.
What do you mean by "partially randomize"?
It's a little distracting. Do you mean if follows some path from the database's assigned values? Please elaborate, I might be interested. :D
I went to post something last night, but never finished it. I have a few comments/suggestions to greatly improve the way this script works.

I could go through all your code, and comment about alot of things you have done, but that wouldn't be as helpful. I honestly hope you take the advice below and greatly revise your code. If you wish to here my thoughts on your original code, I can share a few thoughts over PM. I am not trying to be rude, but I see potential and would just like to help.



To start off, lets re-think the script. You are going to be randomizing stat gains each level, with the option to set the random values for each actor and each stat. Now, this is going to be very repetitive unless we use some quick code blocks to search through efficient organized data. First, since we will be customizing it for each actor for each stat, arrays really aren't the way to go. We should use a multi-dimension hash instead. I also created a constant to hold the each stat name we will be using in the script (this will later serve for loop patterns).

module RandomStatGains
  Stat_Names = ['hp', 'sp', 'str', 'dex', 'int', 'agi']
  # Stats[actor_id] = {stat_name => n, ...}
  Stats = {}
  # Actor 1 setup
  Stats[1] = {'hp' => 100, 'sp' => 30, 'str' => 5, 'dex' => 4, 'int' => 3, 'agi' => 3}
  # Default actor setup
  Stats.default ={'hp' => 50, 'sp' => 25, 'str' => 3, 'dex' => 3, 'int' => 3, 'agi' => 3}
end
Expand to see the code.


Now, we might as well create a method in this module to get the random stat gain for each actor in each stat. This will simply code later.
module RandomStatGains
  def self.random_gain(actor_id, stat_name)
    begin
      return rand(Stats[actor_id][stat_name])
    rescue
      p 'Undefined stat_name: ' + stat_name + ' for actor: ' + actor_id.to_s
    end
  end
end
Expand to see the code.

(The above begin, rescue code just is an error catcher in case someone defines something wrong)

Now, how about the stat gains per level. The best way for this is to create an instance variable in each Game_Actor class (in your previous version, you modified a constant, something you should never do mid-code. Plus, that would not have saved if your game was reloaded or reset). To add this instance, we will create it upon creation of all actors. I suggest a hash, with the keys being the stat names and values being an array of values gain each level. We can add and remove these values for each level up.

class Game_Actor
  alias_method :glitchfinder_randstatgains_setup, :setup
  def setup(actor_id)
    @stat_gains = {}
    for stat in RandomStatGains::Stat_Names
      @stat_gains[stat] = []
    end
    glitchfinder_randstatgains_setup(actor_id)
  end
end
Expand to see the code.


Now, before we get to the stat reading, lets just finish the gaining stats when we level_up and level_down. We will use our module method here for adding a random value to our stat hash, and removing the last bonus of each array for each stat when leveling down. So our stat gains hash will look like:
# After 2 level gains
@stat_gains = {
  'hp' => [39, 21],
  'sp' => [15, 13],
  'str' => [3, 1],
  'dex' => [3, 4],
  'int' => [2, 2],
  'agi' => [3, 1]
}
# After a level down
@stat_gains = {
  'hp' => [39],
  'sp' => [15],
  'str' => [3],
  'dex' => [3],
  'int' => [2],
  'agi' => [3]
}
Expand to see the code.



So here is our code:
class Game_Actor
  alias_method :glitchfinder_randstatgains_levelu, :level_up
  alias_method :glitchfinder_randstatgains_leveld, :level_down
  def level_up
    # Pass through each stats
    RandomStatGains::Stat_Names.each do |stat_name|
      n = RandomStatGains.random_gain(@actor_id, stat_name)
      @stat_gains[stat_name] << n
    end
    glitchfinder_randstatgains_levelu
  end
  def level_down
    RandomStatGains::Stat_Names.each do |stat_name|
      @stat_gains[stat_name].pop
    end
    glitchfinder_randstatgains_leveld
  end
end
Expand to see the code.


So now all we have to do for each stat is add our stat gain sum for each stat gain. Now, arrays don't have a sum method, so we have to create one. There is one in the MACL 2.3, so we can just use that.
class Array
  #---------------------------------------------------------------------------
  # * Name      : Sum
  #   Info      : Sums all values in the array
  #   Author    : Trickster
  #   Call Info : No Arguments
  #---------------------------------------------------------------------------
  def sum
    # Initialize local variable n
    n = 0
    # Sum Up Values in Array
    each {|num| n += num}
    # Return number
    return n
  end
end
Expand to see the code.



Ok, now, we just need to add these to each stat total. An example of the first one can be done below.
class Game_Actor
  alias_method :glitchfinder_randstatgains_hasehp, :base_hp
  def base_hp
    # gets original value
    n = glitchfinder_randstatgains_hasehp
    # adds total stat gains
    n += @stat_gains[stat_name].sum
    # return n
    return n
  end
end
Expand to see the code.



Now put it all together, do that for each stat (or more if you wish). If you wish to do this and need any help, just ask.

I look forward to seeing a completed product. :)
Well, as you just proved, I am still learning to code. First off, I was never really sure what a hash is. Would you be able to explain that in easier terms than the help files do? Next, I greatly appreciate the help, and, as soon as I can figure out what all of your suggestions do, I'll try to incorporate them. Thanks! (I just realized that tutorials on hashes/arrays/other complicated and semi-complicated ruby would be helpful to new scripters...)

Edit: I think I'm figuring out the hashes. It looks similar to arrays, in that it stores data, just with a different format. (And it appears to store arrays as well...) I'm also not sure what your purpose was in keeping some of my old methods, like the levelup or leveldown ones. From what I can tell, you pretty much eliminated any need for them.

Edit2: I just tried to put it all together, and realized you were writing for RMXP. Also, when I modified the other parts to work with rmvx, the MACL's definition of a sum for arrays turned up with a nil:NilClass error.
A hash is just like an array, but instead of the indexing being based of integers that go from 0 to x, the indexing is based off any Ruby object.

Array = [object, object, object]
Hash = {object => object, object => object, object => object}

Array = [object1, object2, object3]
Hash = {0 => object1, 1 => object2, 2 => object3}

Hashes are extremely useful when creating data for existing objects like the $data and $game classes. Hashes also have a .default method, which instead of returning nil when you try to read from a non-defined index, it will return the default. For instance:
hash = {1 => 2, 3 => 4}
hash[1] -> 2
hash[5] -> nil
hash.default = 0
hash[5] -> 0
Expand to see the code.


Not sure what you mean by "I'm also not sure what your purpose was in keeping some of my old methods, like the levelup or leveldown ones. From what I can tell, you pretty much eliminated any need for them." We still need these methods to know when we level up and level down.

I didn't explain one thing, the alias_method. When you call alias_method, you are renaming an existing method.

alias_method :new_method_name, :old_method_name

This allows us to add processing before an existing method call and/or after an existing method call.

Lets say you write a method that returns a name that is passed:
def return_string(string = '')
  return "Name: #{string}"
end
Expand to see the code.


Well, what if we want to add () after it for whatever reason. We alias the method and proceed from there.
alias_method :old_return_string, :return_string
def return_string(string = '')
  n = old_return_string(string)
  return "#{n} ()"
end
Expand to see the code.


Our return_string method will now be called as it always has, but the original return_string method will be called from our new method now.

When I did it with that level_up and level_down method, we want to keep everything that happens in the original level_up/down methods, just add to it. By using the alias_method, more scripts can work together without conflict.

Everything should work just fine in VX. Your code should look something like this:
module RandomStatGains
  Stat_Names = ['hp', 'sp', 'str', 'dex', 'int', 'agi']
  # Stats[actor_id] = {stat_name => n, ...}
  Stats = {}
  # Actor 1 setup
  Stats[1] = {'hp' => 100, 'sp' => 30, 'str' => 5, 'dex' => 4, 'int' => 3, 'agi' => 3}
  # Default actor setup
  Stats.default ={'hp' => 50, 'sp' => 25, 'str' => 3, 'dex' => 3, 'int' => 3, 'agi' => 3}
  def self.random_gain(actor_id, stat_name)
    begin
      return rand(Stats[actor_id][stat_name])
    rescue
      p 'Undefined stat_name: ' + stat_name + ' for actor: ' + actor_id.to_s
    end
  end
end

class Game_Actor
  alias_method :glitchfinder_randstatgains_setup, :setup
  alias_method :glitchfinder_randstatgains_levelu, :level_up
  alias_method :glitchfinder_randstatgains_leveld, :level_down
  alias_method :glitchfinder_randstatgains_hasemaxhp, :base_maxhp
  def setup(actor_id)
    @stat_gains = {}
    for stat in RandomStatGains::Stat_Names
      @stat_gains[stat] = []
    end
    glitchfinder_randstatgains_setup(actor_id)
  end
  def level_up
    # Pass through each stats
    RandomStatGains::Stat_Names.each do |stat_name|
      n = RandomStatGains.random_gain(@actor_id, stat_name)
      @stat_gains[stat_name] << n
    end
    glitchfinder_randstatgains_levelu
  end
  def level_down
    RandomStatGains::Stat_Names.each do |stat_name|
      @stat_gains[stat_name].pop
    end
    glitchfinder_randstatgains_leveld
  end
  def base_maxhp
    # gets original value
    n = glitchfinder_randstatgains_hasemaxhp
    # adds total stat gains
    n += @stat_gains[stat_name].sum
    # return n
    return n
  end
end

class Array
  #---------------------------------------------------------------------------
  # * Name      : Sum
  #   Info      : Sums all values in the array
  #   Author    : Trickster
  #   Call Info : No Arguments
  #---------------------------------------------------------------------------
  def sum
    # Initialize local variable n
    n = 0
    # Sum Up Values in Array
    each {|num| n += num}
    # Return number
    return n
  end
end
Expand to see the code.


I have included everything you need right there. You just need to finish your remaining stats you wish to include. Just make sure you remember to alter your Stat_Names = ['hp', 'sp', 'str', 'dex', 'int', 'agi'] constant. Again, let me know if you need any help.
Well, I'm trying the script you gave me, and as soon as I entered the stats I wanted, it started to give me the nil:NilClass error for the sum method you added to the array class. I absolutely hate that error, as it never seems to mean what it actually says. Anyway, heres the modified version of the script you gave me. (Also, I already knew what aliasing was. I just wasn't sure exactly how the hashes functioned. I meant that it seemed like you were eliminating the need for most of the levelup and leveldown functions I had written, and that all you really needed were the changes to the level variable. I looked closer after you posted, and realized I had made some mistakes in my assumptions)

Modified version of your code. Show
module RandomStatGains
  Stat_Names = ['hp', 'sp', 'atk', 'defense', 'spi', 'agi']
  # Stats[actor_id] = {stat_name => n, ...}
  Stats = {}
  # Actor 1 setup
  Stats[1] = {
  'hp' => 100, 'sp' => 30, 'atk' => 5, 'defense' => 4, 'spi' => 3, 'agi' => 3}
  # Default actor setup
  Stats.default ={
  'hp' => 50, 'sp' => 25, 'atk' => 3, 'defense' => 3, 'spi' => 3, 'agi' => 3}
  def self.random_gain(actor_id, stat_name)
    begin
      return rand(Stats[actor_id][stat_name])
    rescue
      p 'Undefined stat_name: ' + stat_name + ' for actor: ' + actor_id.to_s
    end
  end
end

class Game_Actor
  alias_method :glitchfinder_randstatgains_setup, :setup
  alias_method :glitchfinder_randstatgains_levelu, :level_up
  alias_method :glitchfinder_randstatgains_leveld, :level_down
  alias_method :glitchfinder_randstatgains_base_maxhp, :base_maxhp
  alias_method :glitchfinder_randstatgains_base_maxmp, :base_maxmp
  alias_method :glitchfinder_randstatgains_base_atk, :base_atk
  alias_method :glitchfinder_randstatgains_base_def, :base_def
  alias_method :glitchfinder_randstatgains_base_spi, :base_spi
  alias_method :glitchfinder_randstatgains_base_agi, :base_agi
  def setup(actor_id)
    @stat_gains = {}
    for stat in RandomStatGains::Stat_Names
      @stat_gains[stat] = []
    end
    glitchfinder_randstatgains_setup(actor_id)
  end
  def level_up
    # Pass through each stats
    RandomStatGains::Stat_Names.each do |stat_name|
      n = RandomStatGains.random_gain(@actor_id, stat_name)
      @stat_gains[stat_name] << n
    end
    glitchfinder_randstatgains_levelu
  end
  def level_down
    RandomStatGains::Stat_Names.each do |stat_name|
      @stat_gains[stat_name].pop
    end
    glitchfinder_randstatgains_leveld
  end
  def base_maxhp
    # gets original value
    n = glitchfinder_randstatgains_base_maxhp
    # adds total stat gains
    n += @stat_gains[hp].sum
    # return n
    return n
  end
  def base_maxmp
    # gets original value
    n = glitchfinder_randstatgains_base_maxmp
    # adds total stat gains
    n += @stat_gains[mp].sum
    # return n
    return n
  end
  def base_atk
    # gets original value
    n = glitchfinder_randstatgains_base_atk
    # adds total stat gains
    n += @stat_gains[atk].sum
    # return n
    return n
  end
  def base_def
    # gets original value
    n = glitchfinder_randstatgains_base_def
    # adds total stat gains
    n += @stat_gains[defense].sum
    # return n
    return n
  end
  def base_spi
    # gets original value
    n = glitchfinder_randstatgains_base_spi
    # adds total stat gains
    n += @stat_gains[spi].sum
    # return n
    return n
  end
  def base_agi
    # gets original value
    n = glitchfinder_randstatgains_base_agi
    # adds total stat gains
    n += @stat_gains[agi].sum
    # return n
    return n
  end
end

class Array
  #---------------------------------------------------------------------------
  # * Name      : Sum
  #   Info      : Sums all values in the array
  #   Author    : Trickster
  #   Call Info : No Arguments
  #---------------------------------------------------------------------------
  def sum
    # Initialize local variable n
    n = 0
    # Sum Up Values in Array
    each {|num| n += num}
    # Return number
    return n
  end
end
Expand to see the code.
Got a little slight problem with your modified version. I get an error that says "undefined method: 'sum' for nil class"

Any ideas?
psiclone wrote:
Got a little slight problem with your modified version. I get an error that says "undefined method: 'sum' for nil class"

Any ideas?


That's what I was trying to figure out. That error is a catchall for other errors. It has even told me that things like +, -, * and / are undefinded. Once, it even told me that nil was undefined.
    n += @stat_gains[hp].sum
should be

    n += @stat_gains['hp'].sum
Cool, thanks! I'll update the first post a little later with the newer version! (as well as a better description)

Edit: I'm trying to make the script take the default stat gain for the current level change (as specified by the game builder in the database), and then use that to modify the stat changes as you've defined them. I was hoping that it could use the following formula:

default_gain + Stats::Radom_Gain - Stats::Random_Gain, where there is no change if the random gain is <= 0, and the change is recorded as 0.

Unfortunately, I'm having quite a bit of trouble figuring out how to make it work in an even partially efficient manner.
Not sure what you are asking...

Algebraically speaking... a + b - b = a

And the random gains are from 0 to your random game number you set. So it should never be less than 0, but the 0s will be required when leveling down.
Heh, I've found the error about that 'sum'.. at:
  def base_maxmp
    # gets original value
    n = glitchfinder_randstatgains_base_maxmp
    # adds total stat gains
    n += @stat_gains['mp'].sum
    # return n
    return n
  end
Expand to see the code.


And at the beginning of the code, you've got:

  Stat_Names = ['hp', 'sp', 'atk', 'defense', 'spi', 'agi']
Expand to see the code.


You don't have 'mp' defined or whatever it is. I changed the 'mp' to 'sp' in the first block, and it works just fine.. to a degree, unless I don't get the random part. It's not really doing the random within the number I specify, it seems to be adding it's own numbers different times. One point level 2 added 1 'hp', then level 3 added 23, then the next time I ran a test level 2 added 14 hp, level 3 added 1.. not sure what to do next, I can't write code to save my life, but I feel I can read it just fine.

I hope that finding that error in the 'mp' section helped you since I can't figure out the rest..

EDIT: Well, I figured out the 'random' part, I just put the random numbers in the wrong section, and it works just fine.

EDIT2: I don't get what 'default actor setup' is.. it doesn't seem to do anything, no matter what number(s) I put in there.

EDIT3: And.. random gains also seems to go backwards, too.. even on a level up. I started with 9 atk, but upon leveling, I had 7..
SephirothSpawn wrote:
Not sure what you are asking...

Algebraically speaking... a + b - b = a

And the random gains are from 0 to your random game number you set. So it should never be less than 0, but the 0s will be required when leveling down.


Since b is a random number, it is more likely to be a + b - c = d. What I was trying to do is calculate how much the predefined change was for that levelup, and then add that into the formula I just gave you as a. (I would also have an if statement thatr would catch it so that if a stat change would be negative, it is 0 instead)

psiclone wrote:
You don't have 'mp' defined or whatever it is. I changed the 'mp' to 'sp' in the first block, and it works just fine.. to a degree, unless I don't get the random part. It's not really doing the random within the number I specify, it seems to be adding it's own numbers different times. One point level 2 added 1 'hp', then level 3 added 23, then the next time I ran a test level 2 added 14 hp, level 3 added 1.. not sure what to do next, I can't write code to save my life, but I feel I can read it just fine.

I hope that finding that error in the 'mp' section helped you since I can't figure out the rest..

EDIT: Well, I figured out the 'random' part, I just put the random numbers in the wrong section, and it works just fine.

EDIT2: I don't get what 'default actor setup' is.. it doesn't seem to do anything, no matter what number(s) I put in there.

EDIT3: And.. random gains also seems to go backwards, too.. even on a level up. I started with 9 atk, but upon leveling, I had 7..


Well, the only code in this thread that functions at any level I like is the one in the first post. If I get these suggestions working, then I will add them into that code. And abbout the random portion, that is what I'm hoping to fix in the update I'm working on.

Edit: I've just updated the script with SephirothSpawn's suggestions, as well as an edit of my own. It should function properly again. It's all in the first post. Alos, SephirothSpawn, I would greatly appreciate your suggestions for the original script, as well. Finally, I'm planning to add a new version for RMXP, that uses the RMXP formulae.
All right, so I just updated this script to a new version. Enjoy!

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=51448