Narwhal:Module

From GMod Wiki

Jump to: navigation, search
G Button.png Go to:
Narwhal
Warning 64.pngThis page needs to be edited as it contains information that is unclear or incorrect. Improvement can be discussed on the talk page. Find more pages that need work here.
Details: None given.
Structure
Name Module
Available On: NewerShared.png
Description:
Used by Narwhal to define modules.

A module structure is used by the Narwhal Game Base to design modules which developers can use to add features to their gamemodes. In order to use modules, you need to add this line to your shared.lua:

 
NARWHAL.Config.UseModules = true // Enable modules
 

Modules are constructed like this:

 
-- Required values:
MODULE.Name = "example_module" -- This is the module's reference name. This needs to be unique, as it is what will be used to access it as a utility or dependency.
MODULE.Title = "Example Module" -- This is the module's "nice-name".
MODULE.Author = "Grea$eMonkey" -- Author's name.
MODULE.Contact = "[email protected]" -- Author's contact.
MODULE.Purpose = "Whatever you want." -- Short description of the module's purpose.
 
-- Optional values:
MODULE.ConfigName = "UseExampleModule" -- If you add this, you can disable the module from being loaded on startup by changing NARWHAL.Config["UseExampleModule"] to false. This must be unique!
MODULE.Protect = true -- Change to false if you don't want module functions pcall'd. It is recommended to leave this true. See Part 4 to see how module methods work.
MODULE.AutoHook = true -- Change to false if you don't want your module methods with gamemode hook names to be autohooked. See Part 5 to learn about how module hooks work.
 
-- MODULE.Init is called after all modules have loaded.
function MODULE:Init()
	-- Modules are object oriented, so self is a component of all module methods.
	print( "The Narwhal Module "..self.Name.." has successfully loaded." )
end
 
-- Depending on what kind of module you're making, you'll have different functions.
-- Here's a generic example method. Modules should be comprised of functions such as this.
-- We will use this simple method to demonstrate how module errors are handled.
function MODULE:AddNumbers( num1, num2 )
	-- In this function we have num1 and num2, and we're going to find their sum. Simple, eh?
	-- Let's make sure we're dealing with valid numbers before adding. If one of the arguments isn't a number, we'll throw an error.
	if !tonumber(num1) or !tonumber(num2) then
		error( "MODULE.AddNumbers failed: Invalid arguments!\n" )
	end
	return num1 + num2
end
 

You don't need to define all values. MODULE.Name is the only really important one since that's what you'll be using to access your module's data.

Before you can use your module, you'll need to get it's data.

You can access the data as follows

 
local exmod = NARWHAL.GetModule( "example_module" ) -- Get the module data
print( exmod:AddNumbers( 1, 2 ) ) -- prints 3 in console
 

To include your modules in your gamemode, put all their .lua files into a folder named modules. So if my Lua file was module_math.lua, I'd place it in "mygamemode\gamemode\modules\module_math.lua".

Note: Module file names have no effect on their reference names. MODULE.Name is what you use with NARWHAL.GetModule.

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox