Author Topic: Arrays in Types?  (Read 2359 times)

Offline matty47

  • Mr. Drawsprite
  • **
  • Posts: 71
    • View Profile
    • Matt and Marg's Homesite
Arrays in Types?
« on: 2011-Nov-13 »
Are they allowed.
For instance I want to do something like this
Code: (glbasic) [Select]
CONSTANT MaxDims=3

TYPE Point
     Loc[MaxDims]
ENDTYPE

.....................................
//then later do

LOCAL P1 AS POINT
.......................................
FOR i = 0 to MaxDims
      IF P1.Loc[i] ................
Now I know this code won't work but the compiler issues an error for the Loc[MaxDims] line
Any answers greatly appreciated
Thanks
Matthew

Offline Qedo

  • Dr. Type
  • ****
  • Posts: 382
  • to program what I have do how should programming?
    • View Profile
Re: Arrays in Types?
« Reply #1 on: 2011-Nov-13 »
Try:

CONSTANT MaxDims=3
TYPE Point
     Loc[]
ENDTYPE
LOCAL P1 AS Point
REDIM P1.Loc[MaxDims]
FOR i = 0 TO MaxDims-1
      IF P1.Loc=0 THEN P1.Loc=10
NEXT

Ciao

Offline matty47

  • Mr. Drawsprite
  • **
  • Posts: 71
    • View Profile
    • Matt and Marg's Homesite
Re: Arrays in Types?
« Reply #2 on: 2011-Nov-13 »
Tried that - didn't seem to work.
I tried changing my attack and as I only want to deal with 3D points
Code: (glbasic) [Select]
TYPE TPoint
Loc[]
FUNCTION CreatePoint:x,y,z
self.Loc[0] = x
self.Loc[1] = y
self.Loc[2] = z
ENDFUNCTION
ENDTYPE

LOCAL P1 AS TPoint
But the compiler errors the "LOCAL P1 AS TPoint" line with (25) error : command not inside function or sub

MrTAToad

  • Guest
Re: Arrays in Types?
« Reply #3 on: 2011-Nov-13 »
Odd - that example compiles fine here.

You do need to define the size of the array :

Code: (glbasic) [Select]
TYPE TPoint
Loc[]
FUNCTION CreatePoint:x,y,z
DIM self.Loc[3]
self.Loc[0] = x
self.Loc[1] = y
self.Loc[2] = z
ENDFUNCTION
ENDTYPE

LOCAL P1 AS TPoint

Offline Ruidesco

  • Mr. Polyvector
  • ***
  • Posts: 236
    • View Profile
Re: Arrays in Types?
« Reply #4 on: 2011-Nov-13 »
Yes, you can use any kind of variable inside a type, even variables of other types as well.
A different thing is initializing  those variables, since there is no default constructor/destructor for types because they are not objects; what I do to overcome that issue is defining an "init" function for my types which I call immediately after defining them, such as:
Code: (glbasic) [Select]
LOCAL abc as MyType; abc.init()In "init" is where I DIM arrays, for example.

Offline matty47

  • Mr. Drawsprite
  • **
  • Posts: 71
    • View Profile
    • Matt and Marg's Homesite
Re: Arrays in Types?
« Reply #5 on: 2011-Nov-14 »
I think I got to the bottom of my troubles. I had the following code in a file called Utils.gbas
Code: (glbasic) [Select]
CONSTANT MaxDims=3

TYPE TPoint
Loc[]
FUNCTION init:x,y,z
DIM self.Loc[MaxDims]
self.Loc[0]=x
self.Loc[1]=y
self.Loc[2]=z
ENDFUNCTION
ENDTYPE


LOCAL P1 AS TPoint;P1.init(1,2,3)

with an empty main file called Test.gbas (Project Test). The compiler kept throwing an error on the creation of P1 (LOCAL P1.....line).
If I move this line to the main file I get no error. Therefore I assume that I can't declare variable of a type in an attached (referenced file) ??
Does that make sense?
Thanks for the help. I hope I can proceed now.
Matthew                     

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10851
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Arrays in Types?
« Reply #6 on: 2011-Nov-14 »
The last line is an executed line - it must be inside a function. When moved to main.gbas, this is the "_MainGame" function that starts with the 1st line of the first file until the first FUNCTION/SUB is reached (or the 2nd file).