Type, Count

Blitz3D Forums/Blitz3D Beginners Area/Type, Count

Almo(Posted 2004) [#1]
Is there a function that gives the current number of a type?

like

Type MyType
  Field WorthlessNumber
End Type

temp1.MyType = new MyType
temp2.MyType = new MyType
temp3.MyType = new MyType
count = SomeFunction(MyType)
; count now equals 3



_PJ_(Posted 2004) [#2]
Don't think so...

If you really need to number the types, you could assign a Field to this with a separate 'counting' variable, however, you will ened up with gaps if Type instances are deleted in the middle etc.


Countr=1

Type MyType
Field InstanceNumber
End Type

For f=1 to 10

MakeType.MyTYpe= New MyType
MakeType\InstanceNumber=Countr
Countr=Countr+1

Next




In that example,

'MakeType\InstanceNumber=Countr' could be replaced with
'MakeType\InstanceNumber=f', but using 'Countr', Other new instances can be created as part of a loop, or separately, with the Countr variable still keeping a track of the number.


Rob Farley(Posted 2004) [#3]
function counttype()
count=0
For c.MyType=Each MyType
count=count+1
Next
return count
end function


There's probably some clever way of passing a particular type into it but I've no idea how.


_PJ_(Posted 2004) [#4]
Actuially, Rob - I think you've just given me an idea (quite obvious when you think about it)...

How about combining my F/N loop with your function, so that the Types are re-numbered each Call. Although the numbers may perhaps change, at least they can then be consecutive, and an accurate total count can be maintained.

------------------------


I just realised, Almo only wanted a total!!!
I was thinking ahead of giving a number (or ranking) to each Instance. Still good brain excercise though!


soja(Posted 2004) [#5]
Here's the simplest solution, programatically:
For t.Type1 = Each Type1 : c% = c + 1 : Next
; c now holds the quantity of Type1s

And here's a solution which doesn't involve counting, or for..next constructs, though it is more obtuse:
Const TYPE1 = 0
Const TYPE2 = 1
Const MAXTYPES = 1

Dim TypeCount%(MAXTYPES)

Type Type1
	Field duh
End Type

Type Type2
	Field duh
End Type

NewType(TYPE1)
t1a.Type1 = Last Type1 ; get the type pointer if you want
t1b.Type1 = Object.Type1(NewType(TYPE1)) ; you can also use Object command
NewType(TYPE2)
NewType(TYPE2)

DebugLog "#Type1: "+TypeCount(TYPE1)+", #Type2: "+TypeCount(TYPE2)
DeleteType(TYPE1, Handle(First Type1))
DebugLog "#Type1: "+TypeCount(TYPE1)+", #Type2: "+TypeCount(TYPE2)
WaitKey

Function NewType%(typeconst)
	If typeconst > MAXTYPES Then Return 0
	TypeCount(typeconst) = 1 + TypeCount(typeconst)
	Select typeconst
		Case TYPE1 : Return Handle(New Type1)
		Case TYPE2 : Return Handle(New Type2)
	End Select
End Function

Function DeleteType(typeconst, t)
	If typeconst > MAXTYPES Then Return 0
	TypeCount(typeconst) = -1 + TypeCount(typeconst)
	Select typeconst
		Case TYPE1 : Delete Object.Type1(t)
		Case TYPE2 : Delete Object.Type2(t)
	End Select	
End Function

This solution would be useful if you had a whole bunch of types, (too many to iterate through each time you wanted a count).

Alternatively, one could forego the Object and Handle stuff and create multiple functions like:
NewType1.Type1(), NewType2.Type2(), DeleteType1(t.type1), etc


Almo(Posted 2004) [#6]
I like soja's obtuse one. But thanks for the info, guys!


Neo Genesis10(Posted 2004) [#7]
Malice: You still wouldnt have an accurate count if you edited the value! If you deleted one of the objects, the last item in the list would still have the same high value!


_PJ_(Posted 2004) [#8]
Yeah, that's why I had the idea and re-posted after Rob Farley's post...


the Types are re-numbered each Call.



So every 'main-loop' the numbering process is repeated! :)