Plugin Definition
To get started in making a plugin, understand the following:
- Plugins are automatically imported into the
Enginebefore startup. - There are different types of
Events:LifetimeEvent: Dispatched when the lifetime of your plugin changes (LoadEvent,UnloadEvent, etc.).CancellableEvent: Any type of event that can be cancelled and therefore prevent any further logic from occurring.SceneEvent: Dispatched when any event occurs within anyScene(KeyPressedEvent,MouseScrollEvent, etc.).
- Each event "listener"/handler has a specific time in which it can fire - controlled by
ExecutionStep:BEFORE: Fired before theEngine/Scene/developer even sees the event. If you cancel the event, theEngine/Scene/developer can't handle the event.AFTER: Fired after theEngine/Scene/developer sees/handles the event. If the event is cancelled before you handle it, you won't see the event.ExecutionStep.BEFORE/AFTERdoes not affectLifetimeEventevents.
- The normal game loop within
EngineandScenecannot be cancelled, i.e.LifetimeEventorEngineEvent(not implemented, yet). - Each
Eventhas access to an externalengineattribute, which is a direct reference to the runningEngine. - Each
SceneEventhas access to an externalsceneattribute, which is a direct reference to the runningScenecontrolled by theEngine. - Every type of
Eventhas external attributes accessible depending on the context in which the event was dispatched (keyandmodsfor aKeyPressedEvent, etc.).
Getting Started
Define an inherited class of Plugin from wame.plugins. Give it a load/unload handler:
from wame.plugins import * # Has all events and objects necessary for plugin development
class MyPlugin(Plugin):
@on_event(LoadEvent)
def on_load(self, event: LoadEvent) -> None:
self.log_info("Hello world from MyPlugin!")
@on_event(UnloadEvent)
def on_unload(self, event: LoadEvent) -> None:
self.log_info("Goodbye world from MyPlugin!")
As it stands, this will work wonderfully. The Engine will instantiate as normal, which will now load plugins. This will log:
[ TIMESTAMP ][ INFO ][ MyPlugin ] Hello world from MyPlugin!
[ TIMESTAMP ][ INFO ][ MyPlugin ] Goodbye world from MyPlugin!
Please make sure to do the following before testing your plugin:
- It's within a
plugins/your_pluginfolder in your game's directory. - Your plugin program's filename is
main.py
Now, please note that you cannot override __init__ when making a plugin, it won't work otherwise.
Further notes include:
- Event listener method names like
on_loadare not necessary, it can be named anything. - Please do not get these confused with regular
Scenelistener methods likeon_key_pressed, etc. - The only way to listen to any
Eventis to listen withon_event. - You can have infinitely many event listeners to any one
Event. Keep in mind there is no guaranteed order in which they will be fired besides the control flow ofBEFOREandAFTER, at least for now. - Most regular
Sceneevent listeners have a direct equivalentEvent, i.e.on_init=InitEvent,on_key_pressed=KeyPressedEvent, etc. - Every event listed in
Scenehas an equivalentEventfor plugins.