I've been working with Types recently and they've been working fine for the most part, but I've discovered what I believe to be a bug.
The code below clearly demonstrates the problem - I create 10 Types, positioning them all at 5,5. In theory every Type should iterate at exactly the same position. However Type[0] ALWAYS appears at position 0,0 - the other Types work fine,
No matter what other fields are in the Type, Type[0] always appears at 0,0 - its other fields are active and work fine though.
SETSCREEN 320,240,1
// Test Type
TYPE test_type
x
y
num
ENDTYPE
GLOBAL tt[] AS test_type
DIM tt[100]
FOR n=0 TO 10
create_test(n)
NEXT
main()
END
// Main
FUNCTION main:
WHILE TRUE
DRAWRECT 0,0,320,240,RGB(255,255,255)
render_test()
SHOWSCREEN
WEND
ENDFUNCTION
// Create test Type
FUNCTION create_test: n
tt[n].num=n
tt[n].x=5
tt[n].y=5
ENDFUNCTION
// Render Test Types
FUNCTION render_test:
FOREACH item IN tt[]
PRINT item.num,item.x,item.y
NEXT
ENDFUNCTION
Is this a bug or a problem with my coding? until now I've been working around it and only displaying Types with a value over zero. But this can't be right. Can it?
no bug! ;)
// Create Type #######################
TYPE test_type
num
X
Y
ENDTYPE
GLOBAL tt[] AS test_type
//####################################
FOR x = 0 TO 39 // 40 types
create_test(RND(640),RND(480),x) // Set a new Type
NEXT
WHILE TRUE
FOREACH t IN tt[] // Check all Types in tt[]
PRINT t.num,t.X,t.Y // Print something
NEXT
SHOWSCREEN
WEND
END
// --------------------------------------------
// - Function Create Test_Type -
// --------------------------------------------
FUNCTION create_test: X,Y,num
LOCAL t AS test_type // a type variable
t.X = X // give X to Type field X
t.Y = Y // give Y to Type field Y
t.num = num // give Name$ to Type field Name$
DIMPUSH tt[],t // setup the new type
ENDFUNCTION
That's sorted it. Cheers :)
;) thats all :D
Doh! That took me a while to get the problem.
You're pre-dimming tt[100], then you store 0 to 10 as 5,5.
However, the others (11 - 99) are still predimmed with 0,0.
Got it?
DIMPUSH is the best for types. Then foreach works correct.
I'm more used to dealing with Types in PlayBasic (which appear to be nigh-on identical), where DIM was required for multiple Types.
Never needed DimPush in the past, so wasn't aware of it.
I understand it now though. Thanks to both of you :)