Types Problem

Blitz3D Forums/Blitz3D Programming/Types Problem

SSS(Posted 2003) [#1]
hi everyone, i was trying to use types in my level editor and while i am pritty familiar with them i was wondering if there was any way to do something like this,

Type a
field val,next.a
end type

type b
field val
field list.a
end type

b.b = new b

a.a = b\list

for i = 0 to 10
a\next = new a
a = a\next
next

print b\list\next\val


this is how i would do a linked list in C++ using pointers however in blitz it says that print b\list\next\val is not a valid object so i wonder, is there any way of doing this...

thanks


Ziltch(Posted 2003) [#2]
Blitz does things a bit differently than C++. Lists of a Type can be gone thru in order using the BEFORE and AFTER commands.

If you inserted data or reorder it and wanted to go thru it in the original creation order, then I guess using a chaining method of storing the next record would still be handy.

Here is some code you may find useful.

Type Record
Field val
Field Rec_Num
Field Next_Record
End Type

;-- Initialize
For i = 0 To 10

  NewRec.Record = New Record
  NewRec\Rec_Num = Handle NewRec
  Newrec\val = 1000+i ; test info

  If i > 0 Then
    NextRec.Record = Before Newrec
    NextRec\Next_Record = Handle NewRec
  End If

Next


;-- Look At Each record
For Rec.Record = Each Record
  Print "Recnum = "+Rec\Rec_num + " Val =" + Rec\val + " Next " + Rec\next_Record
Next


;-- Look at Random
For i = 1 To 5

  recnum=Rand(1,11)
  Rec.record = Object.Record(recnum)
   Print "Look for Rec = "+Recnum + ": Recnum = "+rec\Rec_num + " Val =" + Rec\val + " Next " + Rec\next_Record

Next

Delay 10000


How you format the list is really got to do with what you are needing it for. How are you going to need to access the data?


SSS(Posted 2003) [#3]
oh, its just to read weapons from a unit file where i need to enumerate all of the weapons and then read through them with a list and push them onto the stack of weapons for a particular unit, i would show you the whole code but its really messy... i still can if you want


Ziltch(Posted 2003) [#4]
If that is the case then you don't need the next record bits of my example. The rest of the example code you should be able to modify a bit to do what you need.

The main thing you need to decide is if you need to have random access to the data in the list. The example code shows how to get the data from the list in two different ways.

Hope this helps


SSS(Posted 2003) [#5]
here is my source... it is slightly more complex than this, because it is not just one type... but here is the source... i really wanted to do it the way that i placed as my example because it is a far more elegant solution... anyway here it is


Type WeaponList
	Field WeaponName$
	Field WeaponType$
	Field Damage
	Field Speed
	Field AirDamage
	Field NextWeapon.WeaponList
End Type

Type UnitData
	Field Speed
	Field Armor$
	Field UnitType$
	Field Health
	Field Transport
	Field TransportSpace
	Field Space
	Field Detector
End Type

Type Unit
	Field Name$
	Field NumWeapons
	Field ImgSm,ImgLg
	Field Weapons.WeaponList
	Field UnitData.UnitData
End Type

Function LoadUnit.Unit(FileName$)
	state = 0
	file = False
	this.Unit = Null
	in = ReadFile(FileName$)
 	ln$ = ""
	While Not ln$ = "#END"
		ln$ = EatWhite$(ReadLine$(in))
		If Left$(ln$,2) <> "//"
			Select(state)
				Case 0:
					If Instr(ln$,"#HEADER") And file = False Then state  = 1
					If Instr(ln$,"#CORE") Then state = 2
					If Instr(ln$,"#UNITWEAPONS") Then state = 3
					If Instr(ln$,"#UNITDATA") Then state = 4
				Case 1:
					If Instr(ln$,"FILETYPE")  And file = False
						If Right$(ln$,Instr(ln$,"=")-2) = "UNITFILE"
							file = True
							this = New Unit
							this\UnitData = New UnitData
						EndIf
					EndIf
					If Instr(ln$,"VERSION")
						If Not Right$(ln$,Instr(ln$,"=")-5) = VS_UNITFILE Then RuntimeError("Fileversion was not current " + Right$(ln$,Instr(ln$,"=")-5))
					EndIf
					If Instr(ln$,"#ENDHEADER") Then state = 0
				Case 2:
					If Instr(ln$,"UNITNAME")  And file = True 
						this\Name$ = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"NUMWEAPONS")  And file = True 
						this\NumWeapons = Right$(ln$,Instr(ln$,"=")-5)
						this\Weapons = New WeaponList
						w.WeaponList = this\Weapons
						For i = 1 To NumWeapons-1
							w\NextWeapon = New WeaponList
							w = w\NextWeapon
						Next
					EndIf
					If Instr(ln$,"IMGSM")  And file = True
						this\ImgSm = LoadImage(Right$(ln$,Instr(ln$,"=")-5))
					
					EndIf
					If Instr(ln$,"IMGLG")  And file = True 
						this\ImgLg = LoadImage(Right$(ln$,Instr(ln$,"=")-5))
					EndIf
					If Instr(ln$,"#ENDCORE") Then state = 0
				Case 3:
					w.WeaponList = this\Weapons
					If Instr(ln$,"WEAPON")
						w\WeaponName$ = Right$(ln$,Instr(ln$,":")-2)
					EndIf
					If Instr(ln$,"TYPE")
						w\WeaponType$ = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"DAMAGE")
						w\Damage = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"SPEED")
						w\Speed = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"AIRDAMAGE")
						w\AirDamage = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"]") And w\NextWeapon <> Null Then w = w\NextWeapon 
					If Instr(ln$,"#ENDUNITWEAPONS") Then state = 0
				Case 4:
					If Instr(ln$,"SPEED")
						this\UnitData\Speed = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"ARMOR")
						this\UnitData\Armor$ = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"HEALTH")
						this\UnitData\Health = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"TYPE")
						this\UnitData\UnitType$ = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"TRANSPORT")
						this\UnitData\Transport = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"CARGOSPACE")
						this\UnitData\TransportSpace = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"SPACE")
						this\UnitData\Space = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"DETECTOR")
						this\UnitData\Detector = Right$(ln$,Instr(ln$,"=")-5)
					EndIf
					If Instr(ln$,"#ENDUNITDATA") Then state = 0
				Default:
					
			End Select
		EndIf
	Wend
Return this
End Function 


if you see anything that iv done that is wrong... please feel free to comment but what im really after is suggestions on how to do the WeaponList, as i would like a solution that is very friendly(lol) thanks


Ziltch(Posted 2003) [#6]
What is this use for in later code?

Field NextWeapon.WeaponList

Blitz has a command called AFTER which should remove the need for this.

I can not help if I do not know how you plan to access this data and how the data may change during game play.