Jump to content
MakeWebGames

Modules Guide


Zeggy

Recommended Posts

Introduction

Modules are located in the sub-folder /modules. Almost everything you see on ezRPG is a module - login, register, logout, members list, account settings, etc.

You can see the most basic code for a module in /modules/skeleton.php:

defined('IN_EZRPG') or exit;

class Module_Skeleton extends Base_Module
{
   public function start()
   {
       requireLogin();
   }
}

This is probably the best place to start whenever you write a new module - just copy the code in skeleton.php and work from there.

Rules

There are some rules that you need to follow when writing modules for them to integrate properly with ezRPG:

 

  1. Modules go in their own /modules/ModuleName sub-folder. Always. Admin modules go in the /admin/ModuleName sub-folder.
     
  2. Module class names must begin with the Module_ prefix, followed by the module name.
     
  3. Modules are accessed by their class name, minus the Module_prefix. For example, Module_Blabla will be accessed throughindex.php?mod=Blabla.
     
  4. Modules must extend the Base_Module class in order to access database, template and player objects.
     
  5. Modules must have a public start() method. This is the method that will be called when your module is viewd by a player.

 

Class Variables

In every module, there are three class variables available to you, as well as global functions and constants:

$this->db - This is the database object. Check out the documentation for the database class to see what kind of stuff you can do!

Example of writing a database query:

//Count the number of players

$result = $this->db->fetchRow('SELECT
   COUNT(`id`) AS `count` FROM `players`');

echo $result->count; //Display the result

 

$this->tpl - This is the smarty template object. Check out the included modules to see how you can display templates or assign variables!

For example, to display a template you can use $this->tpl->display('example.tpl');.

$this->player - This is the player object. It contains all player data as class variables.

For example, to access the player's username, you can use $this->player->username;.

Other Files

If you add template files, they belong in /smarty/templates. If you are adding many template files for your mod, you can place them in their own sub-folder.

If you add files like images or CSS stylesheets, they belong in /static.

If you want to add your own global functions, place the files in /lib.

After they have been put in the lib folder, you can add an include to that file in /init.php and it will be included globally in the game.

Examples

For examples of how real modules are used, check out the source code! All the modules included in ezRPG are completely usable, and demonstrate different ways of writing different types of modules.

Link to comment
Share on other sites

  • 4 weeks later...
How would you call a hook in the TPL file? Can you? This would make it easier to display $msg.

Sorry, hooks aren't available in template files.

To use $msg to display messages to the user, you can either redirect to the same url with &msg=text.

Or you can manually set the template variable GET_MSG.

Header redirects are used throughout ezrpg, mostly for security, to stop repeat refreshes and accidental refreshes.

 

Can you use Javascript in TPL files?

Yes, you can use javascript in template files.

 

How would you do mysql_fetch_assoc() in $db?

The fetch function uses mysql_fetch_object by default. Is there any reason why you want an array result set instead?

I could add it to the class in the next release if you want.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...