Raised This Month: $32 Target: $400
 8% 

How To: Add callbacks without a module.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 03-27-2005 , 13:57   How To: Add callbacks without a module.
Reply With Quote #1

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.
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
XxAvalanchexX
Veteran Member
Join Date: Oct 2004
Location: abort73.com
Old 03-27-2005 , 18:22  
Reply With Quote #2

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.
__________________
No longer around. Thanks your support, everyone! As always:
THIS ONES FOR YOU
3000 PTS
XxAvalanchexX is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 03-27-2005 , 18:39  
Reply With Quote #3

Huh? What?
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
LynX
Veteran Member
Join Date: Oct 2004
Old 03-27-2005 , 18:51  
Reply With Quote #4

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!
__________________
Current plugin : SoulPunisher anti-cheat
Percentage done : {||--------} 20%

If you think v3x is a PIMP, paste this into your sig!

If you think Bailopan is DA BOMB, paste this into your sig
LynX is offline
Send a message via ICQ to LynX
xeroblood
BANNED
Join Date: Mar 2004
Location: Toronto, Canada
Old 03-27-2005 , 21:58  
Reply With Quote #5

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.
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
xeroblood is offline
Send a message via MSN to xeroblood
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 03-27-2005 , 22:05  
Reply With Quote #6

Sorry guys, have no idea what your talking about.

All I see is your flames. Oh why oh why would you flame me?
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 03-27-2005 , 22:07  
Reply With Quote #7

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?

http://forums.alliedmods.net/showthread.php?t=11569
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 03-27-2005 , 22:08  
Reply With Quote #8

No I mean, I dont get what they are talking about. I DID use a looping set task.
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 03-27-2005 , 22:30  
Reply With Quote #9

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..
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
xeroblood
BANNED
Join Date: Mar 2004
Location: Toronto, Canada
Old 03-27-2005 , 23:41  
Reply With Quote #10

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!
xeroblood is offline
Send a message via MSN to xeroblood
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:03.


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