Sendmessage

BlitzMax Forums/BlitzMax Beginners Area/Sendmessage

tonyg(Posted 2008) [#1]
How is the sendmessage method used?
I simple example of sending a "Hello" message from one object to another would help.


Kistjes(Posted 2008) [#2]


(Blueapples helped me with this one before)


tonyg(Posted 2008) [#3]
Thanks Kistjes.
However, I must be missing something. That example simply creates a method called sendmessage. If you rename them sendmessage_blah they do the same thing.
How is the inbuilt sendmessage used :
The SendMessage method provides a simple, generic messaging system available to all objects. The default implementation of SendMessage simply returns null.

What is the object returned from the sendmessage method for?
<edit> Just to be clear... I don't understand why sendmessage is a provided method. How do I use it without having to define my own sendmessage method?
SuperStrict

Type test1
	
End Type

Local msg:Object="Hello World"
Local mytest:test1=New test1
mytest.sendMessage(msg,Null) 



Kistjes(Posted 2008) [#4]
Ah! Good question. Sorry, can't help you. I would like to know why and when you should use the provided SendMessage method, either.

Anyone else, perhaps?


ziggy(Posted 2008) [#5]
Well, as instance, you could use the object to return the state of the object once the message has been processed.

Imagine this scenario:
Const NoChange:int = 0
Const DeleteShip:int = 1

Type GameEngine
    Field Ships:Tlist 
    ....
    ....
    Method UpdateShips()
        For S:Ship = eachin Ships
            Local Result:UpdateResult = UpdateResult(S.SendMessage("Update"))
            If Result.Status = DeleteShip then
                Ships.Remove(S)
            End If
        Next
    End Method
End Type

Type UpdateResult
    Field Status:Int=NoChange
End Type

Type Ship
    ...
    ...
    Method SendMessage:Object(Message:Object, Param:Object = Null)
        ....
        ....
        If Message = "Update" Then
            Local R:UpdateResult = New UpdateResult
            If ShipCollidedWithLaser then
                R.Status = DeleteShip
                Return R
            Else
                R.Status = NoChange
                Return R
            End If
        End If
    End Method
End Type


this is just a pseudo-code silly example of a possible usage for the sendmessage method


Kistjes(Posted 2008) [#6]
Nice example but again you overrule the default SendMessage method.

Is this default SendMessage method only there to prevent runtime errors to occur when running through all different object types (with or without custom SendMessage methods) in a EachIn loop?


ziggy(Posted 2008) [#7]
More or less. The sendmessage method is a method in the base class 'object'. It is there to have a cross-object way to send messages between objects. Each object has to implement its own message trapping method, or the messages will be ignored (processed by the base object class). It is just a functionallity you can use or not.


tonyg(Posted 2008) [#8]
OK. It seems a bit odd but I now understand the reasoning.
How is it used to actually send messages between objects?
This seems quite funky but is it what sendmessage is for?



Yan(Posted 2008) [#9]
It's just a generic interface included in the base object for you to use in any way you wish...




tonyg(Posted 2008) [#10]
Thanks again although it's inclusion in the language still strike me as odd especially as I have only seen it mentioned 1-2 on these forums and assumed it was network related.
I'd be interested in who is making use of this and what they're using it for.


deps(Posted 2008) [#11]
It's most likely used in MaxGUI to send mouse clicks and keypreses to the widgets.
I don't own MaxGUI so I can't check.


tonyg(Posted 2008) [#12]
I can't find it used (or defined) anywhere.