The poll system
Since mods that create a poll and let users vote on events are popular, a poll system exists in tapi. the idea being to queue polls in an orderly manner, and avoid instances where polls clash or cover each other.
generally speaking, there will be 3 tapi components you will be using. "MainPollController", handles poll behavior, used for submitting polls. "Poll", a class for storing poll information. Each poll class contains instances of the "VoteOption" class, which represnt things you can vote for.
The poll class:
contains your basic poll info. Can be submitted to "mainPollController" to enqueue a new poll.
has the following properties
public int time; //the time, in seconds, a poll runs for.
public string name; //internal name
public string title; //poll title to be displayed above the options.
public List options; //an array of "VoteOptions". that can be voted for.
public Action callBack = null; //An action that gets called when the poll concludes. If null, skips the call. Passes Poll type as parameter, usually itself.
"public resolvePollOptions resolveSettings;" //each voteOption may have its own callback. At the end of the poll, the winning vote callback will be called if not null. This setting allows you to resolve ties, or call none of vote callbacks.
public enum resolvePollOptions
{
mainOnly = 0, //only calls the callback of the poll. Useful for resolving edge cases. Remember each poll contains its own options
randomIfTie = 1,//in case multiple options tie, choose one at random
noneIfTie = 2,//if there is a tie, call only main poll callback
AllIfTie = 3//call all option callback
}
remember that the main poll callback is ALWAYS called, and always happens before the automatic VoteOption callbacks.
The poll class has the following constructors
public Poll(int time, List options, string title, Action callBack,resolvePollOptions resolveSettings ,string name = null)
makes a new poll
public Poll(Poll poll)
makes a copy of a poll
VoteOption class: represents an option users can vote for. Has the following properties
public string displayText;//text displayed to the voters
public int votes; //vote counter
private Action callBack = null;// callback which happens if vote wins. Passes VoteOption as paramaeter, usually itself.
MainPollController.
this is how you submit polls. You can access it by MainPollController.Instance
mostly used with the "SubmitPoll(Poll)" method example
MainPollController.instance.SubmitPoll(poll);
There are a couple things to note. When submitting a poll, youre putting it into a queue. There may be another poll in progress, or it may be on a short coolDown. The poll does not instantly start.
SubmitPoll returns a boolean value. If it returns false it means it failed and did NOT enqueue your poll.
usually this happens because it tries to validate your poll. If you poll a.is null, b.has no vote options or c. vote options have no display text.
another important thing to note, is that submit poll does not enqueue your submitted poll object, but a COPY of your poll object. This copy will be sanitized in the following ways to not break ui/user exprience. a.any vote options with text more than 50 characters will be reduced to 50 characters
b.any newline charactes will be replaced with empty character
c. if your poll has more than 4 vote options, all but the first 4 will be removed.
please try to keep your polls and vote options valid as to not encounter unexpected results.
Last updated
Was this helpful?