larsiusprime / SteamWrap

Haxe native extension for the Steam API
MIT License
106 stars 44 forks source link

Fixing Game Overlay check, for pausing game. #33

Open SETENTIAdev opened 4 years ago

SETENTIAdev commented 4 years ago

I just thought I might give some pointers on how I fixed GameOverlayActivated.

By default, some events won't be listened to, unless are specifically set.

My main concern was to pause the game when the overlay is up. Current calls, such as IsOverlayEnabledor BOverlayNeedsPresent didn't work for me, as the first just checks if the overlay is enabled (this is always true, unless user turned it off) and the later only is called for drawing purposes (to refresh the screen). Doesn't work as expected, will return true anytime something about the overlay renders over the screen. You don't want that.

By reading this issue I realized I could implement my own callbacks, and thats what I did. Make sure you read that link I mentioned so you know where to put this. Here's the code: static const char* kEventTypeOnGameOverlayActivated = "GameOverlayActivated";

m_CallbackGameOverlayActivated( this, &CallbackHandler::OnGameOverlayActivated )

STEAM_CALLBACK( CallbackHandler, OnGameOverlayActivated, GameOverlayActivated_t , m_CallbackGameOverlayActivated );

void CallbackHandler::OnGameOverlayActivated( GameOverlayActivated_t *pCallback )
{
    SendEvent(Event(kEventTypeOnGameOverlayActivated, pCallback->m_bActive));
}

WherepCallback->m_bActive does come from?

You can read Steam API docs here: https://partner.steamgames.com/doc/api/ISteamFriends#GameOverlayActivated_t

You will need to build the wrapper again, by using build.bat

Then, over Steam.hx, on steamWrap_onEvent you can access the event, like this:

case "GameOverlayActivated": 
                if (success) 
                    overlayActive = true;
                else
                overlayActive = false;

                trace("GameOverlayActivated " +overlayActive);

Our framework is HaxeFlixel, so we needed another function that is checked every frame, that sits in top of the current framework, so, the listener is added in our Main.hx:

addEventListener(Event.ENTER_FRAME, onEnterFrame);

Then you can do your magic by doing this:

private function onEnterFrame(_):Void
    {
        if (Steam.active)
        {
            Steam.onEnterFrame();

            FlxG.paused = Steam.overlayActive;
        }   
    }

I know this might be poorly explained, but is the meat of it. Hope it helps someone else.