Array and type question

Previous topic - Next topic

Leginus

At the moment I have two type arrays with differnt fields (x,y,)etc 
Code (glbasic) Select
Buildings[] and
Code (glbasic) Select
Produce[]
i would like to call in Buildings.x at array index 1, into a function within my Produce type.

is there a quick way to do this e.g   something like
Code (glbasic) Select
Buildings[0].x  or do I have to do the following OR set a global variable within my Buildings type array on initialisation

Code (glbasic) Select

Fuction CalcRoute()
   FOREACH item IN Buildings[]
      if item.BuildingType = 4
          x=item.x
      endif
   NEXT
ENDFUNCTION


I am currently doing the above for each, but thought I would just check if it was possible to do something different
Any help would be appreciated (even if it is pointing out stupidity)  :P



MrTAToad

I presume you are talking about arrays of extended type, and calling a function in the array ?

Something like :

Code (glbasic) Select
TYPE TTest
    FUNCTION example%:
    ENDFUNCTION
ENDTYPE

LOCAL array[] as TTest

DIM array[6]

DEBUG array[0].example()


works okay

Leginus

Thanks MrTAToad.  Sorry for late reply, only just got back to pc and bloody iphone 3g today was none existen  :(

Hark0

About arrays... little question:


Code (glbasic) Select
FUNCTION Reset_System: x

  REDIM data[0] // Clear data, prepare array  <- It's really need?
  REDIM data[x]

ENDFUNCTION



Imagine this function for initialize an array. I use this function some times. I change the value of x on every call.
I need first REDIM? Not valid only the second REDIM?


TIA, Hark0
http://litiopixel.blogspot.com
litiopixel.blogspot.com - Desarrollo videojuegos Indie · Pixel-Art · Retroinformática · Electrónica Development Indie Videogames · Pixel-Art · Retrocomputing · Electronic

Slydog

#4
If I understand what you are asking Hark0, then both the REDIM commands can just be replaced with a single DIM command, such as:
Code (glbasic) Select
FUNCTION Reset_System: x

  DIM data[x]

ENDFUNCTION


That will clear any data that was already in the array, plus resize it to a new dimension, which is what the two REDIMs were doing.

[Edit]
If you removed the first REDIM in your code, the array would still be resized, but the previous data would not be cleared.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Kitty Hello

Some more internals:

DIM will try to use the currently allocated memory. If it's enough to fit what you need, it will just set all entries to emtpy. If it does not fit, the old memory is deleted, and new memory is allocated. When allocating, GLBasic tries to allocate a few entries more, depending on the memory size of one single element, to avoid reallocation later.
DIM arr[0] will totally free the internal memory.

REDIM does internally call DIM for an temporary array and copy the old data in the new array and then delete the old array and use the new one. If, however, the REDIM is smaller that the allocated memory of the array, only the internal size variables are adjusted and the new elements are overwritten with empty elements.
REDIM[0] will just set the internal size variable to 0. The memory is still allocated in the background.

DIMPUSH is the quickest way to add a new element to an array. It tries to avoid any allocations or unnecessary copies.

DIMDEL just moves (copy) the elements below the deleted one up in the array. The internal size variable is decresed. NO MEMORY IS FREED. Thus, dimdel+dimpush work very fast once the array has a good size to fit new elements.

And this is the reason, why I sometimes write:
Code (glbasic) Select

DIM array[1000] // allocate a buffe of 1000 elements
REDIM array[0] // set the size of the array to 0 - all memory is still allocated

... DIMPUSH array[], xxx
... DIMDEL array[], id



Hark0

VERY thanks for the explanation.

:good:
http://litiopixel.blogspot.com
litiopixel.blogspot.com - Desarrollo videojuegos Indie · Pixel-Art · Retroinformática · Electrónica Development Indie Videogames · Pixel-Art · Retrocomputing · Electronic