Duh! Types

Blitz3D Forums/Blitz3D Beginners Area/Duh! Types

_PJ_(Posted 2004) [#1]
I need to know the number of custom types I have created (I want to limit it to 50, and stop so it will only create new if <50)

Is there a quick way of doing this without:

count=0
For counttypes.mytype=each mytype
count=count+1
next

????

As It seems a bit long-winded for such a simple thing!


Shambler(Posted 2004) [#2]
Instead of counting the number of types like that just increment a variable when you create a type.

count=0

if count<50
newtype.mytype=new mytype
count=count+1


GfK(Posted 2004) [#3]
Is there a quick way of doing this without:

count=0
For counttypes.mytype=each mytype
count=count+1
next

That *is* a quick way. Iteration through type collections is very fast.

The only other way would be to use a variable to keep count when a new item is added or removed.


Koriolis(Posted 2004) [#4]
Generally using a dedicated function to create your type is better, as you can create and initialize the fields in one shot. Just make sure you always sue this function and don't create your objects directly, and then you can simply add code in it to count your objects:
Type MyType
    Field MyField1%
    Field MyField2#
End Type

Global MyTypeCount% = 0

Function CreateMyType.MyType(f1%, f2#)
   tmp.MyType = New MyType
   tmp\MyField1 = f1
   tmp\MyField2 = f2
   MyTypeCount = MyTypeCount + 1
End Function

Function DeleteType(obj.MyType)
    Delete obj
    MyTypeCount = MyTypeCount - 1
End Function
That's the best you can do.


_PJ_(Posted 2004) [#5]
Hmm I guess my original method is actually best for what I want...

Sorry shoulda made it a little clearer. I am using the types to create a kind of 'starfield' so I will create xxx stars initially, but then delete them as they get out of visible range, however I need to keep creating them, but obviously not too much or the screen will get saturated!

I had a look at this, but had a bit of trouble implementing it into my code - although it's the kind of thing Im after!


Shambler(Posted 2004) [#6]
Can't you rather than delete and recreate just move those stars that are out of range into a new position?


_PJ_(Posted 2004) [#7]
Hahah!!!!! Hence the Duh! in the title....

Thanks Shambler - maybe I should leave the coding alone for today :)


EOF(Posted 2004) [#8]
Here's an old starfield program I have which uses types:
; Starfield - adapted from Amiga Blitz II code
; Syntax Error

Const width=640 , height=480
Const cx=width/2    ; screen centre xpos
Const cy=height/2   ; screen centre ypos
Const numstars=575  ; number of stars to render
Const spread=34     ; spread of stars (smaller=narrow field)
Const speed#=1.8    ; speed of stars movement

Graphics width,height
SetBuffer BackBuffer()

Type startype
 Field x#,y#,z#,sx#,sy#
End Type
Global star.startype

For d=1 To numstars
  star.startype=New startype
Next

; main loop
While Not KeyDown(1)
 Cls
 DrawStars
 Flip
Wend
End

; update & plot stars
Function DrawStars()
 For star.startype=Each startype
  If star\z<speed SetStarPosition
  star\z=star\z-speed
  t=star\x*spread : star\sx=t/(star\z)+cx
  t=star\y*spread : star\sy=t/(4+star\z)+cy
  If star\sx<0 Or star\sx>width SetStarPosition
  If star\sy<0 Or star\sy>height SetStarPosition
  Color 255-star\z,255-star\z,255-star\z
  Plot star\sx#,star\sy
 Next
End Function

; place star at new position
Function SetStarPosition()
	star\z = Rnd(200,255)
	star\x = Rnd(2000)-1000
	star\y = Rnd(2000)-1000
End Function



Clyde(Posted 2004) [#9]
Pretty smart and cool Syntax Error!
Nice one Buddy! :)