Functions that don't have to be called

I’m used to writing functions and then calling them in draw() or setup(), but in other people’s sketches, I’ve found functions that clearly run but don’t seem to be called or referenced anywhere else in the code. Am I just misunderstanding something? Thanks.

1 Like

Can you give a specific example of what you’re talking about?

Are you talking about functions like mousePressed() or keyPressed()? If so, Processing calls those automatically, just like it automatically calls the setup() and draw() functions.


i think that might be a example:
@quark Self rendering objects?


The “Self rendering objects” link shows how you can add graphic stuff to the frame without having to add code in the draw() method. The technique is aimed at library creators but can be used by sketchers. It should only be employed when there is a clear meaningful need to do so and not just on a whim.

There are other methods you can register but the sketcher needs to be able to create classes to get the best from this technique.

This page tells you more about it.


Expanding on Kevin’s answer:
The main methods (‘functions’) that Processing calls automatically in a sketch are:

  1. setup() at the beginning, then whenever frameCount = 0
  2. draw() repeatedly whenever loop() is set (on by default) or manually when redraw() is called
  3. settings(), primarily if you need to set size() with a variable
  4. exit() if you choose to override it – just be sure to call super.exit() at the end.
  5. the interactive methods that check for input events:
    • keyPressed()
    • keyReleased()
    • keyTyped()
    • mouseClicked()
    • mouseDragged()
    • mouseMoved()
    • mousePressed()
    • mouseReleased()
    • mouseWheel()

Some libraries also what can see like magic functions – generally the hooks / callbacks work through registerMethod(). Like keyPressed(), you put code in those functions and they just run, even though nothing in the sketch explicitly calls them. For example, in the Video library, movieEvent() is automatically called whenever a new frame is available; the sketch author can just read the frame, or they can put whatever code they want there.

// Called every time a new frame is available to read
void movieEvent(Movie m) {;