Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to destory/delete objects?
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
tyuo9980




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: 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.
tyuo9980




PostPosted: 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.
Raknarg




PostPosted: 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.
Insectoid




PostPosted: 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).
tyuo9980




PostPosted: 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.
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
tyuo9980




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu May 12, 2011 9:11 pm   Post subject: RE:How to destory/delete objects?

something like
code:

var my_array : array 1 .. 10 of int
var end_of_array : int := 5

for i : 1 .. end_of_array
  ...

you are working with an array of 5 elements, but allocated enough space for 10 items.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Insectoid




PostPosted: Thu May 12, 2011 9:12 pm   Post subject: RE:How to destory/delete objects?

More like;
code:

var foo : array 1..1000 of int
var endx : int := 1
for x : 1..endx
    %do stuff
end for


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.
tyuo9980




PostPosted: 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.
Insectoid




PostPosted: 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.
Tony




PostPosted: Thu May 12, 2011 9:33 pm   Post subject: RE:How to destory/delete objects?

why would you need to sort that array?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
tyuo9980




PostPosted: 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.
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: