what's a callback function and when we should use them?
-
1`callback` is just a function – apple apple Mar 22 '20 at 14:23
-
Does this answer your question? [What is a callback function?](https://stackoverflow.com/questions/824234/what-is-a-callback-function) – Isaiah Mar 23 '20 at 00:51
1 Answers
A callback is a function you can pass to the framework (in this case Blender). The framework has the control and does some stuff. Now, whenever appropriate, the framework (Blender) calls (executes it with given parameters) this function. Therefore control is given back to your code.
(fictional) Example: Handover a function that prints a text, say
void test()
{
std::cout << "Got called back!" << std::endl;
}
There will be some means of registering it, let's say
onRenderCallback(test);
Now, whenever Blender is rendering it will execute test
and therefore print "Got called back!
".
Commonly, the function takes some argument. This can be used for mouse-input or similar. For example, you could register a function that on any keypress takes the key code and does something with it.
Callbacks are especially useful in the context of the observer-pattern.

- 159
- 1
- 11