Skip to content

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

Notifications You must be signed in to change notification settings

GammaGames/Signal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages