Want to make a little debug mode in my program, need help

Hi, im a beginner (like, REALLY beginner) and i want to make a Debug Mode for my program. It shold be activated by pressing “o”, and deactivated with “p” (yes, it is intentional that it is (de-)activated with two different keys.) Here is the code for the debug stuff i have made thus far:

void debugCheck() {
if (keyPressed) {
if (key == ‘o’) {
debug = true;
} else if (key == ‘p’) {
debug = false;
}
}
}

void debug() {
if (debug == true) {
println(“X:” +mouseX + " Y:" + mouseY);
fill(255, 0, 0);
textSize(40);
text(“DEBUG MODE”, 360, 100);
}
}

It does not work at all. I have put both of the functions in the “draw”-loop, but nothing happens when i press “o”. Can someone help…?

This works for me:

boolean debug=false; 

void setup() {
  size(777, 888);
  background(111);
}

void draw() {
  background(111); 
  debugCheck() ;
  debug();
}

void debugCheck() {
  if (keyPressed) {
    if (key == 'o') {
      debug = true;
    } else if (key == 'p') {
      debug = false;
    }
  }
}

void debug() {
  if (debug) {
    println("X: " +mouseX + " Y: " + mouseY);
    fill(255, 0, 0);
    textSize(40);
    text("DEBUG MODE", 360, 100);
  }
}
1 Like

thanks! it appears the only thing different is that i used

if (debug == true) {

instead of just

if (debug) {

i don´t know what that difference that makes in practice, but it works with the rest of my code just fine.

instead of using if (keyPressed) { (which is an internal boolean)
you can use the function of the same name:

It gets called automatically - no need to call it from draw().

void keyPressed() {
 if (key == 'o') {
      debug = true;
    } else if (key == 'p') {
      debug = false;
    }
}
1 Like

that makes no difference; the error must be somewhere else

1 Like

oh. okay, that simplifies it a little

but what was your error then?

Please show your initial version of your code

1 Like

here, but i literally just removed " == true" from my original code and it worked.

void debugCheck() {
  if (keyPressed) {
    if (key == 'o') {
      debug = true;
    } else if (key == 'p') {
      debug = false;
    }
  }
}

void debug() {
  if (debug == true) {
    println("X:" +mouseX + " Y:" + mouseY);
    fill(255, 0, 0);
    textSize(40);
    text("DEBUG MODE", 360, 100);
  }
}

void setup() {
  size(720, 720);
  frameRate(100);
  textAlign(CENTER);
  output = createWriter("save.txt");
}

void draw() {
  background(255);
  //animate();
  //render_generalUI();
  //gameCheck();
  //if (pause == false) {
    renderBoard();
  }
  debugCheck();
  debug();
}

(ignore the comments, i just made that so you can run the code without removing these unassigned functions).

can’t see an error there really


boolean debug=false; 

void debugCheck() {
  if (keyPressed) {
    if (key == 'o') {
      debug = true;
    } else if (key == 'p') {
      debug = false;
    }
  }
}

void debug() {
  if (debug == true) {
    println("X:" +mouseX + " Y:" + mouseY);
    fill(255, 0, 0);
    textSize(40);
    text("DEBUG MODE", 360, 100);
  }
}

void setup() {
  size(720, 720);
  frameRate(100);
  textAlign(CENTER);
  //  output = createWriter("save.txt");
}

void draw() {
  background(255);
  //animate();
  //render_generalUI();
  //gameCheck();
  //if (pause == false) {
  // renderBoard();
  //}
  debugCheck();
  debug();
}

weird…i testet it again and it definetly does not work when i use debug == true instead of just debug. anways there is another problem: it is really inconsistent. sometimes when testing the sketch, the key presses just wont get detected, and i have to, for a reason i absolutely dont get, go into my settings menu inside of my program in order to be able to actiavte the debug mode. it then also works when i am not inside the settings. really confusing…if you want, i can send you the projects code and you can test it yourself.