RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
module vs. def?

0 Members and 1 Guest are viewing this topic.

***
Rep:
Level 82
We learn by living...
what's the difference between a module like

Code: [Select]
module taco
 beef = 0
 shell = 0
 spices = 0
 veggies = 0
end

and def, like

Code: [Select]
def taco
 beef = 0
 shell = 0
 spices = 0
 veggies = 0
end

they look the same to me.

pokeball TDSOffline
***
Rep:
Level 84
-T D S-
Silver - GIAW 11 (Hard)Silver - Game In A Week VII
The difference in your example would be that first a module must start with an uppercase letter to work.

As for the working difference between a module and a defining a method (def). A module is often a collection methods and constants that does not need to be encapsulated into an object in order to call it's methods or read it's constants.

Defining a method (def) is most often used to perform a repetitive set of actions within a class or module such as calculations or getting a value based on the arguments of the method.

***
Rep:
Level 82
We learn by living...
so if I wanted to access some command from any class, would I use a module? I'm not sure I understand.

pokeball TDSOffline
***
Rep:
Level 84
-T D S-
Silver - GIAW 11 (Hard)Silver - Game In A Week VII
That depends on what you are trying to do.

Most modules in rpg maker are used for global actions such as playing sounds or in VX the Cache module which loads bitmaps and keeps them in a hash for faster accessing of the bitmap.

This is an example of how I would use a module.

Code: [Select]
module Colors
  def self.white(alpha = 255) ; return Color.new(255, 255, 255, alpha) ; end
end

The Colors module would allow me to have preset colors with names such as "white", "black", "red" etc. I could use this whenever I needed to set a color by using Colors.white and it has the added option to change the alpha value with an argument.

This is just a simple example of how a module would be used, there are of course a lot of ways to use a module but they really depend on what you are trying to achieve.

*
Rep:
Level 82
I keep writing different things that results in a wall of text but I'll settle on this:

Having written up various versions trying to explain this, I'm going to say this: depending on how they are used, they can have a huge difference and they can have no difference whatsoever. However, what you want to know actually has no relevance on whether you should use one over the other or whether there is a difference at all.

Module is the ruby syntax for namespace used in C++ or package used in Java. It's like separating a section of a folder from another with a divider. The contents of one section can be the same as another section, but because they are divided they can be given different meanings without excluding the other's.

It doesn't matter whether the variables are held within a module or not, they will always be accessible at any time (assuming they are not held within class or local space). However, by dividing them into modules the same variable names can be used between modules and the meaning can be different.

That's the important thing to remember when deciding on whether to use a module to contain symbols (i.e. variables, classes etc).

They are also used to hold onto logically similar data; another key thing to note.

So like in TDS' example, a module called Colors that returns a variety of different color objects is a wise move. It also means however, that I can write my own Color module and use the same names for different purposes: perhaps I want a different default alpha or not allow the choosing of alpha value.

If, however, I was to write code that calculated the volume of a variety of 3 dimensional objects. It wouldn't make sense to include them in the Colors module, nor would I want it in the global module-space*. So I would make a new module called "Volume" or something similar (they should try to be as unique and specific sounding as possible so something like "LFShapeVolume" is probably better).

In essence:

Modules provide a namespace for symbols to exist because they are logically similar in purpose. Using different modules allows symbols to have the same name and be given different purposes. It also prevents potential future name clashes by making your symbols more unique. This is the main reason you should use them.

However, modules are also used for one other important reason:

Modules implement the mixin facility. This is a bit more of a complex use of modules but I'll happily talk about that if you want to know more. I'll likely write up some post to show how they can be better used in RGSS(2) if something like that doesn't already exist.

*The global module space is where all the default RGSS classes exist. Classes like Window, Scene_Map, Scene_File are all held within the global space. They can be used at any time in any part of the program, even within other classes and at any scope. There's a kind of hierarchy involved:

Global scope
Modular scope
Class scope
Local scope

Anything in the higher scope can be used in lower scopes but not the other way around. You cannot create a symbol at local scope, for example, and use it in the global scope. You can, however, refer to a symbol held in a particular scope by first referring to the scope it is held in. I could go on for a long time to explain all this in detail, but I won't for now. However, if it would be helpful to do that, I can do that no problem. I'm pretty sure Pacman started to do that but I think he got busy with school work to finish it.
« Last Edit: December 01, 2011, 04:38:43 AM by LoganForrests »
(Why do I always feel like it's the end of the world and I'm the last man standing?)