With the below code, specifically the CreateOneForward line, I have been having some difficulty getting the forwards to actually function correctly.
Another thing I noticed is that register_plugin seems to always return 1, which according to Bail should not happen (it should return plugin id)
The first section is the API, the second is the test plugin, and the third is the include file.
Code:
/* AMX Mod X script.
* Voting API
*
* by the AMX Mod X Development Team
* originally developed by OLO
*
* This file is part of AMX Mod X.
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*/
// Error codes:
// -1 The voting system is already in use by another plugin
// -2 Forward execution/creation error
#include <amxmodx>
#include <amxmisc>
// there are 9 menu items, -1 due to exit
#define MAX_ANSWERS 9
new g_szAnswers[MAX_ANSWERS][32]
new g_iAnswers
new g_szMenuTitle[] = "mVote"
new const g_iKeys = MENU_KEY_0|MENU_KEY_1|MENU_KEY_2|MENU_KEY_3|MENU_KEY_4|MENU_KEY_5|MENU_KEY_6|MENU_KEY_7|MENU_KEY_8|MENU_KEY_9
new g_iTally[MAX_ANSWERS]
new bool:g_bInUse
new g_iPlugin
public plugin_init()
{
g_iPlugin = register_plugin("Vote API",AMXX_VERSION_STR,"AMXX Dev Team")
register_menucmd(register_menuid(g_szMenuTitle),g_iKeys,"fnVoteTally")
register_clcmd("amx_plugin1","fnCmdPlugin")
}
public fnCmdPlugin(id)
client_print(id,print_chat,"Vote API: [%i]",g_iPlugin)
public plugin_natives()
{
register_library("vote")
register_native("vote_clear","fnVoteClear")
register_native("vote_add_answer","fnVoteAddAnswer")
register_native("vote_begin","fnVoteBegin")
}
public fnVoteClear()
{
g_iAnswers = 0
for(new iCount = 0;iCount < MAX_ANSWERS;iCount++)
g_szAnswers[iCount][0] = 0
}
public fnVoteAddAnswer(iPlugin,iParams)
{
if(g_bInUse)
return -1
new szAnswer[33]
get_string(1,szAnswer,32)
format(g_szAnswers[g_iAnswers++],31,"%s",szAnswer)
return PLUGIN_CONTINUE
}
public fnVoteBegin(iPlugin,iParams)
{
if(g_bInUse)
return -1
new szQuestion[33],Float:flTime,szFunction[33]
get_string(1,szQuestion,32)
flTime = get_param_f(2)
get_string(3,szFunction,32)
client_print(0,print_chat,"[%s][%f][%s]",szQuestion,flTime,szFunction)
g_bInUse = true
// slightly bigger in order to accomodate the actual menu
new szMenu[MAX_ANSWERS * 35],iPos
iPos += format(szMenu[iPos],(MAX_ANSWERS * 35) - iPos,"%s^n^n",szQuestion)
for(new iCount = 0;iCount < g_iAnswers;iCount++)
iPos += format(szMenu[iPos],(MAX_ANSWERS * 35) - iPos,"%i. %s^n",iCount + 1,g_szAnswers[iCount])
format(szMenu[iPos],(MAX_ANSWERS * 35) - iPos,"^n0. Exit")
show_menu(0,g_iKeys,szMenu,floatround(flTime),g_szMenuTitle)
set_task(flTime,"fnVoteFinish",iPlugin,szFunction,32)
return PLUGIN_CONTINUE
}
public fnVoteFinish(szFunction[],iPlugin)
{
client_print(0,print_chat,"szFunction: [%s], iPlugin: [%i]",szFunction,iPlugin)
g_bInUse = false
fnVoteClear
new iVoteEnd = CreateOneForward(iPlugin,szFunction,FP_ARRAY)
client_print(0,print_chat,"iVoteEnd: [%i]",iVoteEnd)
new pTally = PrepareArray(g_iTally,MAX_ANSWERS)
client_print(0,print_chat,"ExecuteForward: [%i]",ExecuteForward(iVoteEnd,pTally))
return PLUGIN_CONTINUE
}
public fnVoteTally(id,iKey)
{
if(iKey == 9 || iKey > g_iAnswers)
return PLUGIN_CONTINUE
g_iTally[iKey]++
new szName[33]
get_user_name(id,szName,32)
client_print(0,print_chat,"[AMXX] %s voted for %s.",szName,g_szAnswers[iKey])
return PLUGIN_CONTINUE
}
Code:
#include <amxmodx>
#include <amxmisc>
#include <vote>
new g_iPlugin
public plugin_init()
{
g_iPlugin = register_plugin("Vote API Test","1.0","Hawk552")
register_clcmd("vote_add_answer","fnAddAnswer")
register_clcmd("vote_begin","fnBegin")
register_clcmd("amx_plugin2","fnCmdPlugin")
}
public fnCmdPlugin(id)
client_print(id,print_chat,"Vote Test: [%i]",g_iPlugin)
public fnAddAnswer(id)
{
new szArg[33]
read_argv(1,szArg,32)
vote_add_answer(szArg)
}
public fnBegin(id)
{
new szArg[2][33]
read_argv(1,szArg[0],32)
read_argv(2,szArg[1],32)
client_print(0,print_chat,"Vote begin ret: [%i]",vote_begin(szArg[0],floatstr(szArg[1]),"fnVoteEnd"))
}
public fnVoteEnd(iTally[9])
client_print(0,print_chat,"[%i] [%i] [%i] [%i]",iTally[0],iTally[1],iTally[2],iTally[3])