How to Conveniently Build Messages
BlitzMax Forums/BlitzMax Programming/How to Conveniently Build Messages
| ||
I am implementing a simple message passing system. At this point each message is a stack of floats. Let's say I want to make a message for changecolor. MCHANGECOLOR will be globally defined. I want to do something LIKE: mymessage:TMessage = new TMessage (MCHANGECOLOR, 255,0,0) But have a variable number of arguments (all floats) so that I can pass messages of whatever length, and then have the message receiver act on it based on the first parameter, here MCHANGECOLOR. Is this the best way to do it? Function buildmessage(inputarray:Float[]) For Local i=0 Until inputarray.length < add message parameters from inputarray, more worried about argument passing> Next End Function Local float_array[]=[MCHANGECOLOR,255,0,0] buildmessage (float_array) The question is, how to do this conveniently. Ideally it would be a one liner similar to a function call but the two lines above would be OK. Also, how to do the same thing with multiple data types? eg, for a create function call in pseudocode this would look like mymessage = createmessage(MESSAGETYPE:int, "Item Name":string, foo:int, bar:int, etc etc) For my purposes I am pretty sure I can accomplish most things by floats but building a TList of disparate data types from one or two lines of code, similar to the way arrays can be defined Local float_array[]=[MCHANGECOLOR,255,0,0] would be very handy. Thanks! |
| ||
Hi, not sure if I got it, but usually types have some kind of Create mechanism in order to initialize a new object with parameters. So in your case something like: mymessage:TMessage = TMessage.Create (MCHANGECOLOR, [255, 0, 32]) Print "Colors are: " + mymessage.r + ", " + mymessage.g + ", " + mymessage.b Type TMessage Field r:Int, g:Int, b:Int Function Create:TMessage (flag:Int, color:Int[] ) Local m:TMessage = New TMessage m.r = color[0] m.g = color[1] m.b = color[2] Rem You could alternatively store the array directly into an int array field. EndRem Return m EndFunction EndType -Henri |
| ||
This is the syntax I was looking for:mymessage:TMessage = TMessage.Create (MCHANGECOLOR, [255, 0, 32]) Thanks! |
| ||
If you want to have a generic message-system your param will be of type "object" Of course you cannot pass an integer that easily then (need a wrapper and some casting). For my EventSystem (using string-keys instead of ints): https://github.com/GWRon/Dig/blob/master/base.util.event.bmx I used my TData-Container: https://github.com/GWRon/Dig/blob/master/base.util.data.bmx Maybe it is of use for you. bye Ron |
| ||
I've implemented something but (like suggested by Derron) using string-keys and string-parametersNew Tmessage.Create("FBCOLOR","255,255,255") New Tmessage.Create("TITLE","my tile") the parameters are more elastic, I can separate splitting them with commas, can have different type (int, string, float etc as they are converted )etc |
| ||
BRL.Reflection uses Object[] for argument lists and Object for return values, and converts arguments to and from numbers via String. While other options would be faster (a dedicated box type would certainly seem like a better choice for numbers than String), it's worth at least considering sticking with BRL's convention in case you want to interop easily with it - it's really useful for systems like this to be able to build on Invoke() behind the scenes. |
| ||
You would just set it up as object? That would obviously be hugely handy for other reasons. Is it as simple as writing a parser that looks at the first argument and then responds appropriately to the other arguments in the list? |