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 › void setup() error
Page Index Toggle Pages: 1
void setup() error (Read 1003 times)
void setup() error
Nov 8th, 2005, 12:52am
 
I am getting unexpected void errors when I have delcarations above it.  Does it have to be the first line of code?  What do I do if I need global variables across it and functions such as draw() and mousereleased()?  Is there a way to declare vars in setup() and have other functions such as draw() and event functions access them?  Or even send parameters to those latter functions?


Thanks!
Re: void setup() error
Reply #1 - Nov 8th, 2005, 2:31am
 
you can declare and assign primitive variables outside of setup() and draw(). these are global variables and can be assigned and accessed within both setup() and draw(). if you declare a variable inside setup, it can only be accessed inside setup. check out the example for variable scope:

http://processing.org/learning/examples/variablescope.html
Re: void setup() error
Reply #2 - Nov 8th, 2005, 6:50am
 
what about the error and possibly having declarations above it?  I thought I remembered creating vars above setup() :/


edit: forgot to say thank you Smiley there is no other way to pass vars to setup or anything though?

the following gives me an unexpected token: void error at the setup () line
the error doesnt appear if i move declarations to inside functions
Code:


color bgc =  color (100,50,0);
PFont maintext;
maintext = loadFont ("CG18.vlw");
PSound song;
song=loadSound ("song1.wav");
boolean playing=false;



void setup ()
{
 colorMode(RGB,255,255,255);

 if (online)
 {
   framerate(8);
 }
 else
 {
   framerate(24);
 }
 size (200, 48);
 background (255,266,255);
 loop();
}

void draw()
{

   background (bgc);  
}


void mouseReleased()
{
 
 if (playing==true)
 {
   song.loop();
   playing=false;
 }
 else
 {
   song.play();
   playing=true;
 }
}
Re: void setup() error
Reply #3 - Nov 8th, 2005, 7:56am
 
aqua_scummm wrote on Nov 8th, 2005, 6:50am:
what about the error and possibly having declarations above it  I thought I remembered creating vars above setup() :/


edit: forgot to say thank you Smiley there is no other way to pass vars to setup or anything though  I guess i could define a class wide variable (is that static) and just access that through a instance of a class in each function... sounds like a royal pain though

the following gives me an unexpected token: void error at the setup () line
the error doesnt appear if i move declarations to inside functions
Code:


color bgc =  color (100,50,0);
PFont maintext;
maintext = loadFont ("CG18.vlw");
PSound song;
song=loadSound ("song1.wav");
boolean playing=false;



void setup ()
{
 colorMode(RGB,255,255,255);

 if (online)
 {
   framerate(8);
 }
 else
 {
   framerate(24);
 }
 size (200, 48);
 background (255,266,255);
 loop();
}

void draw()
{

   background (bgc);  
}


void mouseReleased()
{
 
 if (playing==true)
 {
   song.loop();
   playing=false;
 }
 else
 {
   song.play();
   playing=true;
 }
}

Re: void setup() error
Reply #4 - Nov 8th, 2005, 11:55am
 
May I relate the box analogy?

Consider your variables as boxes. Before setup() you say what boxes you're going to need. Then in the setup you fill up those boxes. You can pre-fill simple boxes like int, String, float, boolean, etc. But more complicated ones can't be done like that (array contents, PImage, PFont, etc).

The following should run smoothly (touching wooden desk).
Code:

color bgc = color (100,50,0);;
PFont maintext;
PSound song;
boolean playing = false;

void setup ()
{
colorMode(RGB,255,255,255);
maintext = loadFont ("CG18.vlw");
song=loadSound ("song1.wav");
if (online)
{
framerate(8);
}
else
{
framerate(24);
}
size (200, 48);
background (255,266,255);
loop();
}
Re: void setup() error
Reply #5 - Nov 9th, 2005, 12:43am
 
thanks guys! sorry about the double post...


why is it that we cant do more complex ones before setup?  Does it have to do with whether its a class or what?
Page Index Toggle Pages: 1