Skip to main content

CastVoteAsync

Casts a vote for a specific Sensei poll.

Method Signature

public async Task<SenseiPollCastVoteResponse?> CastVoteAsync(string pollId, string response)

Parameters

ParameterTypeDescription
pollIdstringThe ID of the poll to vote on (obtained from GetActivePollAsync)
responsestringThe user's response/vote (must match one of the poll's available options)

Returns

Returns a SenseiPollCastVoteResponse object containing the vote confirmation, or null if the request failed.

Usage Example

// First get the active poll
string personaId = "your-persona-id";
var poll = await playSafeManager.GetActivePollAsync(personaId);

if (poll != null && poll.Ok && poll.Data != null)
{
// Cast a vote
string pollId = poll.Data.Id;
string userResponse = "yes"; // Must match one of the poll's options

var voteResult = await playSafeManager.CastVoteAsync(pollId, userResponse);

if (voteResult != null && voteResult.Ok)
{
Debug.Log($"Vote cast successfully!");
Debug.Log($"Vote ID: {voteResult.Data.Id}");
Debug.Log($"User response: {voteResult.Data.Response.Value}");
Debug.Log($"Voted at: {voteResult.Data.CreatedAt}");
}
}

Response Type

SenseiPollCastVoteResponse

public class SenseiPollCastVoteResponse
{
public bool Ok { get; set; }
public SenseiPollCastVoteData Data { get; set; }
public string Message { get; set; }
}

SenseiPollCastVoteData

public class SenseiPollCastVoteData
{
public string Id { get; set; }
public string PollId { get; set; }
public string UserId { get; set; }
public SenseiPollCastVoteResponseValue Response { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}

SenseiPollCastVoteResponseValue

public class SenseiPollCastVoteResponseValue
{
public string Value { get; set; }
}

Properties

PropertyTypeDescription
OkboolIndicates whether the vote was successfully cast
Data.IdstringUnique identifier for this vote record
Data.PollIdstringThe poll this vote was cast for
Data.UserIdstringThe user who cast the vote
Data.Response.ValuestringThe vote response that was submitted
Data.CreatedAtDateTimeWhen the vote was first created
Data.UpdatedAtDateTimeWhen the vote was last updated
MessagestringResponse message from the server

Error Handling

The method returns null if:

  • Network request fails
  • Poll ID doesn't exist
  • Response doesn't match available options
  • User has already voted on this poll
  • Poll has expired
  • Response parsing fails

Always check for null and verify the Ok property before accessing the data.

Notes

  • The response parameter must exactly match one of the options from the poll's Options array
  • Users can typically only vote once per poll
  • The method automatically uses the current player's user ID from telemetry data
  • Votes are permanent and cannot be changed once submitted
  • Check poll expiration before attempting to vote