This is a javascript plugin that will not do anything on it's own, but can be useful for other scripters. This script essentially implements an observer pattern into RPG Maker. Event systems help modularize your code by sending messages between pieces of code that do not directly know about each other. They allow things to respond to a change in a state without constantly monitoring it in an update loop. You can read more about it here: Observer pattern - Wikipedia
en.wikipedia.org
This script hasn't been fully optimized and I'm sure there are some things that may not be fully standardized with RPG Makers coding style. It was made in a couple of hours. On every triple a project I've worked on so far, each engine has had one of these, so I wanted to make my own for my internal project. Hopefully this comes in handy for you!
This was made with RPG Maker MV in mind. I have not tested this with RPG Maker MZ, but I don't see much of a reason why this wouldn't work with MZ as well.
I made a small project with a couple of use-case examples! Nothing mind-blowing, but hopefully will provide you with a few ideas to get some ideas across for how it can be used! You can find that project here.
Terms of Use: Free to use or modify for free or commercial projects. My only request is that you don't republish this (or your edits) and claim it as your own. If you want to give credit (not required), please give to Nathan Pringle and specify it was this script in particular.
* string eventName: The event you want to listen to. When this event is invoked, the callbackMethod will be called.
* string methodName: The string name of the method. This is simply used for lookup. When you stop listening, you will need to provide this string again.
* method methodCallback: The method to call when the event is invoked.
* method condition: a method that should only return true or false. Will be checked every frame. When the condition returns true, the event will be invoked once.
/// Summary: This will call the event, but also pass a single parameter to all methods being invoked. Will call all methods that are listening for this event.
/// Argument - eventName: The name of the event to invoke.
/// Argument - parameter: The data to pass to the methods.
/// Returns: N/A
EventManager.InvokeWithParameter = function (eventName, parameter) {
var eventExists = false;
var eventIndex = -1;
for (var i = 0; i < this._eventDictionary.length && !eventExists; ++i) {
var eventEntry = this._eventDictionary[i];
if (eventEntry.eventName == eventName) {
eventExists = true;
eventIndex = i;
}
}
if (eventExists) {
var eventDetails = this._eventDictionary[eventIndex];
for (var i = 0; i < eventDetails["methods"].length; ++i) {
/// Summary: This will call the event as invoke, but will do so after a certain amount of time.
/// Argument - eventName: The name of the event to invoke.
/// Argument - time: The amount of time in seconds.
/// Returns: N/A
EventManager.InvokeAfterTime = function (eventName, time) {
var entry = {};
entry["eventName"] = eventName;
entry["time"] = time;
this._eventsToCallAfterTime.push(entry);
}
/// Summary: This will call the event as invoke, but will do so only when a certain condition is true.
/// Argument - eventName: The name of the event to invoke.
/// Argument - condition: a method that should return either true or false. It will be checked every frame. When it returns true, the event will be invoked. Will only be invoked once.
/// Returns: N/A
EventManager.InvokeWhenConditionIsTrue = function (eventName, condition) {
var result = condition.call(this);
var entry = {};
entry["eventName"] = eventName;
entry["condition"] = condition;
this._eventsToCallWhenCondition.push(entry);
}
/// Summary: A single tick. Should not be called outside of Window.update
/// Returns: N/A
EventManager.Tick = function() {
for (var i = 0; i < this._eventsToCallAfterTime.length; ++i) {
for (var i = 0; i < this._eventsToCallWhenCondition.length; ++i) {
var result = this._eventsToCallWhenCondition[i]["condition"].call(this);
if (result == true) {
var eventName = this._eventsToCallWhenCondition[i]["eventName"];
this._eventsToCallWhenCondition.splice(i, 1);
--i;
EventManager.Invoke(eventName);
}
}
}
/// Summary: Starts listening for a specific event. When a piece of code calls the invoke method, this method will be called.
/// Argument - eventName: The name of the event to listen to.
/// Argument - methodName: The string name of the method. This is simply used for lookup. When you stop listening, you will need to provide this string again.
/// Argument - methodCallback: The method to call when the event is invoked.