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.

[RESOLVED] Still having problems with optimize

Okay, I'll keep this short.

I'm basically using a hash to assign weapon ID's with certain skills, what I need to know is how do I pull all the skill id's from the array within the hash? Here's the code snippet I barely started, should speak for itself. If actor is equipped with Weapon ID, then actor learns Skill ID.

Code:
#===============================================================================
# ~** Weapon Skills Script **~
#-------------------------------------------------------------------------------
# Written by  : Kain Nobel
# Version     : 0.1
# Last Update : 07/09/2008
# Created On  : 07/09/2008
#===============================================================================
module Weapons
  Skills = {
    1 => [1, 2, 3],
    2 => [4, 5, 6]
  }
end
#===============================================================================
# ** Game_Actor
#-------------------------------------------------------------------------------
#   This class has been enhaced to so the actor will 'learn' the skills assigned
# within the Weapon::Skills module.
#===============================================================================
class Game_Actor
  #-----------------------------------------------------------------------------
  # * Alias Listings
  #-----------------------------------------------------------------------------
  alias_method :kn_wpnskills_game_actor_setup,       :setup
  #-----------------------------------------------------------------------------
  # * Object Initialization (*Aliased*)
  #-----------------------------------------------------------------------------
  def setup(actor_id)
    kn_wpnskills_game_actor_setup(actor_id)
    for i in 0...$data_weapons.size
      if @weapon_id == Weapons::Skills[i]
        self.learn_skill(Weapons::Skills[i])
      end
    end
  end
end
 

OS

Sponsor

If your Hash is Weapon_ID => Skill_ID, and you want to get the values, you'll want to use:

Code:
newArray = myHash.values

This returns an Array of the Values (Skill_IDs) from your hash. To get the Weapon_IDs, you'd use 'keys' instead of 'values.'

Hope this is useful.

~Owen Sael

P.S. Looking at your code, I can't see why you use $data_weapons in your iteration. It isn't used anywhere else, so it isn't very important, is it? Why not check against the Weapon IDs in your Hash? Just a suggestion. Peace!
 
Thanks. One more quick question, if anybody's still out there. It's weapon related, but has to do with a different script dealing with optimizing an actor's equipment.

How do I pull the strongest weapon ID out of the pool of $game_party.weapons if that weapon is the strongest weapon out of the bunch? (try saying that 10 times fast...)
 

OS

Sponsor

Try using

Code:
$data_weapons[$game_party.weapons[i]].atk

to compare the ATK of weapons. I assume you only want to check attack, but if not, you can use these attributes of RPG::Weapon:

RMXP Help File":1l3wxyc7 said:
id
The weapon ID.

name
The weapon name.

icon_name
The weapon's icon graphic file name.

description
The weapon description.

animation1_id
The animation ID when using the weapon.

animation2_id
The animation ID when on the receiving end of the weapon.

price
The weapon's price.

atk
The weapon's attack power.

pdef
The weapon's physical defense rating.

mdef
The weapon's magic defense rating.

str_plus
The weapon's strength bonus.

dex_plus
The weapon's dexterity bonus.

agi_plus
The weapon's agility bonus.

int_plus
The weapon's intelligence bonus.

element_set
The weapon's element. An Elemental ID array.

plus_state_set
States to add. A State ID array.

minus_state_set
States to cancel. A State ID array.

If you felt the need, you could write a method in RPG::Weapon called is_stronger_than(weapon) that returns a bool.

I hope this is helpful.

~Owesome Scriptor
 
Nevermind... my brain is having major lag tonight... RESOLVED! I just changed the method to this...

Code:
  #-----------------------------------------------------------------------------
  # * Optimize : 
  #-----------------------------------------------------------------------------
  def optimize_attack
    # If game party has no weapons...
    if $game_party.weapons.nil?
      $game_system.se_play($data_system.buzzer_se)
    else
      for i in 0...$data_classes[@actor.class_id].weapon_set.size
        if $game_party.weapons.include?(i) #and @actor.equippable?(i)
          if $data_weapons[i].atk > $data_weapons[@actor.weapon_id].atk
            @actor.equip(0, i)
          end
        end
      end
    end
  end

...and now it works fine :thumb:

Okay, that got me the result I was looking for (thus far...) but now I'm having another problem.

When determining if actor can equip the strongest weapon in question, it doesn't work. When I comment out the condition "and @actor.equippable?(i), he'll equip the Mythril Gun (strongest default weapon in game) but when the condition exists, his equipment doesn't change to "Mythril Sword" like it should... what'd I do wrong now?

Here is an example code...

Code:
  #-----------------------------------------------------------------------------
  # * Optimize : 
  #-----------------------------------------------------------------------------
  def optimize_attack
    # If game party has no weapons...
    if $game_party.weapons.nil?
      $game_system.se_play($data_system.buzzer_se)
    else
      for i in 0...$data_weapons.size
        if $game_party.weapons.include?(i) #and @actor.equippable?(i)
          if $data_weapons[i].atk > $data_weapons[@actor.weapon_id].atk
            @actor.equip(0, i)
          end
        end
      end
    end
  end
 
Look at the method inside Game_Actor:
Code:
  def equippable?(item)
    # If weapon
    if item.is_a?(RPG::Weapon)
      # If included among equippable weapons in current class
      if $data_classes[@class_id].weapon_set.include?(item.id)
        return true
      end
    end
    # If armor
    if item.is_a?(RPG::Armor)
      # If included among equippable armor in current class
      if $data_classes[@class_id].armor_set.include?(item.id)
        return true
      end
    end
    return false
  end

What you did is send an integer as a parameter when it should be either RPG::Weapon or RPG::Armor.

Fixed code:
Code:
for i in 0...$data_weapons.size
        if $game_party.weapons.include?(i) and @actor.equippable?($data_weapons[i])
          if $data_weapons[i].atk > $data_weapons[@actor.weapon_id].atk
            @actor.equip(0, i)
          end
        end
      end

That should work.

Later
 

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