KSortGroup XP

kyonides

Reforged is laughable
Veteran
Joined
Nov 17, 2019
Messages
285
Reaction score
70
First Language
English
Primarily Uses
RMXP
KSortGroup XP

by Kyonides Arkantes


Introduction

Just as I've been doing lately, I've posted another scriptlet to let you get stuff done the easy way.
At least that's what I think...

What it does it to let you fetch the heroes or the enemies based on how strong or weak they are.

o_O How did I define what's strong or weak there?

Well, I used some script calls that handle a specific stat per call.

Following a single script call model you get any stat like this:

Code:
$game_party.members_by(:low_hp)
$game_troop.members_by(:high_int)
The former list will return a list of heroes sorted by HP in increasing order while the latter will offer you all troopers sorted by INT in decreasing order. Returns an empty list if no living member was found.

Code:
$game_party.member_by(:low_hp)
$game_troop.member_by(:high_int)
These calls let you do the same for their first member only, if that member is ALIVE.
Otherwise it returns nil for all members are already dead.

o_O? You say you need to keep that data at hand. Well, go assign it to any variable! XP

Code:
my_variable = $game_party.member_by(:low_hp)
By the way, you've got 16 different options as I've explained in the Comments section of my script.


The Script

Code:
# * KSortGroup XP
#   Scripter : Kyonides Arkanthes

# This is a scriptlet that should let you fetch a sorted list of actors based on
# a specific stat like HP or PDEF, etc.

# Warning!
#   It won't work on the Title and File Loading scenes obviously. :P
#   Here mana stands for SP in RMXP but MP in other versions.
#   It will automatically pick living heroes or troopers during battles.
#   It will return all heroes sorted by stat while on the map.
#   It might return an empty array with no Hero or Trooper.
#   Or it might return no Hero or Trooper but nil aka nothing.

# * Script Call Section *

# To get all members of your party or enemies
#   $game_party.actors   ## Already included in RM
#   $game_troop.enemies  ## Already included in RM

# To get all DEAD members of your party or enemies
#   $game_party.dead_members
#   $game_troop.dead_members

# To sort Heroes or Troopers by some basic stat using the script calls mentioned
# below, use any of the following  Code  options:
#   :low_hp    :high_hp    :low_mana  :high_mana
#   :low_str   :high_str   :low_int   :high_int
#   :low_agi   :high_agi   :low_dex   :high_dex
#   :low_pdef  :high_pdef  :low_mdef  :high_mdef

# To get all LIVING members of your party or enemies

#   $game_party.members_by(Code)
#   $game_troop.members_by(Code)

# To get the FIRST member on that list
# Option 1
#   Add [0] to the end of your favorite script call. No spaces included!
# Option 2
#   Add .first to the end of your favorite script call. No spaces included!
# Option 3 - The calls do the job for you.
#   $game_party.member_by(Code)
#   $game_troop.member_by(Code)

module KSort
  def self.group(type, list)
    case type
    when :low_hp
      list.sort{|a, b| a.hp < b.hp }
    when :high_hp
      list.sort{|a, b| a.hp > b.hp }
    when :low_mana
      list.sort{|a, b| a.sp < b.sp }
    when :high_mana
      list.sort{|a, b| a.sp > b.sp }
    when :low_str
      list.sort{|a, b| a.str < b.str }
    when :high_str
      list.sort{|a, b| a.str > b.str }
    when :low_int
      list.sort{|a, b| a.int < b.int }
    when :high_int
      list.sort{|a, b| a.int > b.int }
    when :low_agi
      list.sort{|a, b| a.agi < b.agi }
    when :high_agi
      list.sort{|a, b| a.agi > b.agi }
    when :low_dex
      list.sort{|a, b| a.dex < b.dex }
    when :high_dex
      list.sort{|a, b| a.dex > b.dex }
    when :low_pdef
      list.sort{|a, b| a.pdef < b.pdef }
    when :high_pdef
      list.sort{|a, b| a.pdef > b.pdef }
    when :low_mdef
      list.sort{|a, b| a.mdef < b.mdef }
    when :high_mdef
      list.sort{|a, b| a.mdef > b.mdef }
    end
  end
end

class Game_Party
  def dead_members
    @actors.select{|a| a.hp == 0 and !a.hidden }
  end

  def living_members
    @actors.select{|a| a.hp > 0 and !a.hidden }
  end

  def members_by(type)
    list = $game_temp.in_battle ? living_members : @actors
    KSort.group(type, list)
  end

  def member_by(type)
    members_by(type)[0]
  end
end

class Game_Troop
  def dead_members
    @enemies.select{|a| a.hp == 0 and !a.hidden }
  end

  def living_members
    @enemies.select{|a| a.hp > 0 and !a.hidden }
  end

  def members_by(type)
    KSort.group(type, living_members)
  end

  def member_by(type)
    members_by(type)[0]
  end
end
Terms & Conditions

Free for use in any kind of game.
Include my nickname in your game credits.
Mention this forum in your game credits as well.
Give me a free copy of your completed game if you include at least 2 of my scripts! :p
 
Last edited:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

aww I am sad but happy for brackeys to decide to pursue something new but seing brackeys ending is so sad ;w;
Now RPG Maker has brought him here. He will never be rid of his need for it. He hates and loves the RPG Maker, just as he hates and loves himself. Featherbrain's life is a sad story. Yes, Featherbrain, he was called. Before the RPG Maker found him. Before it drove him mad.
Really struggling with XP animations but my game deserves my best.
I think the characters are really too short. Should at least add a little more
Just the legs should be longer.
Ami
--- Survival For RPGist ---

Survivalist: I going to Test you: How to make the Fire.
F.Mage: That easy. (Preparing the Spell) EXPLOSIVE!!!
(an Explosion on the Wood Branch then Fire is Appeared)
F.Mage: (Proud Face) See?
Survivalist: (Angry Tone) MAGIC ISN'T ALLOWED!!!

Forum statistics

Threads
102,925
Messages
996,008
Members
134,379
Latest member
Marcussss
Top