Yet another TYPES question

Previous topic - Next topic

fuzzy70

Me & TYPES are getting on like fire & water at the moment. It is bound to be something obvious that I am missing but the "penny is not dropping" so to speak. I think most of it is due to using BlitzBasic/Blitz3d for so long (since my Amiga days lol) that adjusting to the different way GLB uses TYPES is my problem.

I have used GLB TYPES as arrays in a few tests no problem, however with them I knew the size of the arrays in advance so did not come across any problems. I presume you have to use arrays if you want more than one of the same type, again am used to the NEW command in Blitz.

If someone could convert the following Blitz code into GLB code I will be able to make the connection & know in future. I have quite a few code snippits in Blitz that I have wrote over the years & would like to use/convert some of them into GLB .

Blitz code
Code (glbasic) Select

Type Ttest
Field x
Field y
Field layer
End Type

Function addtemp(x,y,layer)
additem.Ttest = New Ttest

additem\x = x
additem\y = y
additem\layer = layer
End Function

Function moveitems(dx,dy)
For additem.Ttest = Each Ttest
additem\x = additem\x+dx
additem\y = additem\y+dy
Next
End Function


My attempt at converting the above to GLB

Code (glbasic) Select

TYPE Ttest
x%
y%
layer%
ENDTYPE

GLOBAL test%[] AS Ttest

//DIM test%[1]

GLOBAL additem AS Ttest


FUNCTION additems: x%,y%,layer%
additem.x = x
additem.y = y
additem.layer = layer
DIMPUSH test[],additem
ENDFUNCTION

FUNCTION moveitems: dx%,dy%
FOREACH loop IN test[]
test%[loop].x%=test%[loop].x%+dx%
test%[loop].y%=test%[loop].y%+dy%
NEXT
ENDFUNCTION


Which fails at compile time with a stream of errors
Code (glbasic) Select

C:\Users\LEEFUR~1\AppData\Local\Temp\glbasic\gpc_temp0.cpp: In function `DGInt __GLBASIC__::moveitems(DGNat, DGNat)':
C:\Users\LEEFUR~1\AppData\Local\Temp\glbasic\gpc_temp0.cpp:90: error: no match for call to `(__GLBASIC__::DGArray<__GLBASIC__::Ttest>) (__GLBASIC__::Ttest&)'
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:595: note: candidates are: T& __GLBASIC__::DGArray<T>::operator()(int) [with T = __GLBASIC__::Ttest]
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:640: note:                 __GLBASIC__::DGArray<T>& __GLBASIC__::DGArray<T>::operator()() [with T = __GLBASIC__::Ttest]
C:\Users\LEEFUR~1\AppData\Local\Temp\glbasic\gpc_temp0.cpp:90: error: no match for call to `(__GLBASIC__::DGArray<__GLBASIC__::Ttest>) (__GLBASIC__::Ttest&)'
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:595: note: candidates are: T& __GLBASIC__::DGArray<T>::operator()(int) [with T = __GLBASIC__::Ttest]
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:640: note:                 __GLBASIC__::DGArray<T>& __GLBASIC__::DGArray<T>::operator()() [with T = __GLBASIC__::Ttest]
C:\Users\LEEFUR~1\AppData\Local\Temp\glbasic\gpc_temp0.cpp:94: error: no match for call to `(__GLBASIC__::DGArray<__GLBASIC__::Ttest>) (__GLBASIC__::Ttest&)'
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:595: note: candidates are: T& __GLBASIC__::DGArray<T>::operator()(int) [with T = __GLBASIC__::Ttest]
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:640: note:                 __GLBASIC__::DGArray<T>& __GLBASIC__::DGArray<T>::operator()() [with T = __GLBASIC__::Ttest]
C:\Users\LEEFUR~1\AppData\Local\Temp\glbasic\gpc_temp0.cpp:94: error: no match for call to `(__GLBASIC__::DGArray<__GLBASIC__::Ttest>) (__GLBASIC__::Ttest&)'
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:595: note: candidates are: T& __GLBASIC__::DGArray<T>::operator()(int) [with T = __GLBASIC__::Ttest]
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:640: note:                 __GLBASIC__::DGArray<T>& __GLBASIC__::DGArray<T>::operator()() [with T = __GLBASIC__::Ttest]
*** FATAL ERROR - Please post this output in the forum
_______________________________________
*** Finished ***
Elapsed: 1.1 sec. Time: 00:21
Build: 0 succeeded.
*** 1 FAILED ***


not a pretty sight  :D, tried it with the DIM statement & still fails.

Cheers

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

MrTAToad

Try this :

Code (glbasic) Select
TYPE Ttest
x%
y%
layer%
ENDTYPE

GLOBAL test[] AS Ttest

//DIM test%[1]

GLOBAL additem AS Ttest


FUNCTION additems: x%,y%,layer%
LOCAL additem AS Ttest

additem.x = x
additem.y = y
additem.layer = layer
DIMPUSH test[],additem
ENDFUNCTION

FUNCTION moveitems: dx%,dy%
FOR loop%=0 TO BOUNDS(test[],0)-1
test[loop].x%=test[loop].x%+dx%
test[loop].y%=test[loop].y%+dy%
NEXT
ENDFUNCTION


You were trying to use an non-integer variable to index an array of TYPE's

Ruidesco

You can REDIM an array, it will keep the data previously introduced and append new items (or delete them, if you REDIM to a smaller size).
You can also DIMPUSH an item into an array, or DIMDEL an existing item of an array.

fuzzy70

Thank you both, you have saved my sanity (& my pc from being kicked in frustration  :D)

So was I right in saying that you have to use arrays to store more than 1 item in a TYPE, compared to the Blitz NEW command which just adds to the existing ones. If so thats not a problem & easy to remember for future.

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Moebius

When using FOREACH, "loop" will be a refence to an element of the array -  i.e. you use "loop.x" not "array[loop].x" in a "FOREACH loop IN array[]" loop.
Once you get the hang of things, the way blitz handled types almost seems restrictive  :good:
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

fuzzy70

Blitz types where pretty simple & lacked the extras that GLB provides like functions, but like I said using them for years & being used to the NEW command to add to them & using "\" to access them rather than "." etc will just take getting used to.

I have seen some GLB code with self. added to the start but no idea what that does & what others are available as no mention of them in the online help. Have also seen code with "?" & "@" in front of function names yet same again cannot find any docs on what they are for either  :O

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

MrTAToad

@ in front of a function stops it appearing in the function list.
? is used with pre-processor commands...

fuzzy70

Thank you MrTAToad  :D. Out of interest are these things covered in one of your books?

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)