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 › creating new objects at runtime
Page Index Toggle Pages: 1
creating new objects at runtime (Read 828 times)
creating new objects at runtime
Dec 18th, 2009, 8:31am
 
im new to processing.. i sw in one of the processing tutorials that you can create objects in "setup()"  and call them at runtime in "draw()". what if i want to create objects at runtime when it satisfies some condition.

creating objects inside the draw() method doesnt seem like the best way ..

any idea ?

thanks in advance.
Re: creating new objects at runtime
Reply #1 - Dec 18th, 2009, 10:12am
 
You can create objects at any time you want.
Just be careful you don't create objects which could perfectly be reused.
Creating objects takes time.
Re: creating new objects at runtime
Reply #2 - Dec 18th, 2009, 10:22am
 
what i meant ws.. if i create objects inside draw() method.. each time that function is called.. that object gonna be reconstructted..


like..

draw(){
if(condition){
Object nm=new Object();
}
}

so each time the screen gets refreshed a new object is created (if "if conditon is true").  is it a good practice ?
Re: creating new objects at runtime
Reply #3 - Dec 18th, 2009, 10:32am
 
If possible, try to avoid doing it too much.
But you won't see much difference if you create only a few objects each time the draw method is called.
Re: creating new objects at runtime
Reply #4 - Dec 18th, 2009, 10:38am
 
what if i want to keep track of an object after creating it ? (like changing some value in a different iteration)

if i put it inside the draw() method it will be overridden ryte?
Re: creating new objects at runtime
Reply #5 - Dec 18th, 2009, 11:18am
 
declare it outside of setup() or draw() for later assignement :

Code:
Object yourObj;

void setup() {}

void draw() {
 if (condition) {
   yourObj = new Object(...);
 }
 if (otherCondition && yourObj != null) {
   yourObj.setPosition(...);
 }
}
Page Index Toggle Pages: 1