Signals and Slots

Started by
22 comments, last by Emmanuel Deloget 18 years ago
Hey everyone, Lately I've been investigating the concepts of signals and slots. So far, they seem like a great way to implement the Observer pattern on a generic level. I'm wondering about a couple things, though. First, how often are signals and slots used in game programming? Second, are there any disadvantages to implementing them? I look forward to your answers. [smile]
Advertisement
Hey there.

Yea, signals are a great OOP tool. As for game programming, the only drawback I can think of is their speed. They usually need a few extra functions on the stack to get to the functions that are supposed to be called. This however is only a drawback for heavy duty functions. For the general "game-level" programming, they are great.
About the implementation, I'd rather go with libsigc++ or boost::signals than implementing them yourself.
Quote:Original post by LtJax
Hey there.

Yea, signals are a great OOP tool. As for game programming, the only drawback I can think of is their speed. They usually need a few extra functions on the stack to get to the functions that are supposed to be called. This however is only a drawback for heavy duty functions. For the general "game-level" programming, they are great.
About the implementation, I'd rather go with libsigc++ or boost::signals than implementing them yourself.


Thanks for your reply, LtJax! [smile]

Since I'm developing games in Java, I can't use either of those great libraries that you mentioned. Right now I'm trying to devise a simple signal/slot system using Java's Method objects (part of its Reflection API). Hopefully doing this will not require much more overhead than what you mentioned with C/C++.
Quote:Original post by RobAU78
Quote:Original post by LtJax
Hey there.

Yea, signals are a great OOP tool. As for game programming, the only drawback I can think of is their speed. They usually need a few extra functions on the stack to get to the functions that are supposed to be called. This however is only a drawback for heavy duty functions. For the general "game-level" programming, they are great.
About the implementation, I'd rather go with libsigc++ or boost::signals than implementing them yourself.


Thanks for your reply, LtJax! [smile]

Since I'm developing games in Java, I can't use either of those great libraries that you mentioned. Right now I'm trying to devise a simple signal/slot system using Java's Method objects (part of its Reflection API). Hopefully doing this will not require much more overhead than what you mentioned with C/C++.


Reflection? It might. The C++ solutions incur little overhead other than an extra function call (you call the signal, the signal calls the function), because it uses partial template specialization to produce the correctly typed object at compile time.
Take a look at ClanLib. It uses signals and slots primarily for input, but it can be used for anything else you want.
If you're using C++, check out the boost.signals library
I know people really like Boost and for good reason, but there are alternatives. If you want something very lightweight and portable for C++, you might want to look at this:

http://sigslot.sourceforge.net/

As you seem to be interested in it from an academic point of view, you might also be interested in the paper Sarah Thompson wrote:
http://sigslot.sourceforge.net/sigslot.pdf

Try to back in to a pattern, prefer simple until that kind of complexity is required. The observer pattern is useful to add flexibility to your code. It also encourages you to split off the game logic and the game view, which is no bad thing (means it's easier to develop test firsts that use the game logic, then building the view seperately on top).

Use whatever works for you.


EDIT: Ah noticed you wrote you are using Java. Well that's got a few things already in there to help you out, and there are tons of example pattern implementations in Java - the classes that make up the sdk itself are littered with example uses of the observer pattern.

[Edited by - paulecoyote on April 4, 2006 5:02:30 PM]
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Quote:Original post by RDragon1
If you're using C++, check out the boost.signals library


I believe he stated that he'll be using Java.

I don't know much about java, but if functions are first-class objects and allows for weak references to GC objects, then it should be simple to create a signal/slot mechanism. I created a signal/slot library for python last year. Even though it's written in python, it might provide a good guideline to follow for a Java implementation.
Quote:Original post by smr
Quote:Original post by RDragon1
If you're using C++, check out the boost.signals library


I believe he stated that he'll be using Java.


My mistake and apologies then, I didn't read the entire thread.

Hey guys,

Thanks for your replies!

I've now been able to implement a rather simple signal/slot mechanism in Java. It uses just one extra class, the Signal class. However, it's a little more difficult to use it compared to C++ implementations. For one thing, Java's Method objects (the closest thing to C++ function pointers) can only be invoked with Object arrays that contain the parameters. Another issue really concerns connecting the Signal objects with Methods to begin with. Should the connections occur in the constructors? Or should there be a separate method to handle the connections? Does it really matter? Finally, I'm also wondering if it's not overkill to implement signals/slots in my game, since it's a relatively simple one (for those who haven't seen it, feel free to click on the link in my signature and download the source code -- critiques are welcome). Again, I'd appreciate any and all help with this. [smile]

This topic is closed to new replies.

Advertisement