How to destroy object(self) from method

BlitzMax Forums/BlitzMax Beginners Area/How to destroy object(self) from method

abdedf(Posted 2010) [#1]
I tried the following, which did not work.

Method Destroy()
    self=Null
End Method




GW(Posted 2010) [#2]
not possible, anything you can set to null is only a reference.


Volker(Posted 2010) [#3]
If all references to an object are deleted (nullified), it will be
removed by the garbage collection.


abdedf(Posted 2010) [#4]
So if I create an object like this
Global gamelist:TList=CreateList()

Type SomeObject
	Field x:Int,y:Int
	Function Create(startx:Int,starty:Int)
		Local newgameobject:Tplayer=New Tplayer
		newgameobject.x=startx
		newgameobject.y=starty
		ListAddFirst(gamelist,newgameobject)

		newgameobject=Null   'set this to null

	End Function
End Type

SomeObject.Create(320,240)     'object to be deleted

For Object:SomeObject=EachIn gamelist
        ListRemove(list,Object)       'remove the object from list
Next



Would this completely delete the object?


plash(Posted 2010) [#5]
So if I create an object like this
[...]
Would this completely delete the object?
Yes. Note also that the newgameobject in the Create function doesn't need to be nulled. It will be invalid once the scope of the function has ended (upon return).


abdedf(Posted 2010) [#6]
Thanks.


DinoEntrails(Posted 2010) [#7]
Thank you, I have had a similar question for a while now.


Czar Flavius(Posted 2010) [#8]
When you add an object to a list, it returns a TLink which acts as a shortcut to that object. You can use this link to quickly remove an object from a list. You can add the object to the list in the New method so it always happens automatically.