get next object in list that is red
BlitzMax Forums/BlitzMax Beginners Area/get next object in list that is red
| ||
if i had a list of ships how would I get the next one in the list that had a colour field of "red" also if the one I have selected is the last red one on the list how do I get the first red one on the list ? e.g. type ship global list field colour$ endtype |
| ||
you could either: use EachIn to iterate through the list and look for red ones (the first one you find would be the first in the list). or: have a separate list for each colour or (if you're insane) have a separate type for each colour and then iterate with: For Local ship:TRedShip = EachIn MyBigShipList print ship.name Next that would only select ships that inherited from the TRedShip type |
| ||
You could keep a record of the last list item checked plus a count of how many items on the list and do a for/loop with a ValueAtIndex command or go down to the TLINK level and use NEXTLINK, LASTLINK and FIRSTLINK |
| ||
thanks Ill have to delve into the link commands |
| ||
I did this a while back, hope it helps. http://www.blitzbasic.com/codearcs/codearcs.php?code=1807 it allows local list:TListF = new TListF for blah:yourtype = eachin list.From(start:TLink,howMany:int=-1)or for blah:yourtype = eachin list.ReverseFrom(start:TLink,howMany:int=-1)or for blah:TLink = eachin list.LinksFrom(start:TLink,howMany:int=-1) if howMany is -1 then it will continue to the end or the begining as in the case of ReverseFrom. |
| ||
I get an error when I try to run the demo and the example program attempt to access null object on this line: Return (_link._value<>_link) And _howMany |
| ||
whoops... that's because I left some test code in... just comment out the line t.list.Clearnear the bottom. or just recopy the code. |
| ||
have a separate list for each colour Yeah, I would go with using seperate lists for each color, if you really want to go the list route. If you're iterating through a list to find a 'red' item, having this in a loop is going to slow things down quite a bit, otherwise I guess it can't hurt. You can take out the whole sorting process just by having seperate lists, as you will only require the first item in the list, instead of looking for the first item with X attribute in the list. |
| ||
i can 'select' ships so I could select the fifth one and then need the next one, i spose i could use nextlink for that. |
| ||
yes, if you have the link. in my types I always have a field called link so that I can do that among other things.... make sure you have and call a remove method to remove the object. example Type TObject Global list:TList = New TList Field link:TLink Field x:Int Field y:int Method New() link = list.AddLast(Self) End Method Method Remove() link.Remove End Method End Type |
| ||
Aren't you are asking for serious problems doing that? Circular references etc? |
| ||
no.... not with the example above anyway, that's why you put all your clean up in the one method. you should also add in the method super.Remove if appropriate. if you have any references to other objects, this is where you would null them out to prevent a circular reference problem. you don't need to null "link" because link.Remove breaks the chain by nulling it's object reference. |