GLBasic forum

Main forum => GLBasic - en => Topic started by: Leginus on 2011-Feb-02

Title: Array and type question
Post by: Leginus on 2011-Feb-02
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


Title: Re: Array and type question
Post by: MrTAToad on 2011-Feb-03
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
Title: Re: Array and type question
Post by: Leginus on 2011-Feb-03
Thanks MrTAToad.  Sorry for late reply, only just got back to pc and bloody iphone 3g today was none existen  :(
Title: Re: Array and type question
Post by: Hark0 on 2011-Feb-03
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
Title: Re: Array and type question
Post by: Slydog on 2011-Feb-03
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.
Title: Re: Array and type question
Post by: Kitty Hello on 2011-Feb-04
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


Title: Re: Array and type question
Post by: Hark0 on 2011-Feb-04
VERY thanks for the explanation.

:good: