PDA

View Full Version : I'm new to coding and want to understand what "callback" is



Distortional
October 30th, 2006, 22:26
I want to understand all that I put into a piece of code. I can get most other things but this concept. For example - out of the helloworld script here (http://www.psp-programming.com/tutorials/)


/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
sceKernelExitGame();
return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
int cbid;

cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);

sceKernelSleepThreadCB();

return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
int thid = 0;

thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0) {
sceKernelStartThread(thid, 0, 0);
}

return thid;
}


What does that mean?
Thanks :)

yaustar
October 30th, 2006, 23:17
Basically setting up a call back means that you tell the program to run a function or some code when a particular event happens such as the player pressing a button or when a length of time has passed.

The alternative is to poll each frame eg, (pseudocode)
if player has pressed x then
do somestuff
end

The problem is with this, you are wasting CPU cycles doing the check each frame. With a callback, the whole thing only gets triggered when the button is pressed. It also allows you to cut dependenices between classes/files/blocks of code making the project more managible especially in a team project.

Distortional
October 31st, 2006, 08:24
Ok, that explains it. So what is the code I posted above telling the program to do?

What's return 0; ?
What's return thid; ?

A thread is a particular piece (thread?) of coding, yes?
And at the top of the coding? What does the "Exitcallback" bit mean? and the Kernalcallback bits?

Thankyou so much for helping me. I'm just getting a grasp on the coding for the moment before I start to make myself some simple little programs.

yaustar
October 31st, 2006, 19:19
Okay, if you just started programming, those will be covered in due time, but in a nutshell:

return 0 returns the value of 0 when the function ends.
return thid returns the value of thid when the function ends.

Thread as in Multithreaded programming.
Exit_callback is the name of the function. The function itself takes in 3 parameters, two integers and a void pointer to data, it calls the SDKs exit function so the game will exit when this callback is called.

I suggest looking at actual C/C++ tutorials or books rather then PSP specific programming ones. The PSP specific programming tutorials will NOT teach you C/C++.

bronxbomber92
October 31st, 2006, 21:06
Yes, what those callback functios do is petty enable able the ability to return to the XMB by pressing the home button. I suggest cprogramming.com for good, free place to start learning :)