Page 1 of 1

How to delete objects using a script

PostPosted: Tue Apr 21, 2015 1:44 am
by sOvr9000
At first, I was having trouble with figuring out how to delete objects with a script. After some experimentation, it seems that the perfect way to do it is to set an object's timeToLive property equal to 0.
I crossed this problem when I decided to program a star to absorb the mass of any planetary object that hit it, and the code for the star's onCollide event looks like this:

Code: Select all
(e)=>{
    radius = math.sqrt(radius ^ 2 + (e.other.density / density) * e.other.radius ^ 2);
    e.other.timeToLive = 0
}


As you can see, I'm deleting the other circle's instance by eliminating all the time it has to live.
Anyway, I hope this is useful to anyone who's new to programming with Algodoo! (I'm new myself)

Re: How to delete objects using a script

PostPosted: Tue Apr 21, 2015 8:26 pm
by pnvv
Interesting.

Re: How to delete objects using a script

PostPosted: Thu Apr 23, 2015 11:48 am
by Kilinich
scene.removeEntity()

Re: How to delete objects using a script

PostPosted: Sun Aug 09, 2015 2:10 pm
by FRA32
e.other.density = 0 is my typical way

Re: How to delete objects using a script

PostPosted: Mon Aug 10, 2015 3:20 am
by electronicboy
FRA32 wrote:e.other.density = 0 is my typical way

Don't use that, it causes a memory leak as objects aren't removed from the scene properly, they're just effectlvly set to an invalid state and not cleared from the memory.
We got scene.removeEntity() for a reason

Re: How to delete objects using a script

PostPosted: Mon Aug 10, 2015 3:28 am
by pnvv
Or just use killers. I mean, setting a killer on an object is just the nooby way of typing {scene.removeEntity(e.other)} into onCollide...