Hello all Juce persons who know more than me (i.e. all of you).
I'm having trouble using the Message / handleMessage system with Juce, and was wondering whether someone would mind writing a beginners tutorial in everyday terms please. From what I gather, the Message system is a bit like a trainline where a message is tagged with a destination and drops off information when it gets there, compared to ChangeBroadcasting where the train simply notifies a station that it has arrived and something needs to be done. Also, I gather that the message itself is a custom made parcel which you make yourself by creating a struct or class that inherits from message, send by a class that inherits from MessageListener (why it's not a MessageBroadcaster eludes me but I'm sure there is a good reason). I've studied the Demo parts where it's used but still can't work it out, I simply want a broadcaster and listener system where I design the message sent.
Thank you for all help, I'm sure some other beginners will find it helpful too!
Not sure what we could do in a tutorial for messages - you create one, post it, and it arrives later at the MessageListener's callback method.. That's about all there is to say about them! What aspects are you confused about?
Thank you, I based a career in music technology consultancy pretty much based on creating personal analogies for people to understand and learn things haha!
I think code may speak more than words here:
Specialised Message class.h:
#ifndef CHANGEOUTMESSAGE_H_INCLUDED
#define CHANGEOUTMESSAGE_H_INCLUDED
#include "../JuceLibraryCode/JuceHeader.h"
class ChangeOutMessage : public Message
{
public:
ChangeOutMessage(const String&);
String unique_id;
};
#endif
.cpp
#include "ChangeOutMessage.h"
ChangeOutMessage::ChangeOutMessage(const String& a) : unique_id(a)
{
}
Function in parent class that creates an object of Message class (MessageListener inherited):
void ChangeOut::makeChangeMessage(const String& a)
{
postMessage(new ChangeOutMessage(a));
}
Called from elsewhere in the program, Pulse being an object of the calling parent class (ChangeOut):
What I don't understand is where the 'identifier' of the message is stated, how does one 'match up' the message sent and the listener to which it is supposed to go? In the ChangeBroadcaster it is easy to add a listener and divide up the broadcast sources with ifs as in the button listener, Once it arrives at the correct place, how does one go about seperating the required information from the message?
I've had another bash at this but still to no avail. It's the basic premise of getting the handleMessage function to react to a message.
All messages are sent down the same track. Therefore an identifier in the message must match with the handleMessage function so it reacts. I presumed that this would be as simple as a system like:
postMessage(new MessageClass (String Identifier);
handleMessage(const Message& StringIdentifier)
But this, and various permutations does not give results.
A very simple, explicit example of a Message class, posting function and handleMessage would be greatly appreciated.
Are you just confused over basic C++ syntax stuff? What you're saying sounds fine: post a message to a MessageListener object, and it'll definitely arrive in that object's handleMessage method. Simple as that. The example I pointed you at above does exactly that, and if you search the codebase for "postMessage" or "handleMessage" there are lots of other examples.
Ah, I think I may have seen the problem there. I'm trying to listen for the message in a different object, that's what I thought the Message system was for, can you not send messages between objects?
I thought that it worked similarly to ChangeMessage broadcasting/listening, it seemed obvious that that would be it's functionality, looks like I was very wrong! Is there a way to send data along with a ChangeMessage?