AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Problem with vote menu (https://forums.alliedmods.net/showthread.php?t=135407)

t3hNox 08-15-2010 17:39

Problem with vote menu
 
I customized the advanced vote menu from menus tutorial. For some reason it doesn't show up. I guess the problem is in the way menu items are added (with loop). Any ideas ?
Code:
#include <amxmodx> #include <amxmisc> //How many different votes there will be in the menu #define MAXVOTE 5 new gVoteMenu; new gVotes[MAXVOTE]; new gVoting; new Winner new const CHOICE[MAXVOTE][] = {     "Item #1",     "Item #2",     "Item #3",     "Item #4",     "Item #5" } new const CONDITION[MAXVOTE][] = {     "Condition #1",     "Condition #2",     "Condition #3",     "Condition #4",     "Condition #5" } public plugin_init() {     register_plugin("Advanced Vote", "0.1", "n0XX`")     register_clcmd( "say /vote","TheVote");     register_clcmd( "say /winner","WhoWon") } public WhoWon(id) {     if(Winner)         client_print(id, print_chat, "The Winner is %s", CHOICE[Winner-1])     else         client_print(id, print_chat, "Nothing")             return PLUGIN_HANDLED } public TheVote() {     new players[32], pnum, tempid;     get_players(players, pnum, "ac");     for( new i; i<pnum; i++ )     {         tempid = players[i];         {             StartVote(tempid)             client_print(tempid, print_chat, "Menu sent.")  //Only to make sure.         }     } } public StartVote(id) {     if( gVoting )     {         client_print(id, print_chat, "There is already a vote going.");         return PLUGIN_HANDLED;     }         gVoteMenu = menu_create("\rMake a selection!", "menu_handler");         //Here you can do whatever you want to add your voteids.     //We are going to use players for the example     gVoting = 1;         new iNum = 1         for(new i = 0; i < MAXVOTE; i++)     {             new menuitem[21]         formatex(menuitem, 20, "\w%s", CHOICE[0])         menu_additem(gVoteMenu, menuitem, "iNum", 0);         iNum++     }         set_task(10.0, "EndVote");         return PLUGIN_HANDLED; } public menu_handler(id, menu, item) {     //If the menu was exited or if there is not a vote     if( item == MENU_EXIT || !gVoting )     {         return PLUGIN_HANDLED;     }         new data[6], iName[64];     new access, callback;     menu_item_getinfo(menu, item, access, data,5, iName, 63, callback);         //Get the voteid number that was selected     new voteid_num = str_to_num(data);         gVotes[voteid_num]++         return PLUGIN_HANDLED; } public EndVote() {     //This will hold how many different votes were selected     new votes_select;         //This will hold the top 3 votes     new votes[3];         //This will hold the top 3 selected voteids     new voteid[3];         new i, j;         //Loop through all the voteids     for( i=0; i<MAXVOTE; i++ )     {         //If the voteid recieved any votes         if( gVotes[i] )         {             //If we are still trying to get the top 3             if( votes_select < 3 )             {                 //Save the data for the current voteid selected                 votes[votes_select] = gVotes[i];                 voteid[votes_select] = i;                                 //Go to the next voteid that might have been selected                 votes_select++;             }             else             {                 //Loop through all the top votes, replace any that are lower than the selected voteid                 for( j=0; j<3; j++ )                 {                     //If this one recieved less votes                     if( votes[j] < gVotes[i] )                     {                         //Change the data to the voteid with more votes                         votes[j] = gVotes[i];                         voteid[j] = i;                                                 //Don't need to bother looking for more                         break;                     }                 }             }         }     }         //If noone voted     if( !votes_select )     {         client_print(0, print_chat, "CRICKEY! No one voted!");     }     //Else if one voteid recieved all the votes     else if( votes_select == 1 )     {         //Give it to the voteid         SelectWin( voteid[0] );     }     //Else if two different voteids recieved all the votes     else if( votes_select == 2 )     {         //If they recieved even votes         if( votes[0] == votes[1] )         {             //Give it to a random one             client_print(0, print_chat, "Vote has tied. Choosing random from tied votes." );             SelectWin( voteid[ random(2) ] );         }                 //Else if the first recieved the most         else if( votes[0] > votes[1] )             //Give it to the first         SelectWin( voteid[0] );                 //Else the second recieved the most         else         //Give it to the second         SelectWin( voteid[1] );     }     //Else there were at least 3 different votes     else     {         //Here you might want to do run-off voting, but well just select a random one         client_print(0, print_chat, "Could not determine a winner. Selecting random." );         SelectWin( voteid[ random(3) ] );     }         menu_destroy(gVoteMenu);     gVoting = 0; } stock SelectWin(iWin) {     Winner = iWin     client_print(0, print_chat, "The winner is %s", CHOICE[iWin - 1])     set_task(5.0, "Tell", 0) } public Tell() {     switch(Winner)     {         case 1-MAXVOTE:         {             client_print(0, print_chat, "Conditions: %s", CONDITION[Winner-1])         }     } }
I guess everything else should be fine as in other parts of code were made only minor changes.

fysiks 08-15-2010 17:46

Re: Problem with vote menu
 
You have to display the menu with menu_display(). You may need to loop through players like it's shown in the examples.

Also, your menu won't work because of "iNum". You should look at how it's done in the menus in the tutorials using num_to_str().

t3hNox 08-15-2010 18:08

Re: Problem with vote menu
 
I done corrections where you said, only that player loop part remained (for now).
Code:
#include <amxmodx> #include <amxmisc> //How many different votes there will be in the menu #define MAXVOTE 5 new gVoteMenu; new gVotes[MAXVOTE]; new gVoting; new Winner new const CHOICE[MAXVOTE][] = {     "Item #1",     "Item #2",     "Item #3",     "Item #4",     "Item #5" } new const CONDITION[MAXVOTE][] = {     "Condition #1",     "Condition #2",     "Condition #3",     "Condition #4",     "Condition #5" } public plugin_init() {     register_plugin("Advanced Vote", "0.1", "n0XX`")     register_clcmd( "say /vote","TheVote");     register_clcmd( "say /winner","WhoWon") } public WhoWon(id) {     if(Winner)         client_print(id, print_chat, "The Winner is %s", CHOICE[Winner-1])     else         client_print(id, print_chat, "Nothing")             return PLUGIN_HANDLED } public TheVote() {     new players[32], pnum, tempid;     get_players(players, pnum, "ac");     for( new i; i<pnum; i++ )     {         tempid = players[i];         {             menu_display ( tempid, gVoteMenu, 0 )             client_print(tempid, print_chat, "Menu sent.")  //Only to make sure.         }     } } public StartVote(id) {     if( gVoting )     {         client_print(id, print_chat, "There is already a vote going.");         return PLUGIN_HANDLED;     }         gVoteMenu = menu_create("\rSelect a day !", "menu_handler");         //Here you can do whatever you want to add your voteids.     //We are going to use players for the example     gVoting = 1;         new szNum[10], voteid         for(new i = 0; i < MAXVOTE; i++)     {         num_to_str(voteid, szNum, 9);         new menuitem[21]         formatex(menuitem, 20, "\w%s", CHOICE[i])         menu_additem(gVoteMenu, menuitem, szNum, 0);         voteid++     }         set_task(10.0, "EndVote");         return PLUGIN_HANDLED; } public menu_handler(id, menu, item) {     //If the menu was exited or if there is not a vote     if( item == MENU_EXIT || !gVoting )     {         return PLUGIN_HANDLED;     }         new data[6], iName[64];     new access, callback;     menu_item_getinfo(menu, item, access, data,5, iName, 63, callback);         //Get the voteid number that was selected     new voteid_num = str_to_num(data);         gVotes[voteid_num]++         return PLUGIN_HANDLED; } public EndVote() {     //This will hold how many different votes were selected     new votes_select;         //This will hold the top 3 votes     new votes[3];         //This will hold the top 3 selected voteids     new voteid[3];         new i, j;         //Loop through all the voteids     for( i=0; i<MAXVOTE; i++ )     {         //If the voteid recieved any votes         if( gVotes[i] )         {             //If we are still trying to get the top 3             if( votes_select < 3 )             {                 //Save the data for the current voteid selected                 votes[votes_select] = gVotes[i];                 voteid[votes_select] = i;                                 //Go to the next voteid that might have been selected                 votes_select++;             }             else             {                 //Loop through all the top votes, replace any that are lower than the selected voteid                 for( j=0; j<3; j++ )                 {                     //If this one recieved less votes                     if( votes[j] < gVotes[i] )                     {                         //Change the data to the voteid with more votes                         votes[j] = gVotes[i];                         voteid[j] = i;                                                 //Don't need to bother looking for more                         break;                     }                 }             }         }     }         //If noone voted     if( !votes_select )     {         client_print(0, print_chat, "CRICKEY! No one voted!");     }     //Else if one voteid recieved all the votes     else if( votes_select == 1 )     {         //Give it to the voteid         SelectWin( voteid[0] );     }     //Else if two different voteids recieved all the votes     else if( votes_select == 2 )     {         //If they recieved even votes         if( votes[0] == votes[1] )         {             //Give it to a random one             client_print(0, print_chat, "Vote has tied. Choosing random from tied votes." );             SelectWin( voteid[ random(2) ] );         }                 //Else if the first recieved the most         else if( votes[0] > votes[1] )             //Give it to the first         SelectWin( voteid[0] );                 //Else the second recieved the most         else         //Give it to the second         SelectWin( voteid[1] );     }     //Else there were at least 3 different votes     else     {         //Here you might want to do run-off voting, but well just select a random one         client_print(0, print_chat, "Could not determine a winner. Selecting random." );         SelectWin( voteid[ random(3) ] );     }         menu_destroy(gVoteMenu);     gVoting = 0; } stock SelectWin(iWin) {     Winner = iWin     client_print(0, print_chat, "The winner is %s", CHOICE[iWin - 1])     set_task(5.0, "Tell", 0) } public Tell() {     switch(Winner)     {         case 1-MAXVOTE:         {             client_print(0, print_chat, "Conditions: %s", CONDITION[Winner-1])         }     } }

I get this error when I type "/vote":
Code:

L 08/16/2010 - 00:57:20: Invalid menu id 8(9)
L 08/16/2010 - 00:57:20: [AMXX] Displaying debug trace (plugin "zmenu.amxx")
L 08/16/2010 - 00:57:20: [AMXX] Run time error 10: native error (native "menu_display")
L 08/16/2010 - 00:57:20: [AMXX]    [0] zmenu.sma::TheVote (line 62)

This is the line where the error occurs:
Code:

menu_display ( tempid, gVoteMenu, 0 )

fysiks 08-15-2010 19:31

Re: Problem with vote menu
 
Look at the example in the new menus tutorial and do it like it is done there.

Mxnn 08-15-2010 19:32

Re: Problem with vote menu
 
In "TheVote()" you are shown a menu that wasn't created yet.

t3hNox 08-16-2010 12:07

Re: Problem with vote menu
 
Ok, thanks for the replies, I got everything working (though I had no chance to test the vote functionality with more than 2 players but hopefully it's working fine).
Only thing that is not working properly is closing the menu. When those 10 seconds are over and a player didn't vote, the menu is not closed. Any ideas ?

Well I could use this:
Code:

client_cmd(id, "slot10")
..but that's just.. wrong :)

Mxnn 08-16-2010 13:38

Re: Problem with vote menu
 
Try to put instead of
PHP Code:

set_task(10.0"EndVote"

PHP Code:

set_task(10.0"CloseVote")

public 
CloseVote()
         
menu_destroy(gVoteMenu); 


t3hNox 08-16-2010 13:53

Re: Problem with vote menu
 
It seems that it doesn't matter where I put menu_destroy(gVoteMenu); it sill doesn't work and show error:
Code:

L 08/16/2010 - 20:47:55: Invalid menu id 8(9)
L 08/16/2010 - 20:47:55: [AMXX] Displaying debug trace (plugin "menu.amxx")
L 08/16/2010 - 20:47:55: [AMXX] Run time error 10: native error (native "menu_destroy")
L 08/16/2010 - 20:47:55: [AMXX]    [0] menu.sma::KillMenu (line 109)


fysiks 08-17-2010 00:58

Re: Problem with vote menu
 
You cannot actually remove the menu from the player's screen.

t3hNox 08-17-2010 03:09

Re: Problem with vote menu
 
I am looking for a method like in the default amxx vote menus - they closes automatically after some time.
I guess I will have to take a look at those menu codes.


All times are GMT -4. The time now is 21:57.

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