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)
   assignment to variables above the setup loop
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: assignment to variables above the setup loop  (Read 663 times)
kevinP

Email
assignment to variables above the setup loop
« on: May 13th, 2004, 12:33am »

Hi all,
 
I can assign a value to radius here outside my two loops, but I can't do the same with a color (it freezes). Is there a reason why?
 
Code:

int radius = 100;
color clDot = color(50,100,100);
 
void
setup()
{
  size(100,100);
  ellipseMode(CENTER_DIAMETER);
  colorMode(HSB, 100);
  smooth();
}

 
-K
 

Kevin Pfeiffer
justo


Re: assignment to variables above the setup loop
« Reply #1 on: May 13th, 2004, 12:52am »

basically there are what are called "static" variables and methods...that means that no matter how many of a class you instantiate (think of when people make an array of a Particle class in processing) they all share that variable or method. its good for common methods, like finding the distance between two points, cause not everyone needs their own copy, or constants, like CENTER_DIAMETER in ellipseMode()...you dont need to change its value (its really an int) but everyone needs it, so theres no need to waste the memory with a bunch of copies of it.
 
ok, so this applies to your example because outside of the methods is basically a "static space," ie, it exists outside of any particular instance of that class. radius is set to 100 for every version of that class, and every clDot is supposed to be set to the color that your color() call returns. you can always change it later as your program runs, but the assignment is static. color() is not a static method, because it depends on things like the color mode and whatnot, so it cant be called (because of static rules...but it makes sense since you dont have access to variables of your class before it is even instantiated).
 
i tried not to cut corners technically, so hopefully my explanation was at least somewhat clear.
 
kevinP

Email
Re: assignment to variables above the setup loop
« Reply #2 on: May 13th, 2004, 1:37am »

Not so sure I completely understand. I guess the difference is that you can assign values to primitive data types outside of setup(), loop(), and draw(), but not objects. (sound right?)
 
Something else I don't understand is why I can't use translate, etc., within the setup() function.
 
Thanks for your help!
 
-K
 

Kevin Pfeiffer
justo


Re: assignment to variables above the setup loop
« Reply #3 on: May 13th, 2004, 4:51am »

thats basically right, though in this case color is actually stored as an int. its just that you cant call a dynamic method (meaning methods that depend on other, possibly changing, variables and methods) outside of other dynamic methods (eg setup(), loop(), draw(), and any other methods you write).  
 
 
also, though i'm not sure about the exact reason why you cant in processing, wouldnt calling translate() in setup() be meaningless since processing discards the transforms after every frame?
 
kevinP

Email
Re: assignment to variables above the setup loop
« Reply #4 on: May 14th, 2004, 10:09pm »

on May 13th, 2004, 4:51am, justo wrote:
[...]
wouldnt calling translate() in setup() be meaningless since processing discards the transforms after every frame

 
I was wrong about this - you can use translate() in setup(), but as you pointed out, it gets discarded after you exit the loop.
 
-Kevin
 

Kevin Pfeiffer
fry


WWW
Re: assignment to variables above the setup loop
« Reply #5 on: Jul 15th, 2004, 4:41pm »

in megabucket, it's now possible to use color() outside of setup and loop. yeehaa.
 
and yes, translate etc work inside setup but wind up being 'ignored' since they're nuked just before re-entering loop().
 
justo


Re: assignment to variables above the setup loop
« Reply #6 on: Jul 15th, 2004, 5:36pm »

how did you make this work? do you just assume the color space is 8bit/color RGB space outside of setup and loop?
 
fry


WWW
Re: assignment to variables above the setup loop
« Reply #7 on: Jul 15th, 2004, 6:35pm »

on Jul 15th, 2004, 5:36pm, justo wrote:
how did you make this work do you just assume the color space is 8bit/color RGB space outside of setup and loop

yup. basically if BGraphics doesn't exist yet at runtime, it'll just use that as its default assumption, since a large portion of programs don't change it, and it was confusing the hell outta people.
 
Pages: 1 

« Previous topic | Next topic »