Hello! I'm very new to sourcemod and just passed through these tutorials:
https://wiki.alliedmods.net/Introduc...rceMod_Plugins
https://wiki.alliedmods.net/Writing_Extensions
However, I still have some questions which I did not find answers to.
Code from the tutorial (
extension.cpp):
PHP Code:
#include "extension.h"
/**
* @file extension.cpp
* @brief Implement extension code here.
*/
FirstExtension g_FirstExtension; /**< Global singleton for extension's main interface */
SMEXT_LINK(&g_FirstExtension);
IForward *g_pSayChat = NULL;
cell_t SquareNumber(IPluginContext *pContext, const cell_t *params)
{
cell_t number = params[1];
return number * number;
}
const sp_nativeinfo_t MyNatives[] =
{
{ "SquareNumber", SquareNumber },
{ NULL, NULL },
};
void FirstExtension::SDK_OnAllLoaded()
{
sharesys->AddNatives(myself, MyNatives);
g_pSayChat = forwards->CreateForward("OnPlayerSayChat", ET_Event, 2, NULL, Param_Cell, Param_String);
}
void FirstExtension::SDK_OnUnload()
{
forwards->ReleaseForward(g_pSayChat);
}
/** Fires the say chat event in plugins. Returns true to allow the text, false to block. */
bool FireChatEvent(int client, const char *text)
{
if (!g_pSayChat)
{
return true;
}
cell_t result = 0;
g_pSayChat->PushCell(client);
g_pSayChat->PushString(text);
g_pSayChat->Execute(&result);
if (result == Pl_Handled)
{
return false;
}
return true;
}
I don't understand, when exactly
FireChatEvent gets called?
I tried to include this simple extension in tutorial plugin's code:
first_ext.inc:
PHP Code:
#if defined _firstext_included
#endinput
#endif
#define _firstext_included
/**
* Returns the square of a number.
*
* @param num Number to square.
* @return The square of num.
*/
native int SquareNumber(num);
/**
* @brief Called whenever a client says something in chat.
*
* @param client Index of the client.
* @param text String containing the say text.
* @return Plugin_Handled to block text from printing, Plugin_Continue otherwise.
*/
forward Action OnPlayerSayChat(int client, const char[] text);
/**
* Do not edit below this line!
*/
public Extension:__ext_firstext =
{
name = "First extension",
file = "first.ext.2.csgo",
#if defined AUTOLOAD_EXTENSIONS
autoload = 1,
#else
autoload = 0,
#endif
#if defined REQUIRE_EXTENSIONS
required = 1,
#else
required = 0,
#endif
};
helloworld.sp:
PHP Code:
#include <sourcemod>
#include <sdktools>
#include <first_ext>
public Plugin myinfo =
{
name = "My First Plugin",
author = "Me",
description = "My first plugin ever",
version = "1.0",
url = "http://www.sourcemod.net/"
};
ConVar g_cvarMySlapDamage = null;
public void OnPluginStart()
{
PrintToServer("Hello world!");
RegAdminCmd("sm_myslap", Command_MySlap, ADMFLAG_SLAY);
g_cvarMySlapDamage = CreateConVar("sm_myslap_damage", "1", "Default slap damage");
AutoExecConfig(true, "plugin_myslap");
LoadTranslations("common.phrases.txt"); // Required for FindTarget fail reply
}
public Action OnPlayerSayChat(int client, const char[] text)
{
PrintToChat(client, "[SM] %d", SquareNumber(5));
return Plugin_Continue;
}
REQUIRE_EXTENSIONS is defined in
core.inc and my plugin loads successfully so I assume that the extension is also loaded. However, when I say something in chat I don't get "[SM] 25" printed as I expected.
So, either `OnPlayerSayChat` is never fired (in that case, what sould I do to actually get it working?) or it is fired and I just made some mistakes so I don't get expected result. If latter, what are these mistakes and how to fix them?
Thanks!