My very own object module

An important part of Ophelia’s design is the ability for users to define their own object feature modules. But what is an object feature module? It is a relatively small, self-contained module that can add new functionality to game objects. Almost all of the core game object functionality in Ophelia, such as Movement and Collision, have been written in the form of object feature modules.
Let’s take a look at what we need to do to define our own modules:

/* Import the base Object module */
var ObjectModule = require('../../../server/game objects/object module');

/* This contains the interface that will be imported by other files */
module.exports = {
	
	/**
	*	A module must define 'create'
	*	Creates an instance of this module
	*/
	create : function(gameObjects){
		
		/* Create a module 
		/* gameObjects is a reference to the instance of
		/* the gameObjects manager that this module is in */
		var Module = ObjectModule.create(gameObjects);
		
		/**
		*	A module must define 'onAttach'
		*	It is invoked when an object is attached
		*	This is the place to add attributes and events
		*	to objects that attach the module
		*/
		Module.onAttach = function(obj){
			
			/* Register new attributes */
			obj.addAttribute("exampleAttribute", 0, 'float');
			
			/* Register new local events */
			obj.registerEvent('onExampleEvent');
			
			/* Register callbacks to local events */
			obj.registerCallback('onUpdate', function(){
				
			});
		}
		
		/* Return the module */
		return Module;
	}
};

/* We have to register our module with the manager */
GameObjects.registerModule('example module', module.exports);

As we can see, the only requirement is a function ‘create()’ that creates an object module and returns it, an onAttach() method for the module that adds it’s attribute, events and logic, and finally; a call to GameObjects.registerModule() to have the module registered with the engine. Once this is done, we can start using our module, we can even define object templates that include it. We will get to know more about creating your own templates, as well as other content files, in an upcoming post.

Be Sociable, Share!

Published by

Albert Öhrling

Studies computer science in Skellefteå, at Lulea University of Technology.

Leave a Reply

Your email address will not be published. Required fields are marked *