Removing only one from list - help

Blitz3D Forums/Blitz3D Programming/Removing only one from list - help

Caton(Posted 2016) [#1]
Global numlist
Graphics3D 800,600,32,2
SetBuffer BackBuffer()
AntiAlias True
LoadFileData("datafile.dat")
While Not KeyDown( 1 ) 
Cls
if keyhit(2) then numlist=numlist+1:dat.dataobj = new dataobj
if keyhit(3) then
numlist=numlist-1
for dat.dataobj = each dataobj
delete dat
next
endif
if keyhit(4) then SaveFileData("datafile.dat")
Flip
Wend
ClearWorld
EndGraphics
End

Function SaveFileData(filename$)
file=WriteFile(filename$)
If file Then
WriteInt(file,numlist)
for dat.dataobj = each dataobj
WriteByte(file,dat\data1)
WriteInt(file,dat\data2)
WriteInt(file,dat\data3)
next
CloseFile(file)
EndIf
End Function

Function LoadFileData(filename$)
file=ReadFile(filename$)
If file Then
numlist=ReadInt(file)
for l=0 to numlist-1
dat.dataobj = new dataobj
dat\data0=ReadByte(file)
dat\data1=ReadInt(file)
dat\data2=ReadInt(file)
next
CloseFile(file)
EndIf
End Function

Type DataObj
Field data1,data2,data3
End Type



RemiD(Posted 2016) [#2]
I think that you are the king of description (of a problem), i wonder who will find a solution to that (since the description of the problem is vague).


Midimaster(Posted 2016) [#3]
.


Midimaster(Posted 2016) [#4]
I can only guess, what you want to ask...

You have a type "dataobj" and you add new instances "dat" to it. Now you want to remove one from this list?

The solution depends on, which one you want to remove.... You can remove the first, the last, or any position. Or you can kill one if Data1 reaches a condition. What do you want?



here is a sample to kill the first instance (thanks to Bobbysait)
Type DataObj
     Field Data1:INT, Data2:INT, Data3:INT 
End Type
.....
If keyhit(2) 
     numlist=numlist+1
     dat.dataobj = new dataobj
ElseIf keyhit(3) then
     numlist=numlist-1
     local dat.dataobj = First dataobj
     delete dat
endif



here is a sample to kill the last instance (thanks to Bobbysait)
Type DataObj
     Field Data1:INT, Data2:INT, Data3:INT 
End Type
.....
If keyhit(2) 
     numlist=numlist+1
     dat.dataobj = new dataobj
ElseIf keyhit(3) then
     numlist=numlist-1
     Local dat.dataobj = Last dataobj
     delete dat
endif



here is a sample to kill the instance at position P=4. Before you can do this, you need a unique identifier which points to the single instances. So add another field "Number" to the type.

P:INT=4
Type DataObj
     Field Data1:INT, Data2:INT, Data3:INT , Number:INT
End Type
.....
If keyhit(2) 
     numlist=numlist+1
     dat.dataobj = new dataobj
     dat.Number=numlist
ElseIf keyhit(3) then
     numlist=numlist-1
     For dat.dataobj = Each dataobj
          If dat.Number=p
               delete dat
               Exit
          Endif
     Next
endif



here is a sample to kill all instances, when Data1 is smaller than 5:
Type DataObj
     Field Data1:INT, Data2:INT, Data3:INT
End Type
.....
If keyhit(2) 
     numlist=numlist+1
     dat.dataobj = new dataobj
ElseIf keyhit(3) then
     numlist=numlist-1
     For dat.dataobj = Each dataobj
          If dat.Data1<5
               delete dat
          Endif
     Next
endif




My last sample is to kill instances which you don not need any more. Anywhere in your code you can "mark" them and later they will be killed together. Before you can do this, you need a flag "KillMe" which you can set to kill the instance later. So add another field "KillMe" to the type.

Type DataObj
     Field Data1:INT, Data2:INT, Data3:INT , KillMe:INT
End Type
.....
If keyhit(2) 
     numlist=numlist+1
     dat.dataobj = new dataobj
ElseIf keyhit(3) then
     numlist=numlist-1
     For dat.dataobj = Each dataobj
          If dat.KillMe=TRUE
               delete dat
          Endif
     Next
endif



steve_ancell(Posted 2016) [#5]
This is another reason why I stash my ojects inside an array, no offense RemiD! ;)

To make this more valid, it gives me a way of indexing them by number.


Bobysait(Posted 2016) [#6]
Use a cursor to point an object of your type then use the After/Before/First/Last to iterate
When you're on the good one use Delete on the pointer.

(in this sample, you 'll be able to move the cursor with left and right arrows)
LoadFileData("datafile.dat")

Local curData.DataObj
While Not KeyDown( 1 ) 
	Cls
	If KeyHit(2)
		curData = New dataobj
	EndIf
	
	If KeyHit(3)
		If CurData=Null Then CurData = Last DataObj
		If CurData<>Null Then Delete CurData
		CurData = Last DataObj
	EndIf
	
	; Right arrow -> select next dataobj (or first)
	If KeyHit(205)
		If CurData = Null
			CurData = First DataObj
		Else
			CurData = After CurData
			; keep curdata on last object if no next data exists
			If CurData = Null Then CurData = Last DataObj
		EndIf
	EndIf
	
	; Left arrow -> select previous dataobj (or last)
	If KeyHit(203)
		If CurData = Null
			CurData = Last DataObj
		Else
			CurData = Before CurData
			; keep curdata on first object if no previous data exists
			If CurData = Null Then CurData = First DataObj
		EndIf
	EndIf
	
	If KeyHit(4) Then SaveFileData("datafile.dat")
	
	Flip
Wend
ClearWorld
EndGraphics
End



Guy Fawkes(Posted 2016) [#7]
@Bobbysait :: Please correct your "/code]"

Thank You

~GF


Midimaster(Posted 2016) [#8]
Ah, did not know, tha Blitz3D already knows "first", "last", "before" and "after"