Communication is Key: Signals and Slots in Games

When developers talk about the way a game is made, they usually talk about things like physics, graphics, AI, and sound. There is another big aspect however: Communication. I am not talking about players or game characters talking to each other or networking a game. I am referring to objects in the game sending messages to each other. When a button is pressed, the door must be told to open. When the player scores a goal, there must be cheering and confetti.

There are many ways communication is handled in games and I do not wish to compare all the methods. I want to write about a method I like and tend to use called Signals and Slots (specifically, my implementation). Signals emit messages to Slots. A Signal can be viewed as a radio tower that only sends messages while a Slot can be viewed as a boombox which can only receive the message. Signals only emit to slots they are connected to (efficient). Any Signal may connect to any Slot. Any number of Signals may connect to a Slot.

The radio tower (Signal) emits a message to the boomboxes (Slots)

The radio tower (Signal) emits a message to the receivers (Slots)

Signals simply emit a message. An object tells the Signal to emit. Slots call a function once they are emitted to. This function can be in an object, in script, or even on a remote machine across the network. Any object in the game may own any number of Signals and Slots. When that object is destroyed, its Signals and Slots automatically get disconnected from any other Signals and Slots they were connected to. Once you have a framework of generic Signals and Slots, they can be connected in surprising and emergent ways. Below is an example of some objects and their Signals and Slots:

SoundEffect:
Slots - Play, Stop

SpecialEffect:
Slots - Play, Stop

SoccerGoal:
Signals - GoalScored

SoccerBall:
Signals - BallHit
Slots - ResetBall

Now, let’s connect them:

SoccerGoal goal1
SoccerGoal goal2
SoundEffect cheer
SpecialEffect confetti
SoccerBall ball
SoundEffect ballHit

goal1.GoalScored.Connect(cheer.Play)
goal1.GoalScored.Connect(confetti.Play)
goal1.GoalScored.Connect(ball.ResetBall)

goal2.GoalScored.Connect(cheer.Play)
goal2.GoalScored.Connect(confetti.Play)
goal2.GoalScored.Connect(ball.ResetBall)

ball.BallHit.Connect(ballHit.Play)

So, when the ball is hit by a player, a sound effect will be played. When the ball is hit into either of the team’s goals, we will hear cheering and see confetti throw into the air. The ball will also reset to the middle of the field when a goal is scored.

Signals may also emit data to Slots. My implementation wraps all parameters, which can be numbers, strings, bools, etc, into an object so that any Signal can emit to any Slot. As mentioned earlier, because Signals and Slots are objects, they can exist in script. This makes it really easy for objects in script to send and receive messages from objects in the game.

This was intended to give a brief overview of what Signals and Slots are and how they can benefit a game. Some other aspects to think about are how they can be used in an editor, how they can work over the network, and how modders can take advantage of them.

I also wanted to mention that the popular service, Twitter, is really just a giant network of Signals and Slots.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • MySpace
  • N4G
  • Reddit
  • StumbleUpon
  • TwitThis

7 Comments

  • Sinbad Says:

    Nice article. Do Take it then that you wrote your own signal/slot implementation, or did you wrap another library?

  • Murphy Says:

    Thanks :)

    I started with a wrapper around boost::signals but am moving away from it as I don’t require it anymore since all my function signatures are the same. boost::signals also seemed a bit slow compared to how fast it should be.

    I wrote my own implementation to support sig+slot in script code mainly.

  • banal Says:

    Nice article, thank you. I really liked the signals and slots implementation of QT (http://doc.trolltech.com/4.5/signalsandslots.html), sadly this requires the QT meta-object-compiler (moc). I wasn’t aware that there’s an implementation in boost, so thanks for pointing that out. I’ll definitely have a look at it, since signals and slots seem to be a very good pattern for game-object communication.

  • Murphy Says:

    QT was the first place I saw signals + slots in use. Another library to check out is http://sigslot.sourceforge.net/ although I have not used it myself.

  • Lf3T-Hn4D Says:

    sigslot is old and unmaintained. I used to use it, but gave up after hitting certain limitations. It’s weird that such useful feature is so under developed. We just don’t have enough good choices to pick from. :P

  • Murphy Says:

    It isn’t very hard to write your own signal + slot code either. It is pretty simple if you don’t care about handling every type of parameters and return values.

  • [...] a crash. Turned out to be an issue if a sensor thought an object still existed when it did not. Signals + Slots to the rescue! The other big project was to contain anything that modified the physics world into [...]

Leave a Reply