Computer Science Canada How to destory/delete objects? |
Author: | tyuo9980 [ Thu May 12, 2011 7:48 pm ] |
Post subject: | How to destory/delete objects? |
How would you delete a variable after there is a collision made with it? eg) if you shoot an enemy the enemy dies and disappears, how would you delete the enemy's x and y coords? |
Author: | Raknarg [ Thu May 12, 2011 8:00 pm ] |
Post subject: | RE:How to destory/delete objects? |
You can't. The most common solution to use is a dead/alive boolean variable. |
Author: | tyuo9980 [ Thu May 12, 2011 8:38 pm ] |
Post subject: | Re: How to destory/delete objects? |
and then i can resort the true/false ones and then reduce the size of the flex array to save memory! that was what i was thinking as well. hoped for another way tho. thanks. |
Author: | Raknarg [ Thu May 12, 2011 8:41 pm ] |
Post subject: | RE:How to destory/delete objects? |
Why bother with a flexible array? It's a waste of memory. |
Author: | Insectoid [ Thu May 12, 2011 8:44 pm ] |
Post subject: | RE:How to destory/delete objects? |
Better to not reduce the size of the array, since doing so will cause a lot of lag. Turing doesn't actually resize the array; it makes a new one with the new size, copies everything from the old one into the new one, and deletes the old one. Might as well just use a large array (which also allows you to not increase the size of the array when adding new bullets/enemies). |
Author: | tyuo9980 [ Thu May 12, 2011 8:50 pm ] |
Post subject: | Re: RE:How to destory/delete objects? |
Insectoid @ Thu May 12, 2011 8:44 pm wrote: Better to not reduce the size of the array, since doing so will cause a lot of lag. Turing doesn't actually resize the array; it makes a new one with the new size, copies everything from the old one into the new one, and deletes the old one. Might as well just use a large array (which also allows you to not increase the size of the array when adding new bullets/enemies).
im making a geo wars pacifism clone. take a look in the new thread that i made in this forum just now. youll see what i mean. i might possibly have over hundreds of objects. |
Author: | Tony [ Thu May 12, 2011 9:04 pm ] |
Post subject: | RE:How to destory/delete objects? |
You could keep a statically allocated array, and an index variable that specifies where the content ends. Then just increment/decrement it when you need more/less space. |
Author: | tyuo9980 [ Thu May 12, 2011 9:06 pm ] |
Post subject: | Re: RE:How to destory/delete objects? |
Tony @ Thu May 12, 2011 9:04 pm wrote: You could keep a statically allocated array, and an index variable that specifies where the content ends. Then just increment/decrement it when you need more/less space.
so like for x : array 1 .. endx of int? would that work? since the array declaration isnt in the loop. and wouldnt that also make it slower? |
Author: | Tony [ Thu May 12, 2011 9:11 pm ] | ||
Post subject: | RE:How to destory/delete objects? | ||
something like
you are working with an array of 5 elements, but allocated enough space for 10 items. |
Author: | Insectoid [ Thu May 12, 2011 9:12 pm ] | ||
Post subject: | RE:How to destory/delete objects? | ||
More like;
Having hundreds of objects is precisely why you do NOT want to use a flexible array. Multiple objects will be created/destroyed every frame, so you'd be creating/destroying the entire array several times per frame. This would slow down incredibly fast. |
Author: | tyuo9980 [ Thu May 12, 2011 9:15 pm ] |
Post subject: | Re: How to destory/delete objects? |
ok i see. just one huge array then. i think 500 elements is enough since ill be sorting. |
Author: | Insectoid [ Thu May 12, 2011 9:20 pm ] |
Post subject: | RE:How to destory/delete objects? |
Sorting is also resource intensive. Might wanna limit how often you sort everything. |
Author: | Tony [ Thu May 12, 2011 9:33 pm ] |
Post subject: | RE:How to destory/delete objects? |
why would you need to sort that array? |
Author: | tyuo9980 [ Thu May 12, 2011 9:41 pm ] |
Post subject: | Re: RE:How to destory/delete objects? |
Tony @ Thu May 12, 2011 9:33 pm wrote: why would you need to sort that array?
well i wouldnt want to waste any more cpu power by drawing unneeded things on the screen so i would have an array of boolean to see how many enemies are still alive and then draw the minimum number of enemies. |
Author: | Tony [ Thu May 12, 2011 9:56 pm ] |
Post subject: | RE:How to destory/delete objects? |
it sounds like you'll be using a lot more CPU resources doing the sorting. Also, it wouldn't be necessary to sort anything, if your array contains only the elements that are alive. |
Author: | copthesaint [ Thu May 12, 2011 11:03 pm ] |
Post subject: | Re: How to destory/delete objects? |
You can always use demonwasps Hashmap. Very usefull collections are, and very efficent. http://compsci.ca/v3/viewtopic.php?t=20302&highlight=hashmap |
Author: | tyuo9980 [ Fri May 13, 2011 4:54 pm ] |
Post subject: | Re: RE:How to destory/delete objects? |
Tony @ Thu May 12, 2011 9:56 pm wrote: it sounds like you'll be using a lot more CPU resources doing the sorting.
Also, it wouldn't be necessary to sort anything, if your array contains only the elements that are alive. yea true. if alive then draw. |