Unity Subscribe to Events code examples need changes.
On the Unity Getting Started (https://dev.twitch.tv/docs/game-engine-plugins/unity-guide/) page, under Subscribe to Events, the examples might be a bit inaccurate. Here is the code for Channel Point Rewards:
GameTask<EventStream<CustomRewardEvent>> CustomRewardEvents;
start()
{
CustomRewardEvents = SubscribeToCustomRewardEvents();
}
update()
{
var CurRewardEvent;
CustomRewardEvents.TryGetNextEvent(out CurRewardEvent);
if(CurRewardEvent != null)
{
//Do something
Debug.Log("{CurRewardEvent.RedeemerName} has brought {CurRewardEvent.CustomRewardTitle} for {CurRewardEvent.CustomRewardCost}!");
}
}
Issues:
- void Start(), void Update().
- SubscribeToCustomRewardEvents() is a member of 'Twitch.API'. So it should be 'Twitch.API.SubscribeToCustomRewardEvents()'.
- 'var' variables are implicitely typed, so the line 'var CurRewardEvent;' throws an erorr. 'CustomRewardEvent CurRewardEvent;' would work.
- CustomRewardEvents task doesn't have TrgGetNextEvent() or WaitForEvent() methods. To access those methods you have to use 'CustomRewardEvents.MaybeResult'. So it should be 'CustomRewardEvents.MaybeResult.TryGetNextEvent()'.
These issues are present in the Follows, Subscribers, HypeTrain and Raids code examples a well.
-
abhinav_demkeys
commented
I'm not sure if I should be adding this here or creating a new post for this but it's relating to SubscribeToCustomRewardEvents() so I'm gonna post it here and hope one of the devs sees it. In my original post I mentioned "SubscribeToCustomRewardEvents() is a member of 'Twitch.API'. So it should be 'Twitch.API.SubscribeToCustomRewardEvents()'.". This is true, however, in order to receive any events, you need to create an instance of TwitchSDKApi and call SubscribeToCustomRewardEvents() from that instance. Just calling 'Twitch.API.SubscribeToCustomRewardEvents()' doesn't give you any redeems. Sorry about the confusion. Here's my suggested version.
TwitchSDKApi api;
GameTask<EventStream<CustomRewardEvent>> customRewards;void Start()
{
api = new TwitchSDKApi(""); // Add your own ClientID here.
customRewards = api.SubscribeToCustomRewardEvents();
}void Update()
{
// If the 1 key is pressed.
if(Input.GetKeyDown(KeyCode.Alpha1))
{
CustomRewardEvent curReward;
// TryGetNextEvent returns a bool so we can use it
// in a if condition.
if(customRewards.MaybeResult.TryGetNextEvent(out curReward))
{
Debug.Log(curReward.CustomRewardTitle);
}
}
} -
MatisseTec
commented
This really needs to be done, the docs for the unity c# stuff is a bit broken without this knowledge