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.

Module Method Definition problem

I'm developing a mass aggregate physics engine with RMXP. One of the components is the module that follows:

Code:
#=============================================================================

#-----------------------------------------------------------------------------

# ** Spring_Physics

#-----------------------------------------------------------------------------

#=============================================================================

module Spring_Physics

  #---------------------------------------------------------------------------

  # This module defines physics constants for spring physic objects

  #---------------------------------------------------------------------------

  G = 1.0    # Gravity

  K = 0.025  # Spring Constant

  D = 0.95   # Dampening

  R = 25.0   # Rest Length

  #---------------------------------------------------------------------------

  # * Point_Direction

  # Returns the radian angle of the line formed between the two points.

  #---------------------------------------------------------------------------

  def point_direction(x1, y1, x2, y2)

    r = (x1 - x2) / (y1 - y2)

    r *= Math::PI/4

    return r

  end

end

One thing to note, I'm not entirely sure if my conversion of a slope to radian measure is even remotely accurate. But alas, that is not my problem.

As it is, when I try to call Spring_Physics.point_direction() I get an error saying " undefined method for `point_direction' for Spring_Physics:module ".

I'm desperately confused. Admittedly, I haven't defined any modules before, but from everything I can tell, this should work. Anyone have any suggestions?

And while I'm at it, anyone know the way to take a line defined by two points and get the radian angle formed by them?

Any help would be greatly appreciated!
 
Change the method definition to [rgss]def self.point_direction(x1, y1, x2, y2)
[/rgss]
I don't exactly know why you have to do that, so someone more knowledgeable would need to explain.
 
def blah in a module is just an instance method. These methods are really only ever used when you include a module into a class or other module.
Code:
module Test

  def blah

    p rand(3)

  end

end

 

class Test2

  include Test

end

 

a = Test.new

a.blah # -> 0, 1 or 2

Adding self. to a method name makes the method into a module function, that can be accessed from outside the module.
Code:
module Test

  def self.blah

    p rand(3)

  end

end

 

# Is the same as

module Test

  def blah

    p rand(3)

  end

  module_function :blah

end

Then there is access control for your methods:
- Public: Method can be accessed by anything anywhere
- Protected: Method can only be called upon by objects from the same class or sub-classes
- Private: Method can only be called upon by self.

Hope that cleared some things up.
 

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