Author Topic: Type bug  (Read 4485 times)

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4175
  • On the shoulders of giants.
    • View Profile
    • My Apps
Type bug
« on: 2008-Jan-03 »
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.

Code: (glbasic) [Select]
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?
I came. I saw. I played.

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5116
  • O Rly?
    • View Profile
Type bug
« Reply #1 on: 2008-Jan-03 »
no bug! ;)

Code: (glbasic) [Select]
// 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
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4175
  • On the shoulders of giants.
    • View Profile
    • My Apps
Type bug
« Reply #2 on: 2008-Jan-03 »
That's sorted it. Cheers :)
I came. I saw. I played.

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5116
  • O Rly?
    • View Profile
Type bug
« Reply #3 on: 2008-Jan-03 »
;) thats all :D
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10839
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Type bug
« Reply #4 on: 2008-Jan-03 »
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.

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4175
  • On the shoulders of giants.
    • View Profile
    • My Apps
Type bug
« Reply #5 on: 2008-Jan-03 »
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 :)
I came. I saw. I played.