Can I update system variables in the middle of the draw function?

edited January 2014 in Programming Questions

For example, is there a way to update the key variable during a loop that is within the draw function so the following would work?

Thanks in advance.

David

boolean done = false;
int count = 0;

void setup(){

}

void draw(){

  while(!done){
    count += 1;
    if(key == 'a') done = true;
  }

  println(count);

  exit();
}

Answers

  • edited January 2014

    I don't believe Processing's framework updates system variables key or keyCode during draw()! [-X
    However, Processing got a parallel Thread which enqueues every input event transpired.
    We just need to implement which 1s we're interested in!
    In your case, that'd be keyPressed() in order to check for 'A' and display final frameCount: *-:)

    // forum.processing.org/two/discussion/2726/
    // can-i-update-system-variables-in-the-middle-
    // of-the-draw-function
    
    void setup() {
      size(300, 100);
      frameRate(60);
    }
    
    void draw() {
      frame.setTitle("Frame Counter: #" + frameCount);
    }
    
    void keyPressed() {
      if (keyCode == 'A') {
        println(frameCount);
        exit();
      }
    }
    
  • As said, when draw() is executing, Processing variables are not updated. The common idiom with Processing is to use draw() as the body of a loop:

    void draw() {
      count += 1;
      if (key == 'a') done = true;
    
      if (done) { 
        println(count);
    
        exit();
      }
    }
    

    PS.: you should read To newcomers in this forum: read attentively these instructions too...

  • I realize that using draw() as the body of the loop is the common idiom with Processing. I was wondering whether it is possible to implement a more sequential way of organizing a game loop. For example, suppose a game loop had this structure:

    Ask the user for input ("enter your age").

    Get input from user.

    process input.

    ask user for more input (if age is greater than 70, ask the user to enter their favorite song, else ask user to enter their favorite tv show.)

    get input from user.

    process input (recommend a movie).

    Could you implement this structure in Processing, or would you have to translate it into one using states or something equivalent?

  • edited January 2014

    The best way to achieve this kind of structure - in nearly any programming language - in fact is a state system, at least in my opinion. Statesystems can be class or switch/if based and are relatively easy to implement in Processing. A quick example how to implement a simple switch based statesystem:

    String typedText = "";
    int blinkUpdate = 0;
    int state = 0; // Current state/question
    String[] questions = { "How old are you?", "What's your favourite song?", "What's your favourite TV-Show?", "Thanks for your input! Start again? (Y/N)" }; // State related questions
    
    
    void setup() {
        size(300, 100);
    }
    
    
    void draw() {
        background(#000000);
        fill(#ffffff);
        text(questions[state], 10, 10 + textAscent());
        fill(#00ff00);
        text(typedText + ((((blinkUpdate - millis()) >> 9) & 1) == 1 ? "|" : ""), 10, 30 + textAscent());
    }
    
    
    void keyTyped() {
        if(key != CODED) {
            switch(key) {
                case BACKSPACE:
                    typedText = typedText.substring(0, max(0, typedText.length()-1));
                break;
                case ENTER: case RETURN:
                    handleAnswer(typedText);
                    typedText = "";
                break;
                case TAB: case ESC: case DELETE:
                break;
                default:
                    typedText += key;
            }
            blinkUpdate = millis();
        }
    }
    
    
    void handleAnswer(String answer) {
        switch(state) {
            case 0: // How old are you?
                int age = int(answer);
                println("You are " + age + " years old.");
                if(age > 70)
                    state = 1;
                else
                    state = 2;
            break;
            case 1: // What's your favourite song?
                println("Your favourite song is: " + answer);
                state = 3;
            break;
            case 2: // What's your favourite TV-Show?
                println("Your favourite TV-Show is: " + answer);
                state = 3;
            break;
            case 3: // Thanks for your input! Start again? (Y/N)
                answer = answer.toLowerCase();
                if(answer.equals("y"))
                    state = 0;
                else if(answer.equals("n"))
                    exit();
                else
                    println("Please type \"Y\" or \"N\".");
            break;
        }
    }
    
  • Wow! Nice keyboard input, @Poerch ! I've got a not-too-nice using JOptionPane though: 8-|

    /** 
     * No Repeat ID Input (v1.10)
     * by GoToLoop (2013/Nov)
     * 
     * forum.processing.org/two/discussion/869
     * /check-array-contents-with-arraylist
     */
    
    import static javax.swing.JOptionPane.*;
    
    final StringList ids = new StringList( new String[] {
      "Eric", "Beth", "Katniss"
    } 
    );
    
    void draw() {
      println(ids);
    
      final String id = showInputDialog("Please enter new ID");
    
      if (id == null)   exit();
    
      else if ("".equals(id))
        showMessageDialog(null, "Empty ID Input!!!", 
        "Alert", ERROR_MESSAGE);
    
      else if (ids.hasValue(id))
        showMessageDialog(null, "ID \"" + id + "\" exists already!!!", 
        "Alert", ERROR_MESSAGE);
    
      else {
        showMessageDialog(null, "ID \"" + id + "\" successfully added!!!", 
        "Info", INFORMATION_MESSAGE);
    
        ids.append(id);
      }
    }
    
  • I agree: Wow! Both are really useful examples. Thanks to you both.

    David

  • @GoToLoop, @dlewine: Haha, thanks! :D I like these oldschool-ish console inputs.

Sign In or Register to comment.