Linked lists or dynamic arrays?
Blitz3D Forums/Blitz3D Beginners Area/Linked lists or dynamic arrays?
| ||
Hi all, first post and just trying to find my way around the blitz programming world so sorry if this is a really stupid question! I see using TYPES as the way to hold data relating to the same graphical object in one place but is there a way to dynamically grow or shrink an array of these objects? I really don't want to set an arbitrary limit on the number of objects that I have hard coded in the source.... After a first look through the help files for Blitz3D, I can't see a way to create a pointer to a variable type and then point it at an areas of memory so should I accept that this is just not possible with Blitz3D? |
| ||
You can emulate this in Banks. |
| ||
I noticed I could use banks to set aside areas of memory dynamically. Is there a way I can set the address of a pointer to somewhere in this bank to then access as a 'normal' variable? |
| ||
So would: bnkTest=CreateBank(500) mine.MyType=bnkTest+50 leave mine as a reference to an area of memory 50 bytes into my bank, allowing me to reference properties of my type such as mine.x, mine.y and mine.velocity providing I defined a type with such properties? |
| ||
not that I know, but basicly the diffrence is only how the syntax is looking, eg. pointer=200 print myvar(bank,pointer) function myvar(b,p) return peekint(b,p) end function |
| ||
You can resize basic (Dim) arrays, but they're always global. I believe the only thing you can do with Banks is Peek and Poke the bultin types (ints, floats, strings?) Object() and Handle() are undocumented commands which let you work with pseudo-pointers. It sounds like you're trying to implement containers? Check out how I did it. |
| ||
Thanks jfk, you've answered my question in that banks may not be the way to go if I want to use a user-defined type. octothorpe, many thanks as that's just the sort of thing I needed. Although I am a little wary using undocumented commands 2 hours after installing my copy of B3D!!! PS: "We can now safely blow away our cheese()" - getting that line into a tutorial deserves extra cool points! |
| ||
You should. Everyone-1 think that handle/object work well. I always used them without hassle. type mytype field a% end type atype.mytype=new mytype aptr%=handle(atype) deref.mytype=object.mytype(aptr) |
| ||
Trodd: I'm going to be making good use of Ccto's container code for my Spheres project. you could do worse than start your B3D experience by using stuff Octo's written ;) |
| ||
Trodd: are you aware that blitz automatically stores each collection of type objects in a linked list, internally? You can then iterate through all the type objects using a For/Each loop. This may provide all the functionality you're after, I'm not sure. |
| ||
big10p, I was aware of that and iterating through all of them will be useful. I'll just need to have a way of storing handles to other objects within their type so I can link them to other objects that are 'related'. Sorry if that doesn't make much sense! |
| ||
Well, you can have type handles as fields, like so:Type foo Field a End Type Type bar Field a Field b.foo End Type ...but you can't do pointer arithmetic in blitz. |
| ||
And you can store a predefined number of pointers to objects:Type bar Field b.foo[99] End Type But not an arbitrary number of pointers, as "Blitz arrays" cannot be resized. |