Help - Search - Members - Calendar
Full Version: [Scripting][Series]The Scripter’s Journey Series
RPG RPG Revolution Forums > Scripting > Script Tutorials > RGSS
The Law G14
The Scripter’s Journey Series

7. The World of Modules!


Table of Contents
I. What are Modules?
II. Namespaces!!
III. Mixins!!
IV. Conclusion!


What are Modules?
Alright, what are modules? Modules are like an organized interface or common area where you can store methods and constants to use later in your program. What do I mean by this? Think of modules like toolboxes, where they have extra “tools” (constants and methods) you can use to build your house (program). They serve two key roles: creating a unique namespace and allowing mixins with classes.


Namespaces!!
So…what’s a namespace? First of all, let’s first understand that Modules house two things: constants and methods (remember we learned what constants were last tutorial). Now, you’re probably wondering why there are only constants and not other types of “variables” like in classes…this is because modules do not create a new “type” like classes or can be inherited. You cannot make “instances” of a certain module like you can for classes. I want this to be ingrained in your head, modules are like “toolboxes” and can be used to easily add on to your program while classes are organized data types that can allow instances of this new type. So, with that out of the way, let’s go over namespaces.

Basically, namespaces allow you to create names of methods and constants without worrying that you may have naming conflicts. For example, let’s say you have two constants that both have the same name, but deal with two different things…like “ALLIANCE”, one constant refers to the Enemy Alliance (Evil), and the other refers to the Actor Alliance (Good). If you just go ahead and create two constants with the same name, this will cause problems. So, you can create two modules, one being a “toolbox” that contains basic actor information, and the other being a “toolbox” for basic enemy information:

CODE
module Actor
ALLIANCE = “Good”
def Actor.equip
#blah
end
module Enemy
ALLIANCE = “Evil”
def Enemy.gear
# Stuff
end
end


See? Now, notice something else I did in this module, I demonstrated how you would make your own methods. It’s the same way as making class methods in a class. Moreover, like I said, modules are like “toolboxes” and add on to the program…thus, modules and their contents can be used anywhere. Here’s how you would access a module’s contents:

CODE
# inside some random script
print Enemy::ALLIANCE
print Enemy.gear


And there you have it on namespaces! Next, mixins!


Mixins!!
Mixins means the idea of embedding a module into a class, or basically adding all the methods and constants of a module…and pouring it into a class for it to use freely. To do this, you must use the “include” keyword. Check out the following example:

CODE
module Actor
ALLIANCE = “Good”
end
class Game_Actor
include Actor
def initialize
print ALLIANCE
end
end


Notice that by using the “include” keyword, we no longer need to do something like this “Actor::ALLIANCE” but simply “ALLIANCE” because it’s essentially part of the class now. Get it? You can “include” as many modules as you like, thus “mixing it in!” Also, if you’d like to see some example in RMXP, for example, go to the help file and search up “String”, you’ll see under “Included Modules”, Comparable and Enumerable! You can check out those modules for yourself, but that’s just to show you some real examples!


Conclusion
Lol well that really short! But Modules are really important and plus I thought it would be good to have a breath of fresh air with this smaller tutorial verus last tutorial which was a moutfull. Anyway, like always, if you have any questions, concerns, or just feel like commenting, post! Thanks again and be back next! (Not sure what topic to go over next…but yea!)

The order of how these tutorials will progress: http://www.rpgrevolution.com/forums/index....showtopic=54953

~Law
Legacy
This is a great series Law, loved reading it and I know it's going to help a lot of people starting out. happy.gif
The Law G14
Hey, thanks a bunch man! I really appreciate it smile.gif Any suggestions for future tutorials?
MagitekElite
Yes, it certainly will help. biggrin.gif

I have seen the updates, but I never had time to comment on them--mostly because I didn't make it to that lesson yet. I was busy this week so I couldn't sit down to try it (and am still looking for an editor that will work), but after I get home on Monday I'm gonna sit down for it. smile.gif

Shaddow
Thanks, Professor Law, this really helped me get started on learning how to script. While it was a long read and a lot of information, you made it fun to read and informative.

One thing I'd like to see is maybe some 'homework assignments' maybe some simple script types for people to practice what you've taught them. For a hands on learner like myself, nothing could be more useful.
The Law G14
Thanks man smile.gif I'll get right into those homework assignments. The reason I didn't do any for these tutorials are because they are more fundamental type tutorials. This was the last tutorial of the "Beginner Syntax" block. However, my next post will be a homework assignment that synthesizes all the information learned from this "Beginner Syntax" block. From here on out though, there should be a homework assignment with each topic since were now entering "Advanced Syntax". I'm a hands on learner as well so I know how you feel, I'll get down to these assignments, you can count on me biggrin.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.