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] second question - modules

Status
Not open for further replies.

Aran

Member

though, i've been helped many times before on the topic of modules, i still don't really understand when i should use them as opposed to a class.

and are they always running if they're never initalized?
 
Module is the ancestor of Class.
Everything you can do in a module, you can do in a class.
So you could just not use modules and use classes.

It's up to you but what you should know about modules is that :
- they cannot be initialized
- instance methods can't be used with modules
- they're offen used as a countainer for a whole set of classes. (or modules)
 
modules are more oftenly used as "Container" to either hold a group of constants, classes, other modules, etc. as a form of organization.

Another really important use of modules I think over-looked for some reason is functions.

You could basically make the interpreter class a module instead (I am working on one) so each script call just accesses a method in a module.

Code:
module Interpreter
  def self.open_menu
    $scene = Scene_Menu.new
  end
  def self.open_scene(class)
    $scene = class.new
  end
end

You must have the self in front of method names, or the module name (def Interpreter.open_menu).


modules are are actually Constants.

Code:
module Interpreter
  # module stuff here
end

Some_constant = 15
Another_Constant = Some_Class.new
Interpreter = Interpreter.new

Classes have to be initialized as objects where modules are basically initialized as constants.
 
Well, don't quote me technically, since I am not a technical person, I know what I am saying and doing, just not from a technical standpoint.

Constants are variables, so just like variables, they aren't initialized, the objects are. Variables are actually just pointers to objects in the memory.

What I said was classes have to be created, like
Code:
window = Window_Item.new
window.refresh

Where modules are simply
Code:
Module.method

Just sit back and think for a moment. You never go
Code:
object = Module.new
since the object is already created as a constant.

Think of it as this:
Code:
class Your_Module
  def some_method
  end
end

THIS_IS_A_MODULE = Your_Module.new

That's kind what happens when you create a module to my understanding.

That's why you access constants and modules the same.
Code:
module Somemodule
  Constant = 1
  module Submodule
  end
end

Somemodule::Constant
Somemodule::Submodule
 
Modules are a great way to divide up namespace. Especially when working in a community such as ours. I always felt that when Near was developing the SDK that enforcing Module namespace should have been one of the regulations.. Something like:

Code:
module Prexus
  class Party_Switcher
    [..etc..]
  end
end

Code:
module Trickster
  class Party_Switcher
    [..etc..]
  end
end

You could then have both classes in one script, with completely different code within them, and call them seperately:

party1 = Prexus::Party_Switcher.new
party2 = Trickster::Party_Switcher.new
 
Status
Not open for further replies.

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