Main Menu

Nested arrays in types- possibe?

Started by tfb, May 10, 2009, 01:04:06 AM

Previous topic - Next topic

tfb

Is this just not possible and what alternate suggestions are there?


type move
name$
...
endtype

type charstats
...
...
moveset(3) as move
endtype

dim chartypes(5) as charstats

chartypes(1).moveset(1).name$="test"

syntax error

u9

I have been fighting with this myself, and i don't know how to get around it (nicely). Finally i implemented it in c++ hehe

kevin


QuoteIs this just not possible and what alternate suggestions are there?

The follow is cut and pasted straight from the manual.. Help->About->Types

 
Quote
--------------------------------------------------------------------------------
What Kind of Fields can a UDT (User Defined Type) Contain ?
--------------------------------------------------------------------------------


At the time of writing (PlayBasic Version 1.59) user defined type fields are limited to the following.

* Integer
* Float
* Strings$
* Static Arrays(Integer,Float,Strings) (1D only)
* Nested User Defined types.


 
 
  Type MyType
     
   ; Implicitly declare fields.
   ; With implicit declares PB assume the type of the fields based upon the
   ; fields post fix (if any)
   ; integer field
     MyIntegerField
     
   ; float field
     MyFloatField#
     
   ; string field
     MyStringField$
     
     
   ; explicit declares (notice the post fixes aren't required)
     MyOtherIntegerField As Integer
     MyOtherFloatField As Float
     MyOtherStringField As String
     
   ; declare Statically sized Array Fields (Only 1D arrays are supported)
     MyIntegerArray(10)
     MyFloatArray#(20)
     MyStringArray$(30)
  EndType
 
 


   Since you have access to Typed Pointers,  You can get around this in a million ways..  Such as storing the nested types with a local array,  using a integer array as pointer table, store them banks. etc   All of which require 'user' level association. 

  ie.


//
Constant MaxVertexCount =100

// Nested Structure
Type tVertex
x#,y#,z#
EndType

// Pointer used to refer to Vertex structures
Dim Vert as tVertex POinter

// Pre compute the size of the embedded array structure in integers.
Constant VertBufferSize =MaxVertexCount*(SizeOf(tVertex)/4)

//declare it
Type tObject
Pos as tVertex
Colour
VertexCount
// Use integer array 'chunk' to house nested vertex structures within the parent
VertBuffer(VertBufferSize)
EndType

// Alloc parents with an array
Dim objects(20) as tObject

// init parents
For lp=1 to GetArrayElements(Objects(),1)

Objects(lp).Pos.x =rnd(GetScreenWidth())
Objects(lp).Pos.y =rnd(GetScreenHeight())
Objects(lp).VertexCount =rnd#(MaxVertexCount-1)
Objects(lp).Colour =rndrgb()

// Init
Vert=int(Objects(lp).VertBuffer)
For ThisVertex=0 to Objects(lp).VertexCount-1
Vert.x=rndrange#(-100,100)
Vert.Y=rndrange#(-100,100)
Vert=Int(Vert)+SizeOf(tVertex)
next ThisVertex
next lp


// process them
For lp=1 to getarrayelements(Objects(),1)
Xpos=Objects(lp).Pos.x
Ypos=Objects(lp).Pos.y
ink Objects(lp).Colour
circle Xpos,Ypos,32,false
Vert=int(Objects(lp).VertBuffer)
For ThisVertex=0 to Objects(lp).VertexCount-1
line Xpos,Ypos,Xpos+Vert.x,Ypos+Vert.Y
Vert=int(Vert)+SizeOf(tVertex)
next
ink rgb(255,255,255)
centertext Xpos,Ypos-5,Objects(lp).VertexCount
next lp


// Kill them
For lp=1 to getarrayelements(Objects(),1)
// perform a sanity check
if  Objects(lp)

// Warning - If your nested structure contains strings, then you'll have to release them, before
// you release the object. Otherwise your leaking strings.
Objects(lp)=Null
endif
next lp


Sync
waitkey
   






kevin


  Using the Integer Array as Pointer Table





//
Constant MaxVertexCount =10

// Nested Structure
Type tVertex
Name$
x#,y#,z#
EndType

// Pointer used to 'read/write' to Vertex structures
Dim Vert as tVertex POinter

//declare object thing
Type tObject
Pos as tVertex
Colour
VertexCount
// Use integer array as pointer buffer for tVertex structures
VertBuffer(MaxVertexCount)
EndType





// Alloc parents with an array
Dim objects(20) as tObject

// init parents
For lp=1 to GetArrayElements(Objects(),1)

Objects(lp).Pos.x =rnd(GetScreenWidth())
Objects(lp).Pos.y =rnd(GetScreenHeight())
Objects(lp).VertexCount =rnd#(MaxVertexCount-1)
Objects(lp).Colour =rndrgb()


For ThisVertex=0 to Objects(lp).VertexCount-1
// Alloc a new vertex structure
Vert= New tVertex
// Put this pointer into our array
Objects(lp).VertBuffer(ThisVertex) = Int(Vert)

// init the structure
Vert.x=rndrange#(-100,100)
Vert.Y=rndrange#(-100,100)
Vert.name$="Obj:"+Str$(lp)+"VName:"+Str$(ThisVertex)

next ThisVertex
next lp



// process them
For lp=1 to getarrayelements(Objects(),1)
Xpos=Objects(lp).Pos.x
Ypos=Objects(lp).Pos.y
ink Objects(lp).Colour
circle Xpos,Ypos,32,false
For ThisVertex=0 to Objects(lp).VertexCount-1
// Get the Pointer to this object
Vert=Objects(lp).VertBuffer(ThisVertex)
// draw it
x=Xpos+Vert.x
y=Ypos+Vert.Y
line Xpos,Ypos,X,Y
centertext x,y,Vert.name$
next
ink rgb(255,255,255)
centertext Xpos,Ypos-5,Objects(lp).VertexCount
next lp


// Kill them
For lp=1 to getarrayelements(Objects(),1)
// perform a sanity check
if  Objects(lp)

// Free the associated buffers
For ThisVertex=0 to Objects(lp).VertexCount
// get the pointer to this object
Vert=Objects(lp).VertBuffer(ThisVertex)
// Free this allocated structure
Free Vert
next

// Free this parent
Objects(lp)=Null

endif
next lp

Sync
waitkey