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.

Basic question: call methods of other classes

I have a problem. I'm learning RGSS and for practice im implementing all rm2k lost options in XP. The problem is that i don't know exactly how to call methods out of the actual class.
I have a "Other" class that have many new methods, like correction critical for weapon, or another based in Actor/Class, etc.. well, methods that i have to call in many other scripts.

example:
#This is the call i'm making now , in Game_Battler 3

$Other.critic_weapon(@attacker,@critical)

And this the called method class:

class Other

def critic_weapon(attacker, critical)
if attacker.weapon_id == 1
print "critic mod"
critic += 5
return

this is the error message

no method error, undefined method critc_weapon for nil: Nil class
 
Did you do...

$Other = Other.new

...because simply calling an Object like $Object isn't going to do anything, a $ refers to a global variable which is very memory intensive and to be accessed from anywhere, I try not to create any except for very special cases (out of hundreds of scripts, I've only used a global in 2 or 3 of them for new game objects, that should give you an idea of how little you'll need to use a Global... infact, the next best thing to a global variable would to be just make a module or a constant.)

BTW what are you trying to do with this script? Seeing as you're trying to add some kind of critical bonus depending on what kind of weapon is equipped (on an actor, since enemies don't use weapons), I'd code it into Game_Battler.attack_effect instead of setting it in a different class (ie class Other).

Code:
class Game_Battler

  def attack_effect(attacker)

    if attacker.is_a?(Game_Actor)

      case attacker.weapon_id

      when 1 ; critical_bonus = 5

      when 2 ; critical_bonus = 6

      # ...etc...

      end

      # then, the rest of the coding...

    end

  end

end

Keep in mind thats not a *functional* example, but thats closer to the way you'd probably want to do it. You can set up a Module which aids in where the user will set his settings, even make methods so the module can do your dirty work but you'd have to code things like this into Game_Battler.<type>_effect
 

Zeriab

Sponsor

Thinking only procedural is a problem when dealing with the default scripts.
I would suggest you search for tutorial on object oriented design, uml and possible also try to find some Java/C# tutorials.
The reason is that I believe you have a better chance of finding good tutorials on object oriented programming than if you only look for Ruby tutorials.
 

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