Types: After?

BlitzMax Forums/BlitzMax Beginners Area/Types: After?

ghislain(Posted 2005) [#1]
In BlitzClassic I used the following code to cycle through custom types:
; somewhere in during setup
global CurrentType.aType = first Type

while not keydown(1)
  ; further on in main loop
  ; cycle through the types one at a time
  ; during each game cycle

  currentType = after currentType
  if currentType = Null then currentType = first Type

  ; do something with currentType
wend

Is it possible to do something like this in BlitzMax without converting the list to an array?


teamonkey(Posted 2005) [#2]
You can do something like
Local myList:TList = CreateList()
' Push some values of type TMyType on the list here

Local currentLink:TLink = myList.FirstLink()
Assert(currentLink <> Null)

While Not Keydown(KEY_ESCAPE)
    Local currentType:TMyType = currentLink.Value()

    ' use currentType here

    currentLink = currentLink.NextLink()
    If(currentLink=Null)
        currentLink = myList.FirstLink()
    EndIf
Wend


There's also a mysterious TLinkEnum class which might also do what you want, but I've yet to delve in to that.


Robert(Posted 2005) [#3]
Add the objects you want to cycle through to a list, and then use For...EachIn to loop through them.

Eg:
Local typeList:TList=New TList

Local objA:MyType=New MyType
Local objB:MyType=New MyType

ListAddLast typeList,objA
ListAddLast typeList,objB

For Local currentObj:MyType = EachIn typeList
     'Do something with currentObj
Next



ghislain(Posted 2005) [#4]
Thanks Teamonkey. That's what i'm look for.
I had to make a minor change:
currentType:TMyType = TMyType ( currentLink.value () )


It seems to take a bit more code to do it compared to BlitzClassic. But it is ok.

Robert, i appreciate your help. But sometimes it isn't necessary to cycle through the entire list every gameloop.

Ghislain


FlameDuck(Posted 2005) [#5]
Robert, i appreciate your help. But sometimes it isn't necessary to cycle through the entire list every gameloop.
Then use Exit or Return.


ghislain(Posted 2005) [#6]
Hi FlameDuck.

Let me give you an example of what i'm trying to do.

Let's say I've got 100 vehicles and i would like to check that each vehicle doesn't get to close to another vehicle. Now i could make a 'for .. eachin ..' loop and check each vehicle-vehicle distance every game-cycle. That would be approx 100x100 checks. When proximity detection isn't that time critical i could also do the checks over a longer timespan. Maybe 50 at a time? At 60fps it would then take 3.33 seconds to do all checks. And that is what i would like to do.

If you would know a way of this by using Exit or Return I would like to ask you to share it with me..

Ghislain


ImaginaryHuman(Posted 2005) [#7]
Isn't there a better algorithm that dramatically cuts down on how many comparisons you need to do, so that you don't have to check every single vehicle against every single other vehicle?

Like, dividing things into zones or creating a sorted list of coordinates or something?


ghislain(Posted 2005) [#8]
You are right. There are probably algorithm's to do that.
It was just example to get the idea. But like your forum post elsewhere "How to keep track of EachIn loop counter?", can you 'pause' a eachin-loop, return to the mainprogram and continue later on?
I'll stick with Teamonkey's solution for the time being..

Ghislain


teamonkey(Posted 2005) [#9]
I've just been looking at TListEnum which has HasNext() and NextObject() methods. You can use it like this:
Local myList:TList = CreateList()
Local myEnum:TlistEnum = myList.ObjectEnumerator()

Local currentObj:TMyObj = TMyObj(myList.First())

'...

If myEnum.HasNext()
    currentObj = TMyObj(myEnum.NextObject())
EndIf

Unfortunately there's no way of resetting it back to the beginning of the list once it reaches the end. You might be able to Extend it though. The file is mod/brl.mod/linkedlist.mod/linkedlist.bmx


dynaman(Posted 2005) [#10]
I have not looked at the new types much, but a brute force method of only checking certain type instances would be to add a check interval indicator to each type. do the foreach in thing and only do the processing for the ones that match the current check.

Below is really psuedocode...

itemstocheck = 3
For Local currentObj:MyType = EachIn typeList
     if currentobj.checkindicator = itemstocheck then
       'Do something with currentObj
     end if
Next