We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › self destroying objects
Page Index Toggle Pages: 1
self destroying objects (Read 466 times)
self destroying objects
Apr 1st, 2006, 12:39am
 
hello, i would like  to know how can i make that my objects self destroys after some time of beeing created. which is the better way of doing this?
all my objects are stored in a vector

many thanx for the help
punchik
Re: self destroying objects
Reply #1 - Apr 1st, 2006, 12:53am
 
Time in term of frames, or real-world time?

If it's in terms of after 50 frames, you just need to increment a value of every object in the Vector each frame, then fi the value is over a certain ammount, remove it from the Vector.

If it's time based, you jsut have to write the class such that the time they're created is put into a variable when they're created, then in draw, loop through them all and see which ones are older than a certain value, then remove them from the Vector.
Re: self destroying objects
Reply #2 - Apr 1st, 2006, 1:27pm
 
its time based  (real world time). so if i remove an object from my vector: does it becomes destroyed? is this the way it works?
the way i was thinking about doing this is having a function in my class that evaluates the time passed since it was created and when it arrives to a certain value it autodestroy(self destroy)
something like this:
 if ( timepassed >= 2000 )
 {
autodestroy;
 }

Is there any way of making my object auto destroy?
Re: self destroying objects
Reply #3 - Apr 1st, 2006, 3:12pm
 
Not really. Things are only destroyed when nothing points to them anymore. So the best an object could do to itself is jsut do nothing when asked.

Also, unless you're going to make every object a seperate thread, you need to actually call a function in the object to get it to check how old it is and then you have to remove it from the vector.

There's no concept of creating an object, and then it magically checking how old it is every now and then, and eventually deciding to stop existing. Unless you call a function in the object, it'll do nothing, it can't run itself..
Page Index Toggle Pages: 1