View Full Version : Need help with convert Blitz3D code to thinBasic
Hello everbody!
I'm novice in programming. A couple of months ago I started learning Blitz3D, but soon realized that he uses the old DirectX 7. And recently I found in the Internet a great BASIC compiler - thinBasic. It's realy powerful and useful basic. I have some questions about programming in thinBasic.
This is my Simple code in Blitz3D:
Type Monstr
Field number
End Type
For i=0 to 10
enemy.Monstr = New Monstr
enemy\number = i
Next
; This three rows is interesting me. I need to convert this rows into thinBasic code.
For enemy.Monstr = Each Monstr ; whether in the thinbasic this comand (like EACH)?
enemy\number = enemy\number +1
Next
End
P.S. Sorry my BAD English!
Thanks in advance!
Michael Hartlef
10-06-2010, 15:29
Hi,
welcome to the forum. FOR EACH is not supported to my knowledge. You have to use a linked list and then read through it with the regular looping commands.
Cheers
Michael
ErosOlmi
10-06-2010, 18:18
Hi Bagamut and welcome to thinBasic.
Few clarification about thinBasic
thinBasic is not a compiler but an interpreter, it means it does not produce any machine code but just read your source code and interpret it on the fly.
thinBasic is not an object oriented language so whenever there is some code using objects we need to convert it into something different, usually types and functions.
EACH is a construct of OOP. In thinBasic we still do not have it so you need to use standard loops.
Your code can be something like:
'---Define your new type
Type Monstr
Number AS LONG
'... other type elements (properties in OOP)
End Type
'---Define monsters
dim Enemy(10) as Monster
'---Fill in some data
For i=1 to 10
enemy(i).Number = i
Next
But depending on the situation you can allocate monster elements dynamically.
Ciao
Eros
Hi,
welcome to the forum. FOR EACH is not supported to my knowledge. You have to use a linked list and then read through it with the regular looping commands.
Cheers
Michael
Could you write an example plz!
there is a detailed example about using linked list in the thinbasic examples:
C:\thinBasic\SampleScripts\LL\Test_LL.tbasic
and about the linked list concept:
http://en.wikipedia.org/wiki/Linked_list
ErosOlmi
16-06-2010, 08:03
I do not think loops involving EACH keyword has to be handled using lists.
Classical FOR/NEXT looping through elements is a structure (whatever complex it can be) is enough.
The point is to understand the complete structure of enemy and/or monster, what is the first and what is the second and how they are organized.
Than use FOR/NEXT at whatever needed level of the structure to loop each of the elements of an array.
If dynamic creation of UDT is needed so there is no knowlegde at programming time of how many elements will be allocated, than there will be many different techniques that can be used to store data and loop on them.
Michael Hartlef
16-06-2010, 10:13
Hi,
welcome to the forum. FOR EACH is not supported to my knowledge. You have to use a linked list and then read through it with the regular looping commands.
Cheers
Michael
Could you write an example plz!
Hi,
I will try to provide you with something hopefully tonight when I am back from work and the kid is in bed. I think about a more dynamic approach with memory allocation. That should be better than a linked list.
Btw. what is your native language? I have no problems understanding of what you write.
Cheers
Michael
Hi,
I will try to provide you with something hopefully tonight when I am back from work and the kid is in bed. I think about a more dynamic approach with memory allocation. That should be better than a linked list.
Thanks in advance! Does the thinBasic support dynamic arrays of UDTs?
Btw. what is your native language? I have no problems understanding of what you write.
Cheers
Michael
Russian is my native language.
ErosOlmi
16-06-2010, 15:15
Does the thinBasic support dynamic arrays of UDTs?
Dynamic arrays of UDT yes. Arrays of UDT are just standard arrays.
Dynamic arrays inside UDT no but it is something I will work for future versions because I would definitely like to have it.
A possible work around ven on this necessity is possible using dynamic strings inside an UDT and over-impose logical array structures to that strings. You need latest beta version 1.8.1 (http://community.thinbasic.com/index.php?topic=3452.0)
An example:
#MINVERSION 1.8.1
'---Define an UDT having a dynamic string inside
Type tMyUDT
MyLong As Long
MyArrayOnStringBuff As String
Whatever As Ext
End Type
'---Some variables
Dim nItems As Long
Dim MyUDT As tMyUDT
'---How many items you want in your logical array
nItems = 10
'---Dimension your string for exactly the size of the needed data mult number of items
MyUDT.MyArrayOnStringBuff = Repeat$(SizeOf(Long) * nItems, $NUL)
'---Define a logical array that over-impose at the same memory location of
'---dynamic string inside your UDT
Dim MyArrayOfLongs(nItems) As Long At StrPtr(MyUDT.MyArrayOnStringBuff)
'---Fill your logical array. In reality you are filling dynamic memory inside UDT
MyArrayOfLongs(1) = 1,2,3,4,5,6,7,8,9,10
'---output data
MsgBox 0, "OK, data is:" & $CRLF & _
Join$(MyArrayOfLongs, $CRLF)
'---Now add few more items
Dim lMoreItems As Long = 5
nItems = nItems + lMoreItems
'---Increase size of the dynamic string with more space for new items
'[!] Note the += assignment used to increment current buffer
MyUDT.MyArrayOnStringBuff += Repeat$(SizeOf(Long) * lMoreItems, $NUL)
'---Redim your logical array
ReDim MyArrayOfLongs(nItems) As Long At StrPtr(MyUDT.MyArrayOnStringBuff)
'---Fill new items
MyArrayOfLongs(11) = 11,12,13,14,15,16
'---output data
MsgBox 0, "Data after inserting new ietms:" & $CRLF & _
Join$(MyArrayOfLongs, $CRLF)
Petr Schreiber
16-06-2010, 17:20
I think it is possible to create dynamic arrays of UDT, just like with any other data type:
Uses "Console"
Type tPoint
x As Single
y As Single
End Type
' -- Create original array
Dim Points(2) As tPoint
Points(1).x = 1
Points(1).y = 2
Points(2).x = 3
Points(2).y = 4
' ### PRINT IT OUT ###
Points_ToConsole(points)
' -- Change size, but preserve elements content
ReDim Preserve Points(3)
Points(3).x = 5
Points(3).y = 6
' ### PRINT IT OUT ###
Points_ToConsole(points)
' -- Change size, but erase the contents
ReDim Points(4)
' ### PRINT IT OUT ###
Points_ToConsole(points)
WaitKey
Sub Points_ToConsole( points() As tPoint)
Local i As Long
Local numPoints As Long = UBound(points)
PrintL "Total items: "+Format$(numPoints)
For i = 1 To numPoints
PrintL "item #"+Format$(i), points(i).x, points(i).y
Next
PrintL
End Sub
Petr
Michael Hartlef
18-06-2010, 16:55
Thanks Petr,
perfect example and imho one of the easiest ways to do it.
Michael