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.

Weapon type dependant damage calculation

I have only one playable character who can wield any weapon. The thing is, I want the damage calculation to depend on what the character is wielding.

If it's a melee weapon he is using, damage is calculated on strength, weapon attack, enemies' strength and defense.
If it's a ranged weapon he is using, damage is calculated on dexterity, weapon attack, enemies' dexterity and defense.
If it's a magical weapon he is using, damage is calculated on intelligence, weapon attack, enemies' intelligence and magical defense. (example: wands)

How do i do this? How do I make the game find out what type of weapon the character is using? How do I categorize weapons?
 
You would look into Game_Battler 3, under def attack_effect

attacker is a local variable in that method which is another Game_Battler (the enemy - Game_Enemy or actor - Game_Actor).

If you wanted to assign what "type" your weapons are, something quick could be:

Code:
class RPG::Weapon
  Type = { [item_id, ...] => 'Wand', [item_id, ...] => 'Sword', ...}
  def type?
    Type.each do |ids, kind|
      if ids.include?(@id)
        return kind
      end
    end
    return 'Default Kind'
  end
end

Now any weapon ($data_weapons[weapon_id]) now has the property kind.

Code:
$data_weapons[1].kind

Will return a string of what kind.


So in that method, you can use

Code:
case $data_weapons[attacker.weapon_id].kind
when 'Wand'
  # formula for damage here
when 'Something else'
...
end

You can check in the tutorials for the tutorial I did on adding attributes to objects. If you need any help, let me know.
 
I haven't tried applying your answer since I'm busy pulling myself out of a hole I dig in the menu scene, but I do somewhat understand what it does.

My next question is, since this calculates damage type that the hero deals by identifying specified weapon types, what about damage types that enemies deal? My uncomprehending brain guesses that I can use custom element efficiency to check?

Can I also use custom weapon element for types instead of specifying it by ID? Sounds like it would be more convenient for me instead of calculating how many weapons I will have and dividing them into 3 categories (1-100,101-200,201-300).

Sorry if I'm talking rubbish, I don't really script. I look for situations similar to what I want, make a copy of the script and modify it which explains the mess i got into the menu scene.
 
Well, you can divide the method basically like this:

Code:
  def attack_effect(attacker)
    # Clear critical flag
    self.critical = false

    if attacker.is_a?(Game_Actor)
      # Forumal for calculating damage here if attacker is Actor
    else
      # Forumal for calculating damage here if attacker is Enemy
    end

    # If hit occurs
    if hit_result == true
      # State Removed by Shock
      remove_states_shock
      # Substract damage from HP
      self.hp -= self.damage
      # State change
      @state_changed = false
      states_plus(attacker.plus_state_set)
      states_minus(attacker.minus_state_set)
    # When missing
    else
      # Set damage to "Miss"
      self.damage = "Miss"
      # Clear critical flag
      self.critical = false
    end
    # End Method
    return true
  end

If you wanted, you could do what I said, but instead do:

Code:
class RPG::Weapon
  Type = {}
  for i in 1..100
    Type[i] = 'Wand'
  end
  for i in 101..200
    Type[i] = 'Sword'
  end
  def type?
    Type.each do |ids, kind|
      if ids.include?(@id)
        return kind
      end
    end
    return 'Default Kind'
  end
end

You basically just need to figure out the formuals. Let me know if you need any help.
 

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