Thrive Game Development
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Thrive Game Development

Development of the evolution game Thrive.
 
HomeHome  PortalPortal  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  
Welcome new and returning members!
If you're new, read around a bit before you post: the odds are we've already covered your suggestion.
If you want to join the development team, sign up and tell us why.
ADMIN is pleased to note that this marquee has finally been updated.
ADMIN reminds you that the Devblog is REQUIRED reading.
Currently: The Microbe Stage GUI is under heavy development
Log in
Username:
Password:
Log in automatically: 
:: I forgot my password
Quick Links
Website
/r/thrive
GitHub
FAQs
Wiki
New Posts
Search
 
 

Display results as :
 
Rechercher Advanced Search
Statistics
We have 1675 registered users
The newest registered user is dejo123

Our users have posted a total of 30851 messages in 1411 subjects
Who is online?
In total there are 16 users online :: 0 Registered, 0 Hidden and 16 Guests :: 1 Bot

None

Most users ever online was 443 on Sun Mar 17, 2013 5:41 pm
Latest topics
» THIS FORUM IS NOW OBSOLETE
Using signals Emptyby NickTheNick Sat Sep 26, 2015 10:26 pm

» To all the people who come here looking for thrive.
Using signals Emptyby NickTheNick Sat Sep 26, 2015 10:22 pm

» Build Error Code::Blocks / CMake
Using signals Emptyby crovea Tue Jul 28, 2015 5:28 pm

» Hello! I can translate in japanese
Using signals Emptyby tjwhale Thu Jul 02, 2015 7:23 pm

» On Leave (Offline thread)
Using signals Emptyby NickTheNick Wed Jul 01, 2015 12:20 am

» Devblog #14: A Brave New Forum
Using signals Emptyby NickTheNick Mon Jun 29, 2015 4:49 am

» Application for Programmer
Using signals Emptyby crovea Fri Jun 26, 2015 11:14 am

» Re-Reapplication
Using signals Emptyby The Creator Thu Jun 25, 2015 10:57 pm

» Application (programming)
Using signals Emptyby crovea Tue Jun 23, 2015 8:00 am

» Achieving Sapience
Using signals Emptyby MitochondriaBox Sun Jun 21, 2015 7:03 pm

» Microbe Stage GDD
Using signals Emptyby tjwhale Sat Jun 20, 2015 3:44 pm

» Application for Programmer/ Theorist
Using signals Emptyby tjwhale Wed Jun 17, 2015 9:56 am

» Application for a 3D Modeler.
Using signals Emptyby Kaiju4u Wed Jun 10, 2015 11:16 am

» Presentation
Using signals Emptyby Othithu Tue Jun 02, 2015 10:38 am

» Application of Sorts
Using signals Emptyby crovea Sun May 31, 2015 5:06 pm

» want to contribute
Using signals Emptyby Renzope Sun May 31, 2015 12:58 pm

» Music List Thread (Post New Themes Here)
Using signals Emptyby Oliveriver Thu May 28, 2015 1:06 pm

» Application: English-Spanish translator
Using signals Emptyby Renzope Tue May 26, 2015 1:53 pm

» Want to be promoter or project manager
Using signals Emptyby TheBudderBros Sun May 24, 2015 9:00 pm

» A new round of Forum Revamps!
Using signals Emptyby Oliveriver Wed May 20, 2015 11:32 am


 

 Using signals

Go down 
4 posters
AuthorMessage
Nimbal
Programming Team lead



Posts : 258
Reputation : 24
Join date : 2013-03-17
Age : 40
Location : Ratingen, Germany

Using signals Empty
PostSubject: Using signals   Using signals EmptyFri Apr 05, 2013 5:07 am

Hi everyone,
I just pushed the first version of a signalling framework to the signals branch in my fork at GitHub. I'll issue a new pull request to the base repository when the build system changes have been merged because the signals depend on those changes.

I already talked briefly about signals in my post over in the scripting thread (see section "Reacting to third-party changes"). Here, I'll try to describe signals and their intended purpose in a little more detail.

What's a signal?

A signal is a way to broadcast information to interested receivers. They are a form of the observer pattern. A typical usage is to send notification of state changes in an object. Say we want to react to position changes of a movable entity. We could poll the position regularly, like this:

Code:

while (interestedInPosition) {
    if (entity.position != lastPosition) {
        // Do something
    }
    lastPosition = entity.position;
}

Polling works, but it's not very good, performance-wise. If the entity moves sparingly, we end up doing nothing but comparing the positions over long stretches of time. A better way would be for the entity to tell us about position changes. That's what signals can be used for:

Code:

void onPositionChanged(double x, double y, double z) {
    // Do something
}

// ...

entity.sig_positionChanged.connect(onPositionChanged);

Each time its position changes, the entity takes care to emit the sig_positionChanged signal. The signal, in turn, invokes the onPositionChanged function. A function that is connected to a signal is sometimes called a "slot". Note that signals and slots have a many to many relation. A signal can be connected to any number of slots and a slot can be connected to any number of signals. This furthers code reuse and modularity.

How to use signals in Thrive (C++)

To use the signals found in the above linked branch, include the header "signals/signal.h". All signal related classes can be found in the "thrive::signals" namespace. The following examples will assume a "using thrive::signals" declaration. In header files, please don't use the using declaration but rather fully qualify the name.

To define a signal, you need to tell it about the data types it's supposed to broadcast:

Code:

Signal<double, double, double> sig_positionChanged;

The above signal will broadcast three doubles. Note that if you want to broadcast more complex data, like structs, you should broadcast (ideally const) references to avoid copying:

Code:

Signal<const Component&> sig_componentAdded;

You can connect anything to a signal that is convertable to an std::function<Args>, where "Args" is the signature of the signal. Lambdas and the return value of std::bind are always good:


Code:

auto onPositionChanged = [](double x, double y, double z) {
    std::cout << "New x position: " << x << std::endl;
};
sig_positionChanged.connect(onPositionChanged);

The above would print out a message anytime the signal is emitted. The connect method returns a shared pointer to a connection object that allows you to sever the connection:

Code:

std::shared_ptr<Connection> connection = sig_positionChanged.connect(onPositionChanged);
// Do stuff
connection->disconnect();
Back to top Go down
Daniferrito
Experienced
Daniferrito


Posts : 726
Reputation : 70
Join date : 2012-10-10
Age : 30
Location : Spain

Using signals Empty
PostSubject: Re: Using signals   Using signals EmptyFri Apr 05, 2013 6:11 am

Great work, as always, Nimbal.

About accepting the pull request:
One of the things we agreed is that pull requests need to be checked to check whether it works, by at least one extra programmer other than the one that programmed it. Also checking that the code is right (in all meanings, it doesent introduce bugs, is clear and follows the standard structure and all that).I would like to check it myself, but i'm on a travel and wont be back untill one week from now. If someone else claims your code is right, i will accept the pull request gladly.

The same happens with my pull request to the main branch. Some of the things, like the nodes you talked about in the engine architecture thread, that could be stored by the system themselves.
Back to top Go down
crovea
Programming Team lead
crovea


Posts : 310
Reputation : 59
Join date : 2013-10-07
Age : 34
Location : Denmark

Using signals Empty
PostSubject: Re: Using signals   Using signals EmptyFri Oct 11, 2013 2:57 pm

I would like to add to any new people that i have been informed that the signal system has been scrapped (should be added to the post or possibly just deleted). May be replaced with an event system in the future.
Back to top Go down
NickTheNick
Overall Team Co-Lead
NickTheNick


Posts : 2312
Reputation : 175
Join date : 2012-07-22
Age : 28
Location : Canada

Using signals Empty
PostSubject: Re: Using signals   Using signals EmptySat Oct 12, 2013 12:47 am

Thanks, I'll move it to the archive.
Back to top Go down
Sponsored content





Using signals Empty
PostSubject: Re: Using signals   Using signals Empty

Back to top Go down
 
Using signals
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
Thrive Game Development :: Archive-
Jump to: