FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Changing value of variables outside void loop?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Changing value of variables outside void loop?  (Read 374 times)
Phiil

WWW
Changing value of variables outside void loop?
« on: Feb 23rd, 2005, 7:40pm »

Once again, I am afraid I must ask for a morsel of wisdom to be thrown my way :/
 
I'm coding a generative music applet using ProMIDI, but I think this is again, more of a syntax/how Java works question.
 
I have a variable, int basspart, which I would like to default to a particular value when the applet starts - namely, MouseY-12.  However, I also want to use input from the UP, DOWN keys to change the value of basspart as the applet runs.
 
The problem is that if I set up basspart to the value MouseY-12 before void setup, it seems that whenever I try to assign it a new value from within the loop by basspart=basspart+1 for instance, the value of basspart stays the same.
 
If I initialise basspart within void setup, then I can't refer to it from within void loop.
 
If I initialise it at the beginning of void loop, then whenever within the appropriate if loop to detect the keypress, if I press UP/DOWN (which should increment/decrement the value of basspart by 1 respectively), basspart does as it should, BUT, when the loop repeats a fraction of a second later, basspart is naturally reset to the initial value, MouseY-12.  
 
I need to find a way to allow the keypresses to be cumulative, and not be reset when the loop, err, loops.
 
(The way I see it, if I could place basspart before void setup, but then update it's value in void loop, it would work as I want it to, but this doesn't seem to be possible.)
 
Any hints? Part of me thinks there is a way to do it using an array before void setup, but this seems like overkill really (and I couldn't get it to work). What simple thing am I missing?
 
On the positive side, I'm already getting some quite pleasant random music coming out of my speakers, which was one aim of this experiment. Now I'm trying to make it cool..
 
Phil
 
 
 
fjen

WWW
Re: Changing value of variables outside void loop?
« Reply #1 on: Feb 23rd, 2005, 8:24pm »

as a homework please read everything below "scope":
http://stage.itp.tsoa.nyu.edu/~dts204/ppaint/week2/index.html
 
and here is more:
http://processing.org/learning/examples/variable_scope.html
 

 
Code:

int basspart;
void setup(){
basspart = 0;
println("setup: "+basspart);
}
void loop()
{
basspart++;
println("loop: "+basspart);
}

 
please note that using mouseX inside setup is not a good idea since it might not have been initialized or set to a strange value at that point (window popping up somewhere) ...
 
/F
 
Phiil

WWW
Re: Changing value of variables outside void loop?
« Reply #2 on: Feb 24th, 2005, 9:51am »

Thanks Fjen,
 
I could swear I tried it that way, but the trouble with being a newbie to OOP/Java is that I obviously screwed something else up that broke it.... doh.  
 
The first link you posted - really good!  I'd looked at the second, but it didn't seem to treat changing a variable that wasn't local....  
 
Anyhow, suffice to say that after your help I've got all my variables in the right places and it now does what I want it to
 
fjen

WWW
Re: Changing value of variables outside void loop?
« Reply #3 on: Feb 24th, 2005, 11:28am »

oh yes, it does:
"Variables may either have a global or local 'scope'. ..."
 
glad i could help ...
 
/F
 
Pages: 1 

« Previous topic | Next topic »