File size: 7,716 Bytes
c2b71e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
This is a compilation of examples, explanations, and answers for commonly asked questions / issues.

The Countdown
Code:
#include <amxmodx>

#define TASKID  1996
#define SECONDS 10

new iSeconds

public plugin_init()
{
    register_plugin( "Countdown Example", "1.0", "Wrecked" )
    
    register_logevent( "LogEventRoundStart", 2, "1=Round_Start" )
}

public LogEventRoundStart()
{
    if( task_exists( TASKID ) )
        remove_task( TASKID )
        
    iSeconds = SECONDS
    set_task( 1.0, "TaskShowCountdown", TASKID, _, _, "a", SECONDS )
}

public TaskShowCountdown()
{
    set_hudmessage( 120, 120, 120, 0.50, 0.50, 0, 0.1, 0.8, 0.1, 0.1, -1 )
    show_hudmessage( 0, "Seconds Remaining: %i", iSeconds-- )
}
Detecting Kill/Headshot Streaks

Code:
#include <amxmodx>

new Headshots[33]
new Kills[33]

public plugin_init()
{
    register_plugin( "How To Keep Kill Counts", "1.0", "Wrecked" )
    
    register_event( "DeathMsg", "EventDeathMsg", "a" )
    
    register_logevent( "LogEventRoundStart", 2, "1=Round_Start" )
}

public EventDeathMsg()
{
    new killer = read_data( 1 )
    new victim = read_data( 2 )
    
    Headshots[victim] = 0
    Kills[victim] = 0
    Kills[killer]++
    
    if( read_data( 3 ) ) // headshot
        Headshots[killer]++
        
    // Kills holds kills of each player
    // Headshots holds headshots
}

public LogEventRoundStart()
{
    arrayset( Headshots, 0, 33 )
    arrayset( Kills, 0, 33 )
}
Once Every x Rounds

Code:
#include <amxmodx>

#define ROUND_INTERVAL  4

new iRound

public plugin_init()
{
    register_plugin( "Do Something Every X Rounds", "1.0", "Wrecked" )
    
    register_logevent( "LogEventRoundStart", 2, "1=Round_Start" )
}

public LogEventRoundStart()
{
    if( ( ++iRound % ROUND_INTERVAL ) == 1 )
        // do whatever you need to do here
}

Blocking Weapon Pickup
Code:
#include <amxmodx>
#include <hamsandwich>

public plugin_init()
{
    register_plugin( "Block Weapon Pickup", "1.0", "Wrecked" )
    
    new classname[20] // weapon_smokegrenade (19) + 1
    
    for( new i = CSW_P228; i <= CSW_P90; i++ )
    {
        if ( get_weaponname( i, classname, charsmax( classname ) ) )
        {
            RegisterHam( Ham_Item_Deploy, classname, "HamItemDeployPre", 0 )
            /* Other forwards that you could've used:
                * Ham_AddPlayerItem
                * Ham_Item_CanDeploy
            */
        }
    }
}

public HamItemDeployPre( weapon )
{
    return HAM_SUPERCEDE; // blocks function call
}

Properly Removing Buyzones (by Exolent[jNr])
Code:
#include < amxmodx >
#include < engine >
#include < fakemeta >

new const info_map_parameters[ ] = "info_map_parameters";

new g_hSpawn;

public plugin_precache( ) {
    new iEntity = create_entity( info_map_parameters );
    DispatchKeyValue( iEntity, "buying", "3" );
    DispatchSpawn( iEntity );
    
    g_hSpawn = register_forward( FM_Spawn, "FwdSpawn" );
}

public FwdSpawn( iEntity ) {
    static szClassname[ 32 ];
    entity_get_string( iEntity, EV_SZ_classname, szClassname, 31 );
    
    if( equal( szClassname, info_map_parameters ) ) {
        remove_entity( iEntity );
        
        return FMRES_SUPERCEDE;
    }
    
    return FMRES_IGNORED;
}

public plugin_init( ) {
    if( g_hSpawn > 0 ) {
        unregister_forward( FM_Spawn, g_hSpawn );
    }
}

Properly Removing an Entity at Plugin Start (by Alucard^)
Code:
#include <amxmodx>
#include <fakemeta>

new const g_RemoveEntity[] = "classname_here";

public plugin_precache()
{
    register_plugin("Fine", "and", "you?");
    
    // Remember to do this in plugin_precache, if you
    // do this in plugin_init( ) this will not work!
    
    register_forward(FM_Spawn, "HookFmSpawn");
}

public HookFmSpawn(iEntity)
{
    if(!pev_valid(iEntity) )
        return FMRES_IGNORED;
    
    static szClassName[32];
    pev(iEntity, pev_classname, szClassName, 31);
    
    if(equali(szClassName, g_RemoveEntity) )
    {
        engfunc(EngFunc_RemoveEntity, iEntity);

        return FMRES_SUPERCEDE;
    }
    
    return FMRES_IGNORED;
}

What is the proper way to respawn someone?
Well, there are two methods that are correct and efficient.
Code:
// GOOD, hamsandwich
ExecuteHamB( Ham_CS_RoundRespawn, playerindex )

// GOOD, fakemeta
set_pev( playerindex, pev_deadflag, DEAD_RESPAWNABLE )

// BAD, fun
// This is usable, but requires a fix that really isn't worth it.
spawn( playerindex )

// BAD, cstrike
cs_user_spawn( playerindex )

What module should I use for my large plugin? Fakemeta or engine?
You should use whatever module gets the job done. The actual efficiency impact between including either is very trivial. Don't be hesitant to include both if you need both to get what you need done properly.

I've compiled my plugin and the loose indentation warning has come up. What is this and how can I fix it?
This warning shows up whenever you indent your lines with spaces AND tabs. Conform to using one style; I personally use tabs because they're much easier.

How do I dynamically add values into my menu title / items?
Code:
menufunc()
{
    new info[128]
    formatex( info, 127, "Welcome to the Point Shop^nYour Points: %i", iVariableForPoints[id] )
    new menu = menu_create( info, "HandlerFunction" )
    
    // rest of menu code
}

How to remove properly the BuyZones in the map:

Code:
#include <amxmodx>
#include <fakemeta>

public plugin_precache()
{
    register_plugin("How", "are", "you?");

    // If i am not wrong, this must be made in plugin_precache( )
    
    new iEnt;
    
    iEnt = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_map_parameters") );
    SetKeyValue(iEnt, "buying", "3", "info_map_parameters");
    dllfunc(DLLFunc_KeyValue, iEnt, 0);
}

SetKeyValue(iEnt, const szKey[], const szValue[], const szClassName[])
{
    set_kvd(0, KV_ClassName, szClassName);
    set_kvd(0, KV_KeyName, szKey);
    set_kvd(0, KV_Value, szValue);
    set_kvd(0, KV_fHandled, 0);
    
    dllfunc(DLLFunc_KeyValue, iEnt, 0);
}

How to remove properly an entity in the map:

Code:
#include <amxmodx>
#include <fakemeta>

new const g_RemoveEntity[] = "func_push";

public plugin_precache()
{
    register_plugin("Fine", "and", "you?");
    
    // Remember to do this in plugin_precache, if you
    // do this in plugin_init( ) this will not work!
    
    register_forward(FM_Spawn, "HookFmSpawn", 1);
}

public HookFmSpawn(iEntity)
{
    if(!pev_valid(iEntity) )
        return FMRES_IGNORED;
    
    new szClassName[32];
    pev(iEntity, pev_classname, szClassName, 31);
    
    if(equali(szClassName, g_RemoveEntity) )
    {
        engfunc(EngFunc_RemoveEntity, iEntity);
        return FMRES_SUPERCEDE;
    }
    
    return FMRES_IGNORED;
}

This is really simple but, for some people that want to do something at the first spawn only, or at the first event (round start, or whatever):

Code:
#include <amxmodx>
#include <hamsandwich>

#define PLUGIN  "First Spawn"
#define AUTHOR  "Alucard"
#define VERSION "0.0.1"

new bool:FirstSpawn[33];

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR);
    RegisterHam(Ham_Spawn, "player", "HookPlrSpawn", 1);
}

public client_connect(id)
    FirstSpawn[id] = true;

public HookPlrSpawn(id)
{
    if(FirstSpawn[id])
    {
        // do stuff here, that you only want
        // to do at the first spawn
        
        FirstSpawn[id] = false;
    }
}