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.

Calling methods using string that have its names

Hi. I have been creating a combat script for a long time. For now it uses a hash style configuration. My problem is that i want to improve the core script adding a new configuration mode that lets to an more advanced or interested user do anything.

So i need to changue a lot of things from my script that already have 25 battle effects.

The new configuration style will be method based and it will use codes, conditionals and cases to configure values for the script. So, if now it was:
# weap id effect code effect % rate
Improved_weapons = {3 => {'excalibur_syndrome'=>5}}

Now it could be:
Code:
 

class Game_Actor  

  

  def calc_excalibur_syndrome

    n=0

 

    # This iterates through a multiequip

    for item in equips.compact 

      if item.is_a? RPG::Weapon

        n += item.excalibur_syndrome

      end

    end

 

    return n

  end

end

 

My problem is that pasting that loop each time is a pain in the ass so i created a new methods:

# types: arm,weap,all
def sum_weap_eff(code)

n = 0
for item in equips.compact
if item.is_a? RPG::Weapon
case code
when 'excalibur syndrome' ; n += item.excalibur_syndrome
end
end
end

return n
end

My problem is... there is a way yo avoid the case and call dynamically the method with the apropiate code?

I know thats bad for now. If i was starting i could changued it all, but is what i have for now and im thinking in anything that might help to not have to add eternal cases.

so the nice idea will be:
sum_weap_eff('excalibur_syndrome')
that the string is used to call the methof of the item
 

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