AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   How To: Add callbacks without a module. (https://forums.alliedmods.net/showthread.php?t=11740)

Twilight Suzuka 03-27-2005 13:57

How To: Add callbacks without a module.
 
This tutorial will explain how one can add callbacks without building a new module.

A callback is a function that is called when certain things happen. For instance, "pfn_touch" is a callback, called whenever two objects touch.

Callbacks have a lot of useful features. For instance, while all plugins are executed in sequence, top ones to bottom ones, this means a task set on client_connect (id) is not on the same timer for every plugin, there are slight flaws. This caused problems for me when I needed several plugins to be working almost in synch.

So, registering callbacks for your own small functions can be useful, but typically it is not very useful if your plugin is entirely stand alone. Registering callbacks int he methods I'm about to provide is usually most useful when the callback will be received from a plugin different from your own.

In any case, the first thing you want to do is identify when and where you want the callback to originate from. For the purpose of this tutorial, we will use a simple task that will start on client_putinserver(id), and will push out a callback every second.

Code:
#include <amxmodx> // Init. Self explanatory public plugin_init() register_plugin("CallBack Test","Gamma","Twilight Suzuka") // Start the calling, keep it calling public client_putinserver(id) set_task(1.0,"callback",id,"",0,"b"); // Handle the sending of the callback. public callback(id) {     // Start the callback command.     server_cmd("test_callback %i",id)     // Send it.     server_exec(); }

Ok, so we've made a small plugin. It starts the callback feed when someone enters the server, and keeps pushing out callbacks every second. Now we need to receive them:

Code:
#include <amxmodx> // Init. Self explanatory public plugin_init() {     register_plugin("CallBack Test","Gamma","Twilight Suzuka")     // Catch the command we send.     register_srvcmd("test_callback","handle_callback") } // Start the calling. public client_putinserver(id) set_task(1.0,"callback",id,"",0,"b"); public client_disconnect(id) remove_task(id); // Handle the sending of the callback. public callback(id) {     // Start the callback command.     server_cmd("test_callback %i",id)     // Send it.     server_exec(); } // Handle the callback public handle_callback() {     // Get the ID of the person being sent about.     new arg[32]     read_argv(1,arg,31)     new id = str_to_num(arg)         // Tell the server console we got the message     server_print("CallBack Received for: %i",id)     // Return continued so other people can catch it too.     return PLUGIN_CONTINUE; }

As you can see, this plugin sends its own callback, then receives it and prints a message saying it got it. This plugin, on its own, is useless. You could just as easily simply call the function yourself.

But, lets say the catching and sending parts are in two different plugins. Sure, you could force a call of that function and get it done quicker. But what if you didnt know what plugin to call? What if you wanted any plugin that wanted to receive the callback to receive it? In that case, this method is the best method.

So, making a callback is very easy. Find out where you want to start it, send a command, and hook that command to receive it. Using this method, you can quickly, effeciently, and reliably design sort of mini-mod plugins with their own callbacks and such.

- Melanie.

XxAvalanchexX 03-27-2005 18:22

Quote:

Originally Posted by Twilight Suzuka
Code:
// Handle the sending of the callback. public callback(id) {     // Start the callback command.     server_cmd("test_callback %i",id)     // Send it.     server_exec();     // Call it soon.     set_task(1.0,"callback",id); }

omgwtfggh4x why don't you use the looping function for set_task? It's so much easier and betterer lollerskates dontcha know, you said so.

Twilight Suzuka 03-27-2005 18:39

Huh? What?

LynX 03-27-2005 18:51

Quote:

Originally Posted by Twilight Suzuka
Oh fuck you.

Can't even try to help out people without people throwing it back at me.

Now, I'm sure Avalanche didn't wanted to be rude...
I tested the code and it seems to be running smoothly. In debug mode nothing even comes out. Nice tutorial! Thnx!

xeroblood 03-27-2005 21:58

Quote:

Originally Posted by XxAvalanchexX
Quote:

Originally Posted by Twilight Suzuka
Code:
// Handle the sending of the callback. public callback(id) {     // Start the callback command.     server_cmd("test_callback %i",id)     // Send it.     server_exec();     // Call it soon.     set_task(1.0,"callback",id); }

omgwtfggh4x why don't you use the looping function for set_task? It's so much easier and betterer lollerskates dontcha know, you said so.

:lol: ROFL!! Mel argues with me about that method, and then turns around and uses that method also!! But the best part is, she Flames me when I tried to help and now she's mad 'cuz someone Flames her when she tries to help!! I guess now she knows what it is like :P

Twilight Suzuka 03-27-2005 22:05

Sorry guys, have no idea what your talking about.

All I see is your flames. Oh why oh why would you flame me?

v3x 03-27-2005 22:07

Quote:

Originally Posted by Twilight Suzuka
Sorry guys, have no idea what your talking about.

All I see is your flames. Oh why oh why would you flame me?

:arrow:
http://forums.alliedmods.net/showthread.php?t=11569

Twilight Suzuka 03-27-2005 22:08

No I mean, I dont get what they are talking about. I DID use a looping set task.

v3x 03-27-2005 22:30

Quote:

Originally Posted by XxAvalanchexX
Quote:

Originally Posted by Twilight Suzuka
Code:
// Handle the sending of the callback. public callback(id) {     // Start the callback command.     server_cmd("test_callback %i",id)     // Send it.     server_exec();     // Call it soon.     set_task(1.0,"callback",id); }

omgwtfggh4x why don't you use the looping function for set_task? It's so much easier and betterer lollerskates dontcha know, you said so.

You must have edited it.. :P

xeroblood 03-27-2005 23:41

Quote:

Originally Posted by Twilight Suzuka
No I mean, I dont get what they are talking about. I DID use a looping set task.

It's okay to be wrong!! We still love ya Mel!! No one is against you!!
Your tutorial is great, and the idea of writing a tutorial on that topic was a good one..
But denying something you obviously did is foolish, and I think you are just insulting our intelligence! :?

Your original code is still in our quotes! :P


All times are GMT -4. The time now is 05:00.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.