View Single Post
Author Message
Rolnaaba
Veteran Member
Join Date: May 2006
Old 10-09-2006 , 11:20   Half Life 1 Events: Registering, Using, and Reading them
Reply With Quote #1

Helpful Links
Half Life 1 Events and their Data
Correct use of certain Events by: VEN

Introduction

This tutorial will cover the correct ways to register, use, and read events. It is written to address beginners new to PAWN language. So lets begin:

Part 1: Registering Events

Registering events is quite easy. Just use register_event in the plugin_init() function to make that event usable in your code.


e.x.:

Code:
#include <amxmodx> #include <amxmisc> public plugin_init() {     register_plugin("Events", "1.0", "Rolnaaba")     register_event("DeathMsg", "Death", "a", "3!0") }
when using register_event the first parameter is the event to be registered (which in this case is the “DeathMsg” event, which is called whenever anyone dies) the second is the function to be called when this even happens (which in this case is “Death”), the third parameter are the flags (which in this case is flag “a” (global event), for more information on flags check your amxmodx.inc), the fourth parameter are the restrictions. In this case the restrictions are as follows: the 3 argument (which is whether the killshot was a headshot or not), Cannot (“!”) equal “0”. (this means the kill shot must be a headshot for the function to be called upon)
Here is the code from amxmodx.inc concerning register_event:
Code:
/* Registers event on which a given function will be called
* Flags:
* "a" - global event.
* "b" - specified.
* "c" - send only once when repeated to other players.
* "d" - call if is send to dead player.
* "e" - to alive.
* Examples for conditions:
* "2=c4" - 2nd parameter of message must be sting "c4".
* "3>10" - 3rd parameter must be greater then 10.
* "3!4" - 3rd must be different from 4.
* "2&Buy" - 2nd parameter of message must contain "Buy" substring.
* "2!Buy" - 2nd parameter of message can't contain "Buy" substring. */
native register_event(const event[],const function[],const flags[],cond[]="", ... );
Part 2: Using Events

Using an event is even easier than calling it. All you do is register the event you wish to use then on the function you set, then execute what you wish to do.
Lets say you want to create a chat message (sent to everybody on the server) that says “someone has died by a headshot”

e.x.:

Code:
#include <amxmodx> #include <amxmisc> public plugin_init() {     register_plugin("Events", "1.0", "Rolnaaba")     register_event("DeathMsg", "Death", "a", "3!0") } public Death() {     client_print(0, print_chat, “Someone has died by a headshot”) }

Part 3: reading the data that event collect

Most events have special data that is collected when that event occurs in game. Lets use the “DeathMsg” event as an example. This event is called whenever someone dies. The data it collects is as follows: (1) The Killer’s ID, (2) The Victim’s ID, (3) Was Kill A Headshot or Not, (4) Weapon Name that issued the Kill. (check out “HL1 Events and their data” in the Helpful Links Section at the beginning of this tutorial for more information on all events and their data). This means we can use the read_data function to get this data.
e.x.:

Code:
#include <amxmodx> #include <amxmisc> public plugin_init() {     register_plugin("Events", "1.0", "Rolnaaba")     register_event("DeathMsg", "Death", "a", “3!0) } public Death() {     new victim = read_data(2)     client_print(victim, print_chat, “You died by a headshot”) }
This would get the ID of the person killed (new victim = read_data(2)) and print “You died by a headshot” in their chat, when the “DeathMsg” event is called. You use read_data(2) since Victim ID is the second pack of data collected during this event. If we had written:

Code:
#include <amxmodx> #include <amxmisc> public plugin_init() {     register_plugin("Events", "1.0", "Rolnaaba")     register_event("DeathMsg", "Death", "a", “3!0) } public Death() {     new victim = read_data(1)     client_print(victim, print_chat, “You died by a headshot”) }
This would have printed “You died by a headshot” in the chat of the killer’s screen since we used read_data(1) to read the first pack of data, which in this event is the killers ID, and not read_data(2) (the victims ID).
__________________
DO NOT PM me about avp mod.

Last edited by Rolnaaba; 11-01-2006 at 10:19.
Rolnaaba is offline