Jump to content
  • Advertisement
Sign in to follow this  
RobAU78

Signals and Slots

This topic is 4478 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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++.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!