CastVoteAsync
Casts a vote for a specific Sensei poll.
Method Signature
public async Task<SenseiPollCastVoteResponse?> CastVoteAsync(string pollId, string response)
Parameters
Parameter | Type | Description |
---|---|---|
pollId | string | The ID of the poll to vote on (obtained from GetActivePollAsync ) |
response | string | The 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
Property | Type | Description |
---|---|---|
Ok | bool | Indicates whether the vote was successfully cast |
Data.Id | string | Unique identifier for this vote record |
Data.PollId | string | The poll this vote was cast for |
Data.UserId | string | The user who cast the vote |
Data.Response.Value | string | The vote response that was submitted |
Data.CreatedAt | DateTime | When the vote was first created |
Data.UpdatedAt | DateTime | When the vote was last updated |
Message | string | Response 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'sOptions
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