Types again

Blitz3D Forums/Blitz3D Beginners Area/Types again

JoeRetro(Posted 2003) [#1]
Ok...Here I have a simple C struct

typedef struct Stars_struct
{
float x, y, z;
float r, g, b;
} STARS;

Now I can create as many objects of this struct as I want (2 of them here):

STAR Object1
STAR Object2

Now, how can I create different objects using the SAME type in Blitz?

Thanks in advance!


NTense(Posted 2003) [#2]
Type Stars
   Field x#, y#, z#
   Field r, g, b
End Type

Global MyStars.Stars
SeedRnd Millisecs()

;  Here I can make 15 different stars each as it's own
;  individual object.  (types are just a linked list)

For count = 1 to 15
   MyStars.Stars = New Stars

   ; Give'm random coordinates
   MyStars\x# = Rnd(-100,100)
   MyStars\y# = Rnd(-100,100)
   MyStars\z# = Rnd(-100,100)

   ; Give'm random color
   MyStars\r = Rand(0,255)
   MyStars\g = Rand(0,255)
   MyStars\b = Rand(0,255)
Next

counter = 1

For MyStars.Stars = Each Stars
   Print "Star #" + counter
   Print "x: " + MyStars\x# + " y: " + MyStars\y# + " z: " + MyStars\z#

   ;  Same for print for color... here 

   counter = counter + 1
Next

waitkey()



This is a small example. Hope it helps. They're each separate objects tied together in a link list so you can use For/Each, First, Last, and Before commands to navigate through the list of objects (and Delete to get rid of them).


Curtastic(Posted 2003) [#3]
Type Stars
   Field x#, y#, z#
   Field r, g, b
End Type

Object1.stars=new stars
Object2.stars=new stars



JoeRetro(Posted 2003) [#4]
Coorae,

I suspected it to work this way, but it does not. Object1 is essentially Object2. Assigning fields of Object1 can be retrieved from Object2 without intializing Object2.

So, basically think of Types as linked lists rather than C structs (which would be more powerful I believe).

Hey Mark, how bout it? We need a C struct or a (c++ like) class would work :)


soja(Posted 2003) [#5]
Joe,

You're mistaken. Coorae's Blitz code does just what your C code does.

Custom types are like C structs, and the variables that reference them are effectively pointers. They also happen to be stored in a linked list by default. So that's one up on C. =)


JoeRetro(Posted 2003) [#6]
soja,

Apparently so it seems. Then how would one translate the following from C to blitz:

typedef struct Stars_struct
{
float x, y, z;
float r, g, b;
} STARS;

STAR Object1[20]
STAR Object2[20]

Now I have 20 instances of EACH star object.


JoeRetro(Posted 2003) [#7]
think I just figured it out.... :)

Type Stars
Field x#, y#, z#
Field r, g, b
End Type

Dim mystars1.stars(5)
Dim mystars2.stars(5)

For N = 0 To 5
mystars1(N) = New Stars
mystars2(N) = New Stars
Next

For j=0 To 5
mystars1(j)\r = Rand(255)
mystars2(j)\r = Rand(255)
Next

For j=0 To 5
DebugLog("mystars1(j)\r=" + mystars1(j)\r)
DebugLog("mystars2(j)\r=" + mystars2(j)\r)
Next