Example of simple mod that uses polls
simple mod example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TwitchAPI;
using TwitchAPI.Polls;
using UnityEngine;
namespace Mod
{
//this is added to a component to the game manager instance
internal class OccasioanlPollSubmitter :MonoBehaviour
{
void Start()
{
//subscribe to notification to handle turning off polls
TwitchAPI.TwitchAPI.GlobalToggleStatusNotification += tapiToggleHandler;
//creates a random vote option pool
options.Add(new VoteOption("Give glass guon stone",GivePlayerGlass));
options.Add(new VoteOption("for each vote, heal once",HealPlayer));
options.Add(new VoteOption("5 damage",DamagePlayer));
options.Add(new VoteOption("Do nothing",DoNothing));
options.Add(new VoteOption("spawn ammo",SpawnAmmo));
options.Add(new VoteOption("spawn enemey",SpawnBulletKin));
options.Add(new VoteOption("for each vote, give 1 casing",GivePlayerCoin));
}
void SubmitPoll()
{
if (GameManager.Instance.PrimaryPlayer && !GameManager.Instance.IsFoyer)
{
List<VoteOption> optionLoterry = new List<VoteOption>();
for (int i = 0; i < 4; i++)
{
optionLoterry.Add(options[UnityEngine.Random.Range(0, options.Count)]);
}
Poll poll = new Poll(30, optionLoterry, "an example poll", PollExampleCallBack, resolvePollOptions.randomIfTie);
MainPollController.instance.SubmitPoll(poll);
}
}
//main callback. Can be used to handle generic poll conclusion, or edge cases
void PollExampleCallBack(Poll poll)
{
//gets winning votes from a poll. Can be used to handle conlusions any way we want
List<VoteOption> winners = MainPollController.FindWinningVotes(poll);
//since we just use the automatic random winner, we will ignore it and just spawn some particles
Vector2 pos = GameManager.Instance.PrimaryPlayer.CenterPosition;
GlobalSparksDoer.DoRadialParticleBurst(30, pos, pos, 1, 1, 1, null, null, null, GlobalSparksDoer.SparksType.FLOATY_CHAFF);
}
List<VoteOption> options = new List<VoteOption>();
void tapiToggleHandler(bool toggleState)
{
//submits a poll every 60 seconds. Stops the submitions when tapi is toggled off
if (toggleState)
InvokeRepeating("SubmitPoll", 10f, 60f);
else
CancelInvoke("SubmitPoll");
}
//various small effects
void GivePlayerGlass(VoteOption vote) {
Vector2 position = GameManager.Instance.PrimaryPlayer.CenterPosition;
LootEngine.SpawnItem(PickupObjectDatabase.GetById(565).gameObject, position, Vector2.zero, 0f);
}
void HealPlayer(VoteOption vote)
{ GameManager.Instance.PrimaryPlayer.healthHaver.ApplyHealing((float)vote.votes); }
void DamagePlayer(VoteOption vote)
{ GameManager.Instance.PrimaryPlayer.healthHaver.ApplyDamage(2f, Vector2.zero, "poll"); }
void GivePlayerCoin(VoteOption vote) {
Vector2 position = GameManager.Instance.PrimaryPlayer.CenterPosition;
for (int i = 0; i < vote.votes; i++)
LootEngine.SpawnCurrency(position + UnityEngine.Random.insideUnitCircle.normalized, 1);
}
void GivePlayerArmor(VoteOption vote) {
GameManager.Instance.PrimaryPlayer.healthHaver.Armor += 3;
}
void SpawnAmmo(VoteOption vote) {
Vector2 position = GameManager.Instance.PrimaryPlayer.CenterPosition;
LootEngine.SpawnItem(PickupObjectDatabase.GetById(78).gameObject, position, Vector2.zero,0f);
}
void SpawnBulletKin(VoteOption vote) {
string[] a = { "bullet_kin" };
ETGModConsole.Spawn(a);
}
}
}
Last updated
Was this helpful?