Creating Lists, How many ways?
BlitzMax Forums/BlitzMax Beginners Area/Creating Lists, How many ways?
| ||
How many ways are there of creating a list in Bmax? Currently I'm using the following method: Local OjectList:TList = CreateList() I've seen code where a list is created but stored in a type. I hope I'm making sense. I was wondering if storing the list in the type is better than creating it outside the type? Type TObject Field List:TList etc.. etc.. End Type I don't know how to get the second method of using lists working. How do you do it? |
| ||
Type TObject Global ObjectList:TList=CreateList() ... End Type That works. |
| ||
I prefer:Type TMyListWithKnobsOn extends TList Field blah Method foo() End Method End Type This way you can treat it just like a list with AddLast etc + you can add in your own extra commands. Maybe this isn't what you want but I use it in my framework for bullet and alien lists amongst other things. |
| ||
Grey, Explain that a little more. Are you saying that just by extending TList. I could...Using your example above For Local Test:TMylistWithKnobsOn= Eachin MylistWithKnobsOn Print Test.Blah Next or Maybe For Local Test:TMylistWithKnobsOn= Eachin Self Print Test.Blah Next Hmm...I'm going to have to test this. |
| ||
Eric: hmm, sorta but not quite. To print blah (there's only one blah for the entire list!) you would just do this:Local test:TMylistWithKnobsOn test.blah = 100 print test.blah Or make a method in TMylistWithKnobsOn called print, and then call Test.Print() (this is better as you can alter it do what you want). As for looping, well it depends what object you add to the list. For example: Type TMyThing Field X End Type Local test:TMylistWithKnobsOn Local A:TMyThing A.X = 1 Local B:TMyThing B.X = 2 test.AddLast(A) test.AddLast(B) 'here's the loop For temp:TMyThing = eachin TMylistWithKnobsOn Print temp.X 'tada! Next Although in the above example, it would be cool if you made a method in TMylistWithKnobsOn called PrintAll() and it could loop through the list and do the printing so in your maincode all you do is Test.PrintAll(). I haven't written the above in the IDE (I just typed it now) so hopefully the sytax is correct. |