1 (edited by HondaDirtBiker 2006-11-24 14:28:34)

Topic: Removing Scene Nodes from an Array.

I need to create scenenodes in an array, find information about each element, then delete certain scenenodes within the array.
I wrote this code to delete certain nodes but after I delete some nodes then run the function again I receive an error saying "unhandled memory exception, cannot getposition()"

'Delete Asteroid
Local PlayerPos:Vector3df = PlayerNode.getPosition() 'Position of Player

If (AsteroidNode.Size() > 0)
    Local Ast:Int 'Current Array Element
    
    For Ast = 0 To AsteroidNode.Size() - 1
        Local AstNode:ISceneNode = AsteroidNode.elementAt(Ast) 'Current Asteroid Node
        
        If (AstNode.isValid() = True)
            If (AstNode.getPosition().getDistanceFrom(PlayerPos) > Radius And AstNode.getName() >< "Cave")
                AstNode.Remove()
                AsteroidNode.erase(Ast)
            End If
        End If
    Next
End If

I could be removing the scenenodes incorrectly but, If I don't remove the scenenode before the element, the scenenode is not removed.

Re: Removing Scene Nodes from an Array.

greetings smile  the only thing that raises a flag in my mind is the erase() call inside the loop.  this will resize the array during the loop and cause skipping of nodes.  for example, if i was on node 1 and then erased it, node 2 is now node 1 but my next index is 2.  old node 2 is now completely skipped.  i would try building a TList or array to queue up the erase calls and then call them after the main for loop.  the second loop would essentially call the erase() in descending order.  an alternative is store the size and loop backwards in your for loop using the stored size instead of the Size() call. 

also, are you rebuilding the array coming back into this code or is the AsteroidNode array persistant?  if for some reason Irrlicht decides to remove one of the other scenenodes in the array that could cause this error also.

on a side note, i have long thought about a simple try/catch that would improve the IsValid() functionality.  i may see if there is something i can do about that in the next version.  the reason i have not yet done it is most of the time the crashes are telling you something.  not sure if i want to eliminate that or not.

EDIT: i checked out the SpacePirate screenies.  looking good smile

Re: Removing Scene Nodes from an Array.

Thank you, that was the problem. Now it's working fine.
Thank you for the compliment. Wait till you see what I'm working on.
Keep up the great work on the mods. Bye