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 › How to: delete object, make an array of objects
Pages: 1 2 
How to: delete object, make an array of objects (Read 6291 times)
How to: delete object, make an array of objects
Jun 4th, 2005, 9:42pm
 
I'm coming from actionscript, so the java syntax is a little foreign to me, so excuse my noobness. I just made a class of dots/particles that goes like this:

Code:

class MRect {
float xpos; // rect xposition
float ypos ; // rect yposition
float vx=0; //viteza pe orizontala
float vy=0; //viteza pe verticala
int col = 255;
float damping; // damping of the movement

MRect(int ix, int iy, float idamping) {
xpos = ix;
ypos = iy;
damping = idamping;
println("wooohoo");
}

void display() {
col -= 1;
fill(col);

vx += random(2) -1;
vy += random(2) -1;
vx *= damping;
vy *= damping;
xpos += vx;
ypos += vy;
rect(xpos, ypos, 1,1);
}
}


in DRAW I just keep calling
 r1.display();
 r2.display();

The FIRST question is, how do I get r1 and r2 to be actually stored in an array so I can do a for loop through them all ?


And the SECOND q, how can I remove an object from memory ? I want to remove them when (col<5).

Thx
Re: How to: delete object, make an array of object
Reply #1 - Jun 4th, 2005, 9:44pm
 
And here's the entire code if you need it ...

Code:

MRect r1, r2, r3, r4;

void setup(){
size(500, 400);
fill(255);
noStroke();
float damping = .98;
r1 = new MRect(100,100,damping);
r2 = new MRect(200,200,damping);
}




void draw() {
background(0);

r1.display();
r2.display();

}




class MRect {
float xpos; // rect xposition
float ypos ; // rect yposition
float vx=0; //viteza pe orizontala
float vy=0; //viteza pe verticala
int col = 255;
float damping; // damping of the movement

MRect(int ix, int iy, float idamping) {
xpos = ix;
ypos = iy;
damping = idamping;
println("wooohoo");
}
void display() {
col -= 1;
fill(col);

vx += random(2) -1;
vy += random(2) -1;
vx *= damping;
vy *= damping;
xpos += vx;
ypos += vy;
rect(xpos, ypos, 1,1);
}

}
Re: How to: delete object, make an array of object
Reply #2 - Jun 4th, 2005, 10:01pm
 
Code:

MRect [] r;
void setup()
{
r = new MRect[arraysize];
for(int i = 0; i < r.length; i++)
r[i] = new MRect(100,100,damping);
}
void draw()
{
for(int i = 0; i < r.length; i++)
r[i].render();
}


something like that.. allthough now all the rects end up in 100,100, so you might want to multiply y with i, or something Smiley

good luck

-seltar

-seltar
Re: How to: delete object, make an array of object
Reply #3 - Jun 4th, 2005, 10:06pm
 
Aha. Thank you. It worked, I was close Smiley

Now, how do I remove an object from memory, because when it gets to about 1500 it starts to crawl ?
Re: How to: delete object, make an array of object
Reply #4 - Jun 4th, 2005, 10:08pm
 
that part i havn't figured out.. so what i do, is reuse the objects that i'm supposed to delete.. first give them a value of alive, then set alive to false when you want to delete it, then loop through the array searching for rects which aren't alive, and reassigning new variables to them, and setting them back to alive.. Smiley

hope this gives you some pointers..
Re: How to: delete object, make an array of object
Reply #5 - Jun 4th, 2005, 10:44pm
 
Hmm, that kinda sucks ... Sad
Re: How to: delete object, make an array of object
Reply #6 - Jun 4th, 2005, 10:45pm
 
Nou your answer Smiley the fact that I can't dele objects, it gets a little messy ...
Re: How to: delete object, make an array of object
Reply #7 - Jun 4th, 2005, 11:27pm
 
http://forum.java.sun.com/thread.jspa?threadID=526359&messageID=2525736
Lots of info there..
---
Setting an object reference to "null" tells the garbage collector that at least this one reference to the object is no longer needed. Once all references to an object are cleared, the garbage collector is free to reclaim that space.
---
When explicit nulling is helpful
http://www-106.ibm.com/developerworks/java/library/j-perf08273.html
---
Why explicit calls to gc() and explicit nulls may not be such a great idea.
---
Re: How to: delete object, make an array of object
Reply #8 - Jun 6th, 2005, 11:49pm
 
Undecided

assigning null to anything doesn't work, meaning that I get an error. I'm stuck, seems weird how I can't tell something that it's not needed anymore. I could prolly get away with about an hour of running this sketch, but I'd like to run it A LOT, I'm   gonna have to do some testing/experimenting, but it doesn't look good without some way of telling objects to die.

But thanks for your help so far, I doubt I'd have gotten so far without assistance.
Re: How to: delete object, make an array of object
Reply #9 - Jun 7th, 2005, 12:00am
 
Now I have this:

Code:

void draw() {
background(0);

for (int i=0;i<num;i++){
arr[i].update();
}
}

void mouseMoved(){
arr[pos] = new MRect(mouseX, mouseY);
pos++;
num = max(pos, num);
if (pos>200) pos=0;
}




class MRect {
float xpos; // rect xposition
float ypos ; // rect yposition
float vx=0; //viteza pe orizontala
float vy=0; //viteza pe verticala
boolean alive = true;
int col = 255;

MRect(int ix, int iy) {
xpos = ix;
ypos = iy;
}


void update() {
if (alive){
col -= 5;
if (col<5) alive=false;
fill(col);

vx = (vx+random(2) -1)*damping;
vy = (vy+ random(2) -1)*damping;
xpos += vx;
ypos += vy;
rect(xpos, ypos, 1,1);
}
}

}


I wonder if the garbage collector understands the fact that the objects that I'm overwriting (by making pos=0 if pos>200) are no longer reachable and frees the memory ...
Re: How to: delete object, make an array of object
Reply #10 - Jun 7th, 2005, 12:23am
 
Wooo, it works !! Cheesy    This garbage collector seems smart Smiley
I left it running for a while and the memory is still the same, very cool.
Re: How to: delete object, make an array of object
Reply #11 - Jun 7th, 2005, 2:36am
 
Nice Wink now we know.. until next-time, my fellow programmer..
Re: How to: delete object, make an array of object
Reply #12 - Jun 7th, 2005, 4:43am
 
yup! :] setting pointers that point to your objects to null and going out of scope makes them go bye bye. so easy.

now I have a question that needs asking.. which I will post in a new thread!
Re: How to: delete object, make an array of object
Reply #13 - Jun 7th, 2005, 12:15pm
 
yeah, I'm not actually nulling them, I'm just pretending they never existed, by making them point to a new object. Apparently gc() sees that and cleans up the memory.

Code:

arr[pos] = new MRect(mouseX, mouseY);
pos++;
if (pos>200) pos=0;

Re: How to: delete object, make an array of object
Reply #14 - Jun 7th, 2005, 10:30pm
 
No, technically you're overwriting them.

The whole point of setting up the MRect[] array is to reserve space in memory for MRect objects. So when you assign a new MRect object a position in the array you are replacing the memory space of the previous object.

As I understand it, the array represents the reserved memory space, not references to objects, thats why you can only have a single type of data in an array.

Edit: After reading Amoeba's comments and the links provided I concede my view above is incorrect. Thanks for the info amoeba
Pages: 1 2