Basic Message Passing

BlitzMax Forums/BlitzMax Programming/Basic Message Passing

zoqfotpik(Posted 2014) [#1]
This is a very simple demo of a message repository. This sort of style is good for modularizing all sorts of things from AI to collisions to control schemes. This is getting into post-OO style and I don't know if I'm on board with it-- I'm old fashioned that way. You could use it to pass around function pointers or whatever, or escape certain kinds of scope headaches, which may break encapsulation but adds another sort of very powerful modularization.

To use this for collisions, you could give every square a broadcaster, then have everybody subscribe to a square as soon as they entered it. Then everyone in each square inter-collides, and you've just culled down the number of collisions you have to do to something reasonable.

You can also have it log every single change or interaction to everything, which is very useful for debugging. It may seem like a convoluted way of doing things but the time for a new subscription is negligible.

' Message Passing
Global Broadcaster:Tbroadcaster = New TBroadcaster
Global subscriberlist:subscriber[100000]
Global time

Function timer()
	If time > 0 
		Print "Millisecs elapsed since last call:" + ( MilliSecs()-time)
		time = 0
	Else time = MilliSecs()
	EndIf
End Function

Type Tbroadcaster
	Field subscribers:Int[100000]
	Field numsubscribers:Int
	
	Method addsubscriber:Int(subscribernum:Int)
		numsubscribers = numsubscribers + 1
		subscribers[numsubscribers]=subscribernum
		'Print "added subscriber:" +numsubscribers
	End Method

	Method removesubscriber(toremove:Int)
		subscribers[toremove]=subscribers[numsubscribers]
		numsubscribers = numsubscribers - 1
	End Method
	
	Method getnum:Int(subscribernum:Int)
		Return subscribers[subscribernum]
	End Method
	
	Method sendthing(message:Int,tobox:Int)
		subscribers[tobox]=message
		'Print "Sending " + message + " to box " + tobox
	End Method
End Type


Type subscriber
Global NUMSUBSCRIBERS:Int
	Field index:Int
	Field value:Int
	
	Method New(mybroadcaster:Tbroadcaster)
		NUMSUBSCRIBERS:+1
		index = NUMSUBSCRIBERS
		Broadcaster.addsubscriber(index)
	End Method

	Method update()
		value = Broadcaster.getnum(index)
	End Method
End Type

test:subscriber = New subscriber
broadcaster.sendthing(12345,1)
test.update()
Print test.value


Print "100000 Adds:"
timer()
For i = 0 To 99999
	subscriberlist[i] = New subscriber
Next
timer() 

Print "100000 Updates:"
timer()
For i = 0 To 99999
	subscriberlist[i].update()
Next
timer()



Derron(Posted 2014) [#2]
Similar:
https://github.com/GWRon/Dig/blob/master/base.util.event.bmx


Maybe you should think about skipping the preallocation of 100.000-entry arrays.


bye
Ron


zoqfotpik(Posted 2014) [#3]
Yeah. If I was concerned I would use doubling. Maybe I will go in and use dynamic allocation.

How far things have come since 48k... RAM was not cheap on the Apple ][.

Any other interesting ideas for using this? I'll be using it in an action roguelike, it's so I can have things subscribe to AI sources, etc.