Hello,
I am trying to implement STEAM_CALLBACK() macro in my game. I found out that you cannot use the macro with a UCLASS, as it would fail to compile and cause a bunch of errors. So my solution was to have one UCLASS ,which the game reads, and is a inheritance game instance. And then a new non UCLASS which is used to retrieve Steam callbacks and then fire up the game instance’s events. As a very basic start, I just want to detect the Steam Game Overlay, and if it’s enabled, pause the game.
The code below compiles just fine, but the problem I am running into, is that the editor will crash when I start “Simulate” play. And I don’t know why it does that. These changes are the only thing being worked on. It didn’t crash at all before I added the new class FSteamworksCallbackAsync, so I think that the issue is there, but I don’t know why it’s doing that. I tried adding SteamAPI_Init() check, and checking to be sure that the class does have a GameInstance that is of type UMusicalRangeInstance, but I have no luck.
I appreciate any help!
The header File
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "ThirdParty/Steamworks/Steamv132/sdk/public/steam/steam_api.h"
#include "Engine/GameInstance.h"
#include "MusicalRangeInstance.generated.h"
/**
*
*/
UCLASS()
class MUSICALRANGE_API UMusicalRangeInstance : public UGameInstance
{
GENERATED_BODY()
private:
public:
/* test the functionality of the Game Instance */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Game Instance")
int32 InterLevelPersitentValue;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Steamworks")
bool IsSteamOverlayActive;
UFUNCTION(BlueprintCallable, category = "Game Instance")
bool InterLevelPersitentValuePONE();
/*
* Must be called every frame so callbacks can be received
* Always Return True!
*/
UFUNCTION(BlueprintCallable, category = "Steamworks")
bool RunSteamCallbacks();
UFUNCTION(BlueprintImplementableEvent, Category = "Steamworks")
void OnSteamOverlayIsActive(bool isOverlayActive);
void SteamOverlayActive();
void OnSteamOverlayEnabled(GameOverlayActivated_t *pResult, bool bIOFailure);
CCallResult<UMusicalRangeInstance, GameOverlayActivated_t> m_callResultGameOverlayActive;
};
/*
*
*/
class MUSICALRANGE_API FSteamworksCallbackAsync
{
private:
STEAM_CALLBACK(FSteamworksCallbackAsync, OnSteamOverlayActive, GameOverlayActivated_t, OnSteamOverlayActiveCallback);
public:
//Constructors
FSteamworksCallbackAsync();
~FSteamworksCallbackAsync();
UMusicalRangeInstance *MusicalRangeGameInstance;
};
The CPP File
// Fill out your copyright notice in the Description page of Project Settings.
#include "MusicalRange.h"
#include "MusicalRangeInstance.h"
//Musical Range Game Instance Elements
bool UMusicalRangeInstance::InterLevelPersitentValuePONE() {
InterLevelPersitentValue++;
return true;
}
bool UMusicalRangeInstance::RunSteamCallbacks() {
SteamAPI_RunCallbacks();
SteamOverlayActive(); //Check for active overlay
return true;
}
void UMusicalRangeInstance::SteamOverlayActive() {
SteamAPICall_t hSteamAPICall = SteamUtils()->IsOverlayEnabled();
m_callResultGameOverlayActive.Set(hSteamAPICall, this, &UMusicalRangeInstance::OnSteamOverlayEnabled);
return;
}
void UMusicalRangeInstance::OnSteamOverlayEnabled(GameOverlayActivated_t *pResult, bool bIOFailure) {
IsSteamOverlayActive = (pResult->m_bActive != 0);
OnSteamOverlayIsActive(IsSteamOverlayActive);
return;
}
//FSteamworksCallbackAsync elements
FSteamworksCallbackAsync::FSteamworksCallbackAsync() :
OnSteamOverlayActiveCallback(this, &FSteamworksCallbackAsync::OnSteamOverlayActive)
{
if (SteamAPI_Init() && GEngine->GetWorld() != nullptr && GEngine->GetWorld()->GetGameInstance() != nullptr)
{
MusicalRangeGameInstance = Cast<UMusicalRangeInstance>(GEngine->GetWorld()->GetGameInstance());
}
}
FSteamworksCallbackAsync::~FSteamworksCallbackAsync() {
}
void FSteamworksCallbackAsync::OnSteamOverlayActive(GameOverlayActivated_t *CallbackData) {
if (SteamAPI_Init())
{
bool IsOverlayActive = (CallbackData->m_bActive != 0);
GEngine->AddOnScreenDebugMessage(-1, 15, FColor::Cyan, IsOverlayActive ? "Overlay is ACTIVE" : "Overlay is OFF");
MusicalRangeGameInstance->OnSteamOverlayIsActive(IsOverlayActive);
} else {
GEngine->AddOnScreenDebugMessage(-1, 15, FColor::Red, "Error with SteamAPI" );
}
}