Looping in Types

Previous topic - Next topic

MrPlow

Another loopy question...

Is there any occasion when looping through TYPES and you have a function eval like so...

hitv = hitthenemy(xx,yy,xxx,yyy,clan1,clan2)   

...if the current item is deleted how does that affect the returned value to the current item loop?


enemy 1 is moving and encounters enemy 2
    enemy 1 is using collision detection and fires a check to ensure that enemy 2 is an enemy and if so they fight.
         {fight resolved and enemy 1 (attacker) is killed  RETURN 2 ELSE RETURN 0}
We are now back in enemy 1 loop .. so is enemy 1 loop gone or instantly moved on?

I think writing this out gives me a possible solution...mark the unit as dead and clean up after the loop??

I'm sure others have had this issue before...




Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

MrPlow

Looks like my "writing out the problem" has worked for me...

It was obvious that deleting the very item i currently evaluating was going to cause problem and unforeseen events...my units were acting crazy and I couldnt tell why....anyhow for others that run into this...here is my simple fix...

I added a status type to each unit - 0 = alive, 1=dead....created a sub called deathandtaxes and did a cleanup after the looping and before other collision events could fire.

:booze:
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

hardyx

#2
It's a good solution to use the alive state of the unit in a loop. You can use DELETE to delete the current item in a FOREACH loop too, but you can't use the deleted item anymore. If you use indexes to refer to enemies or something like, it's better to use an alive flag and you don't must to change the indexes.