[3D] removeObject() // can't use an element from an array as a handle
-
Hi,
I use an array to place my 3d objects but it seems that i can't remove them (because the handle is an array element).
Is there another solution ?
Thankstilemap [ i ] = placeObject ( ... ) // is okay object_to_remove = i + 80 removeObject (tilemap [ object_to_remove ] // => error "incorrect type int for parameter 1, expected handle"
-
Are you sure there is an object in position I + 80?
What happens if you instead just try to remove the object you just added at position i?
-
This post is deleted! -
with [i] no problem, the object is removed.
You're right, there was no object in this position. There's a problem with my loop.
Thank you @vinicity ! -
Nevertheless, how would you do to test if there's an object in a position before trying to remove it ? Otherwise, is there a way to prevent the program from crashing in this type of case ? (the handle of each object is an index in an array (tilemap)that points to an index in an array of structures. )
Not sure i'm understandable... -
-
Have you tried moving the objects to their new positions instead of removing and creating them at the new location? This could get rid of the issue.
-
No it can't work. :/ Although the displayed area looks repetitive, it is not. In fact, I'm only displaying a 24x24 "tiles" area by moving around in a much larger tilemap. I could have a lot more diversity in the tiles, but I preferred to do it like this, to see if I didn't have row shifts or other problems.
-
Maybe, instead of keeping 3D objects in your array, you keep structs, that each contains the 3D object and a Boolean flag which indicates wether it has already been removed or not?
Or keep a separate array with the same indices that just contains the booleans.
-
Your solution makes sense, you can see that I'm not used to using structures! I will avoid wasting memory with a new array. I'll try as soon as I have some time... Thank you @vinicity !
-
Finally, I couldn't wait and chose the fastest solution, I created a separate array to check if the slot was free or not. It works ! It reassures me because I was not sure that there were no other hidden problems.