GammaGames / Signal

A Lua class for subscribing to keys and notifying subscribers of that key.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Signal for Playdate

Lua Version Toybox Compatible Latest Version

Signal is a Lua class for subscribing to keys and notifying subscribers of that key by Dustin Mierau.

You can add it to your Playdate project by installing toybox.py, going to your project folder in a Terminal window and typing:

toybox add Signal
toybox update

Then, if your code is in the source folder, just import the following:

import '../toyboxes/toyboxes.lua'

This toybox contains Lua toys for you to play with.


In this example we create a global instance of Signal, subscribe to a key, and notify against that key elsewhere. Notice that all of the values passed to Signal:notify are passed on to the subscribed functions.

-- ... creating a global variable in main ...
NotificationCenter = Signal()

-- ... in code that needs to know when score has changed ...
NotificationCenter:subscribe("game_score", self, function(new_score, score_delta)
   self:update_score(new_score)
end)

-- ... in code that changes the score ...
NotificationCenter:notify("game_score", new_score, score_delta)

About

A Lua class for subscribing to keys and notifying subscribers of that key.


Languages

Language:Lua 100.0%