Game Crash when object is destroyed within a list

BlitzMax Forums/BlitzMax Programming/Game Crash when object is destroyed within a list

Arkmon(Posted 2012) [#1]
Hello i seem to have a problem with my current game engine i need to be able to take an object with the image collide function then delete it via a list which works fine but then when i collide with a diffrent object it crashes however it only crashes with that object and not any others


If ImagesCollide(WaterImageload,Tank.X,Tank.Y,0,Player.Image:TImage,Player.X,Player.Y,0)
Player.Water:+900
Tank.RemoveFirst()
EndIf
If ImagesCollide(FoodImageload,Food.X,Food.Y,0,Player.Image:TImage,Player.X,Player.Y,0)
Player.Hunger:+900
FoodList.RemoveFirst()
EndIf
If ImagesCollide(Enemy.Image:TImage,Enemy.X,Enemy.Y,0,Player.Image:TImage,Player.X,Player.Y,0)
DrawText " Hello ",100,100   'crashes when this is run an object been destroyed
EndIf





Last edited 2012

Last edited 2012


Chalky(Posted 2012) [#2]
Do you not need something like:
If Enemy.Image<>Null then ' or whatever image might have been destroyed
    If ImagesCollide(Enemy.Image:TImage,Enemy.X,Enemy.Y,0,Player.Image:TImage,Player.X,Player.Y,0)
        DrawText " Hello ",100,100
    EndIf
EndIf



Derron(Posted 2012) [#3]
do not use "If Enemy.Image<>Null" - you will check it every run but keep it in the list

use something like:
For local obj:TMyObject eachin List
	if not obj
		List.Remove(obj)	'remove so next run it is not there anymore
		continue			'skip rest of "for loop" and continue with next one
	endif
	'your code
	If ImagesCollide(obj.Image,obj.X,obj.Y,0,Player.Image,Player.X,Player.Y,0)
	...
Next


Also there is no need to call a function like myfunction(param:paramType, ...)
You declared the param type already... save the ":paramType" to gain better readability/overview - exception are bad named fields which only exist if using external code.


bye
Ron


TomToad(Posted 2012) [#4]
I don't understand. First you use Tank as an user defined type in the ImagesCollide function, then you use Tank as a TList with Tank.RemoveFirst(). If Tank is a TList, then it wont have an X component nor Y component, and if it is an user defined type, then it is an error to use RemoveLast() on the Tank object, unless you have RemoveLast() defined as a method within the type and therefore we can't figure out the problem without that code.

Are you compiling in debug mode? If it is off, try turning it on, you might get a better description of what's causing your error. If you have debug on, try using Superstrict at the top of the code and see if that returns any errors.

Edit: after rereading your post, I think you might have made a typo and meant to use TankList.RemoveFirst(). If that is the case, be aware that using RemoveFirst() actually removes the first item on the list and not necessarily the one you are testing, possibly causing you to test against a non existent object at some point. I think you might actually want TankList.Remove(Tank).

Last edited 2012


Arkmon(Posted 2012) [#5]
Thanks Derron that helped dont understand why but it worked