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.
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:
setup()
at the beginning, then whenever frameCount = 0draw()
repeatedly wheneverloop()
is set (on by default) or manually whenredraw()
is calledsettings()
, primarily if you need to setsize()
with a variableexit()
if you choose to override it – just be sure to callsuper.exit()
at the end.- 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) {
m.read();
}