ASyncLoop

BlitzMax Forums/BlitzMax Programming/ASyncLoop

JoshK(Posted 2008) [#1]
If you have a list of objects that need to be updated, but it isn't important that they all be updated each frame, this code will update just one or more each frame. The update callback should return 1 if any updating has been performed, or 0 if no updating was needed.

This allows you to do things like just update one enemy AI per frame. I am going to try using it for culling.

Strict

Import brl.linkedlist

Type TASyncLoop
	Field list:TList
	Field currentlink:TLink
	Field UpdateCallback%(o:Object,extra:Object)
	
	Method Update(count=1,extra:Object=Null)
		Local link:TLink
		Local o:Object
		Local firstlink:TLink
		If currentlink
			If currentlink._value=Null currentlink=Null
		EndIf
		If currentlink=Null currentlink=list.firstlink()
		If currentlink=Null Return
		firstlink=currentlink
		Repeat
			If count=0 Return
			o=currentlink.value()
			If o
				If UpdateCallback(o,extra) count:-1
			EndIf
			currentlink=currentlink.nextlink()
			If currentlink=Null currentlink=list.firstlink()
			If currentlink=Null Return
			If currentlink=firstlink Return
		Forever
	EndMethod
	
EndType