[Implemented] Resize Arrays in Structures
[Implemented] Resize Arrays in Structures
Reference (Near Bottom): viewtopic.php?t=10304&highlight=resize+structure
I would like to be able to resize (Dim) arrays that are in structures.
(Implemented as dynamic Array, where ReDim can be called)
I would like to be able to resize (Dim) arrays that are in structures.
(Implemented as dynamic Array, where ReDim can be called)
you cannot (but why would you?)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
I know you answered this one, thus the whole reason it is in the "Feature Requests and Wishlists" forum......
And why? For example, I have a map data that holds tile information. I hate having artifical (not needed) limits on some things. This map data structure holds optional animated tiles (1 to many frames). Instead of putting a hard limit on frames, I want to be able to redim the frames array to hold how ever many frames a sprite will hold.
It would be a pain to have tons and tons of seperate arrays to keep track of, for map data, when it could be one array on the map data structure.
And why? For example, I have a map data that holds tile information. I hate having artifical (not needed) limits on some things. This map data structure holds optional animated tiles (1 to many frames). Instead of putting a hard limit on frames, I want to be able to redim the frames array to hold how ever many frames a sprite will hold.
It would be a pain to have tons and tons of seperate arrays to keep track of, for map data, when it could be one array on the map data structure.
-
- Enthusiast
- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm
Heh, well, if VB isnt a language, so isnt any of the .NET "languages"
And I totally agree there. Anyways...
I was thinking of linklists, and have pointer to the link lists in the structure but consider the following:
I would need to have an linklist of every single tile on the map. The maps can be as small as 900 tiles to as high as ... well, there is currently no limit. There's no way in the world that I am going to type out over 10,000 linklists... the coding would be a nightmare.

I was thinking of linklists, and have pointer to the link lists in the structure but consider the following:
Code: Select all
Structure MapData
PointerToTileLinkList.ptr
EndStructure
Dim Array.MapData(Level,Width,Height)
why do it this way?
how do you think other programs use variable length information?
using pointers and allocated memory
how do you think other programs use variable length information?
using pointers and allocated memory
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
- tinman
- PureBasic Expert
- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
You could have a pointer in your structure to the array:
Now you can use the pointer in the structure to access the array. I can't remember if PB has a "ReDim" or "Dim Keep" type of operation, but that would allow you to resize it.
Alternatively you could allocate the memory yourself.
And try searching the forums for "pointers to arrays". It's been asked about lots of times. My favourite solution is to use the [0] size arrays that everyone *loves* ;)
Code: Select all
Structure MapData
a.w
EndStructure
Structure foo
*array.MapData
EndStructure
Dim real_array.MapData(10)
DefType.foo var
var\array = @real_array(0)
Alternatively you could allocate the memory yourself.
And try searching the forums for "pointers to arrays". It's been asked about lots of times. My favourite solution is to use the [0] size arrays that everyone *loves* ;)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
(WinXPhSP3 PB5.20b14)
here's a pointy
example:

Code: Select all
Structure mapdata
tilenumber.l
p_tilepointer.l ; can't use *tilepointer here?!?
EndStructure
Dim map.mapdata(10,10)
Structure tiledata
number.l
length.l
EndStructure
*tile0.tiledata = AllocateMemory(1,200*1024,0)
tile_n = 100 ; let's say a hundred tiles
*tile.tiledata = *tile0
For tile = 0 To tile_n
*tile\number = tile
*tile\length = Random(1024) ; just arbitrary, i dunno what you wanna store per tile
*tile = *tile + *tile\length
Next tile
; ok got 10 tiles now with variable length
; now fill in the mapdata, let's say every cell has a random tile
For x = 0 To 9
For y = 0 To 9
map(x,y)\tilenumber = Random(tile_n-1)
Next y
Next x
; i assume maps could change, as well could tiles
; so just before running the real stuff, we'd better map each mapcell to a tile
; for this it's easy if we have a function
Procedure findtile(nr)
Protected *tile.tiledata, n, *p
Global *tile0.tiledata, tile_n.l
;
; no error checking in here :-)
;
n = 0
*p = 0
*tile.tiledata = *tile0
While *p = 0
If *tile\number <> nr
*tile = *tile + *tile\length
Else
*p = *tile
EndIf
Wend
ProcedureReturn *p
EndProcedure
; ok let's map it
For x = 0 To 9
For y = 0 To 9
map(x,y)\p_tilepointer = findtile(map(x,y)\tilenumber)
; map(x,y)\*tilepointer = findtile(map(x,y)\tilenumber) ; fred, please fix this!
Next y
Next x
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
-
- Enthusiast
- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm
If you use tiles that are assembled into a map, have you thought of a matrix (2-dimensional array) containing pointers to the tiles?
Assuming you're programming a game or simulation, you'll probably have a 'visibility' set somewhere. This visibility defines the radius around your current position. So, if you're the middle of a map, that is represented by a matrix, that matrix only need to be as large as to be able accomodate your visibility radius in all directions. You'll always be in the middle of the scene, hence matrix. If you move, you merely have to redefine the tiles (or the pointers to the tiles) of your map matrix.
Assuming you're programming a game or simulation, you'll probably have a 'visibility' set somewhere. This visibility defines the radius around your current position. So, if you're the middle of a map, that is represented by a matrix, that matrix only need to be as large as to be able accomodate your visibility radius in all directions. You'll always be in the middle of the scene, hence matrix. If you move, you merely have to redefine the tiles (or the pointers to the tiles) of your map matrix.
hey shanarra, is the problem solved now?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )