Left 4 Voting 2
Left 4 Dead 2 has a VGUI voting system controller by a bunch of Events and UserMessages. You can use either a string from the resource file, or L4D_TargetID_Player which will let you create any vote you want.
Contents
How voting works
- A client issues a callvote with the vote type and argument.
- The VoteStart User Message is sent.
- Clients use the "Vote" command to register their votes ("Yes" or "No"), after which the server sends a VoteRegistered UserMessage to that player to acknowledge their vote. A vote_changed event is sent to all players with updated numbers.
- When the vote is complete, the server sends either a VotePass or VoteFail UserMessage.
Console Commands
Vote
Note: This command is only valid when a vote is ongoing.
| Name: | Vote | |||
| Structure: |
|
User Messages
Note: These user messages use unsigned bytes for the team number. Since Valve represents no team as -1, no team is instead represented as its two's complement, 255.
VoteStart
Note: Sent to all players in the vote group, corresponding with teams. The default implementation also sends it to bots.
| Name: | VoteStart | |||||||||||||||
| Structure: |
|
VoteRegistered
Note: Only sent to player who voted
| Name: | VoteRegistered | |||
| Structure: |
|
VotePass
Note: Sent to all players after a vote passes.
| Name: | VotePass | |||||||||
| Structure: |
|
VoteFail
Note: Sent to all players after a vote fails.
| Name: | VoteFail | |||
| Structure: |
|
Events
Note: team is -1 when sent to all players)
vote_changed
| Name: | vote_changed | |||||||||
| Structure: |
|
Example voting plugin
This is a basic plugin that starts a vote, "Is gaben fat?". It does not ensure the same client does not vote multiple times, nor does it actually kick the user.
#include <sourcemod>
#define L4D2_TEAM_ALL -1
new yesvotes;
new novotes;
#define MAX_VOTES 4
public OnPluginStart()
{
RegConsoleCmd("testvote",Callvote_Handler);
RegConsoleCmd("Vote",vote);
}
public Action:Callvote_Handler(client, args)
{
new Handle:bf = StartMessageAll("VoteStart", USERMSG_RELIABLE);
BfWriteByte(bf, L4D2_TEAM_ALL);
BfWriteByte(bf, 0);
BfWriteString(bf, "#L4D_TargetID_Player");
BfWriteString(bf, "Is gaben fat?");
BfWriteString(bf, "Server");
EndMessage();
yesvotes = 0;
novotes = 0;
UpdateVotes();
return Plugin_Handled;
}
public UpdateVotes()
{
new Handle:msg = CreateEvent("vote_changed");
SetEventInt(msg,"yesVotes",yesvotes);
SetEventInt(msg,"noVotes",novotes);
SetEventInt(msg,"potentialVotes",MAX_VOTES);
FireEvent(msg);
if (yesvotes+novotes == MAX_VOTES)
{
PrintToServer("voting complete!");
if (yesvotes > novotes)
{
new Handle:bf = StartMessageAll("VotePass");
BfWriteByte(bf, L4D2_TEAM_ALL);
BfWriteString(bf, "#L4D_TargetID_Player");
BfWriteString(bf, "Gaben is fat");
EndMessage();
}
else
{
new Handle:bf = StartMessageAll("VoteFailed");
BfWriteByte(bf, L4D2_TEAM_ALL);
EndMessage();
}
}
}
public Action:vote(client, args)
{
new String:arg[8];
GetCmdArg(1,arg,8);
PrintToServer("Got vote %s from %i",arg,client);
if (strcmp(arg,"Yes",true) == 0)
{
yesvotes++;
}
else if (strcmp(arg,"No",true) == 0)
{
novotes++;
}
UpdateVotes();
return Plugin_Continue;
}
See Also
- Left 4 Voting
- TF2 Voting
- BuiltinVotes, a SourceMod extension that exposes a voting API that uses this voting system.