Communicate with J2Play Community
From J2Play
Communicating with the J2Play community is one of the most useful features of the GDK as it allows you to control the environment in which your game resides.
Contents |
Talking to the Community
Use GDKInstance::GameCommunity_StateChange() to send any of the following events.
Enable/Disable Community
Call GDKInstance::GameCommunity_StateChange() with event_id = J2PLAY_COMMUNITY_DISABLE to disable the community header bar.
Call GDKInstance::GameCommunity_StateChange() with event_id = J2PLAY_COMMUNITY_ENABLE to enable the community header bar.
You will likely want to disable the community header bar when your game starts in order to prevent misclicks and then enable it when game finishes. Alternatively, you can handle the pause and resume messages and respectively pause/resume your game accordingly.
Show/Hide Community Header bar
Call GDKInstance::GameCommunity_StateChange() with event_id = J2PLAY_COMMUNITY_HIDE to hide community, and call GDKInstance::GameCommunity_StateChange() with event_id = J2PLAY_COMMUNITY_SHOW to show commuinty, or call GDKInstance::GameCommunity_StateChange() with event_id = J2PLAY_COMMUNITY_SWITCH_SHOWHIDE to switch between show state and hide state. These operations only affects header bar and side bar. Hiding them to let player focus on game.
Minimize Community
Call GDKInstance::GameCommunity_StateChange() with event_id = J2PLAY_COMMUNITY_MINIMIZE to minimize community window
Playground class
Playground class contains static methods to operate the community.
Using Playground::SwitchToHomeTab() for switching back to current game screen
Using Playground::SwitchToHowToPlayTab() for switching to 'How to play' tab page
Using Playground::SwitchToChallengeTab() for switching to 'Challenge - Challenge Center' tab page
Using Playground::SwitchToAchievementTab() for switching to 'Achievement - Leader Board' tab page
Using Playground::SwitchToReviewTab() for switching to 'Review' tab page
Using Playground::SwitchToMoreGamesTab() for switching to 'More games' tab page
Using Playground::SwitchToStoreTab() for switching to 'Store' tab page
Using Playground::BackToOriginGame() for switching back to original C++ game
Event from Community
J2Play GDK uses event to notify the game, there are a lot of events can hooked. GDK uses c++ keywords __event, __hook and __unhook to implement event disptaching.
For make a event notification function,
1. Take a look at GDKInstance.h about the event you are interested in, copy the event signature as the handler function signature
2. call __hook(&CGDKInstance::eventName, pointerToGDKInstance, handlerFunctionPointer)
3. call __unhook(&CGDKInstance::eventName, pointerToGDKInstance, handlerFunctionPointer) before you close or when you don't want that hooker anymore.
For example,
1. GamePause event define as
__event void GameResume();
then,create a function as
void ResumeHandler();
2.add following code at begining
CGDKInstance* inst = CGDKInstance::getInstance(); __hook(&CGDKInstance::GameResume, inst, &ResumeHandler);
3.add following code at end
__unhook(&CGDKInstance::GameResume, inst, &ResumeHandler);
And, if ResumeHandler is a member function of a class, you should add following code before class declaration as
[event_receiver(native)] class EventHandlerClass
and then define the member function as same as the event function signature, and then call __hook and __unhook in a member function of EventHandlerClass, we propose creating a pair of member functions for this task:
void EventHandlerClass::hookEvents(CGDKInstance* inst)
{
__hook(&CGDKInstance::GameResume, inst, &EventHandlerClass::ResumeHandler);
// .. other event hookers
}
and
void EventHandlerClass::unhookEvents(CGDKInstance* inst)
{
__unhook(&CGDKInstance::GameResume, inst, &EventHandlerClass::ResumeHandler);
// .. other event unhookers
}
Then you can use following code to init hookers before you create community
EventHandlerClassInstance.hookEvents(CGDKInstance::GetInstance());
and call
EventHandlerClassInstance.unhookEvents(CGDKInstance::GetInstance());
before you destroy GDK as
CGDKInstance::GetInstance()->Destroy().
Pause Event
Pause event is triggered when community show something over game, community uses it to notifiy the game to pause all process and wait for resume event. The event name used in __hook is CGDKInstance::GamePause.
Resume Event
Resume event is triggered when after paused, when community finishes all process and it is time to resume the game. The event name used in __hook is CGDKInstance::GameResume.
Quit Event
Quit event is triggered when community is to be closed, you can add game cleanup code here. Note: The game window is created as child window of community, so when community window is to be destroyed, it will destroy game window automatically." "Please make sure you didn't do cleanup twice if you have some cleanups in game window WM_CLOSE or WM_DESTROY message handle. The event name used in __hook is CGDKInstance::QuitApplication.
Connection Broken Event
Connection-broken event is triggered when the network connection to J2Play platform is accidentally broken. You'd better close the GDK toolkit and restart it again. The event name used in __hook is CGDKInstance::connectionBroken.
