# Unreal Getting Started C++

Integrating Twitch into an Unreal game using C++

Source: https://dev.twitch.tv/docs/game-engine-plugins/unreal-guide-cpp/

## Before you begin

Ensure that you are on Unreal Version 5.2 or later (Earlier versions will require you to build your own version of the plugin from our base sdk code) The base sdk is available upon request.

In your Projects base folder create a `Plugins` folder (if it does not already exist). In that folder create a `TwitchSDK` folder then unzip everything contained in UnrealTwitchSDK.zip

Note: If you place the plugin into the base Plugins folder but do not create a subfolder for the Twitch plugin, Unreal will not correctly generate the sln file and you will get errors when trying to add the TwitchSDK module.

Then find your project's .build.cs file and add the following:

```cpp
PrivateDependencyModuleNames.Add("TwitchSDK");
```

Next the Unreal plugin requires C++17, you will also need to add the following declaration if you haven't already:

```cpp
CppStandard = CppStandardVersion.Cpp17;
```

Finally go to your Project Settings and navigate to the Plugins subsection and select Twitch. In that submenu enter your `Client ID`.

![Unreal Plugin Settings image](https://dev.twitch.tv/docs/assets/images/pluginsettings.png)

## Authentication

The first step in any Twitch integration is Authentication. 
During Authentication users give access to particular features of their account via  `OAuthScopes` are requested. Please refer to [Scopes](https://dev.twitch.tv/docs/authentication/scopes.md) to learn more about which scopes you should select.
Once you have determined which scopes you need call `TwichSDK::BuildOAuthScopes` with the appropriate scopes to request what you want to interact with. 
Finally once the response has been obtained either show the user the URL they must navigate to, or provide a button which will directly take them there. Checking the `TwitchSDK::AuthenticationInfo` object you will be able to determine if the user is already logged in or not.

In this AuthActorComponent example we are requesting the ability to manage broadcasts and Polls. If you wanted to also be able to modify Predictions you would need to add `ChannelManagePredictions` to the `BuildOAuthScopes` function.

```cpp
void UAuthActorComponent::BeginPlay()
{
	Super::BeginPlay();

	auto core = FModuleManager::GetModuleChecked<FTwitchSDKModule>("TwitchSDK").Core;
	core->GetAuthenticationInfo(
		TwitchSDK::BuildOAuthScopes({ FTwitchSDKOAuthScope::ChannelManageBroadcast, FTwitchSDKOAuthScope::ChannelManagePolls }),
		[](const TwitchSDK::AuthenticationInfo& info) {
			if (info.UserCode.size() == 0)
			{
				UE_LOG(LogTemp, Warning, TEXT("The user is already authenticated"));
			}
			else
			{
				UE_LOG(LogTemp, Warning, TEXT("The user should authenticate at %s with code %s"), info.Uri.data(), info.UserCode.data());
			}
		},
		[](const std::exception& e) {
			UE_LOG(LogTemp, Warning, TEXT("GetAuthenticationInfo failed: %s"), UTF8_TO_TCHAR(e.what()));
		}
	);
}
```

## Event Subscriptions

All event subscriptions are handled in the same way in the Unreal plugin.
First you must create a subscription for your subscription, we suggest using `auto stream` as the container for this.
Then at a later point in your code you call the wait for event function on the same stream object. `stream.WaitForEvent([](Type of Subscription))`

Below are various examples

### Subscribe to Channel Subscriptions

#### Create the Subscription

```cpp
auto stream = core->SubscribeToChannelSubscribeEvents();
```
#### Wait for Events

```cpp
stream.WaitForEvent([](const TwitchSDK::ChannelSubscribeEvent& e) {
	auto userDisplayName = TwitchSDK::ToFString(e.UserDisplayName);
	auto message = FString::Printf(TEXT("%s just subscribed!"), *userDisplayName);

	//print message to screen or do something interesting
});
```

### Subscribe to Channel Follows

#### Create the Subscription

```cpp
auto stream = core->SubscribeToChannelFollowEvents();
```

#### Wait for Events

```cpp
stream.WaitForEvent([](const TwitchSDK::ChannelFollowEvent& e) {
	auto userDisplayName = TwitchSDK::ToFString(e.UserDisplayName);
	auto message = FString::Printf(TEXT("%s just followed!"), *userDisplayName);

	//print message to screen or do something interesting

});
```

### Subscribe to Custom Channel Rewards

#### Create the Subscription

```cpp
auto stream = core->SubscribeToCustomRewardEvents();
```

#### Wait for Events

```cpp
stream.WaitForEvent([](const TwitchSDK::CustomRewardEvent& e) {
	auto UserName = TwitchSDK::ToFString(e.RedeemerName);
	auto RewardName = TwitchSDK::ToFString(e.CustomRewardTitle);
	auto message = FString::Printf(TEXT("%s just purchased %s!"), *UserName, *RewardName);

	//print message to screen or do something interesting

});
```

### Subscribe to Hypetrain

#### Create the Subscription

```cpp
auto stream = core->SubscribeToHypetrainEvents();
```

#### Wait for Events

```cpp
stream.WaitForEvent([](const TwitchSDK::HypeTrainEvent& e) {
	auto HypeLevel = TwitchSDK::ToFString(e.Level);
	auto message = FString::Printf(TEXT("The HypeTrain is at level %s!"), *HypeLevel);

	//print message to screen or do something interesting

});
```

### Subscribe to Raids

#### Create the Subscription

```cpp
auto stream = core->SubscribeToChannelRaidEvents();
```

#### Wait for Events

```cpp
stream.WaitForEvent([](const TwitchSDK::ChannelRaidEvent& e) {
	auto UserWhoRaided = TwitchSDK::ToFString(e.FromBroadcasterName);
	auto Viewers = TwitchSDK::ToFString(e.Viewers);
	auto message = FString::Printf(TEXT("%s just raided with %s!"), *UserWhoRaided, *Viewers);

	//print message to screen or do something interesting

});
```
