The Kongregate API will fire various events to the hosting application. Here’s a description of each event that is fired. The API provides constants for each of these events, their string names are used here for portability. The API’s support registering a listener for the events or to manually poll for new events.
KONG_API_EVENT_GAME_AUTH_TOKEN_CHANGED
event, which always follows this one.
requestUserItemList
. At this point you can call hasItem
to determine if the Kongregate user has a given item in their inventory.
The preferred method for listening for API events is to use an event listener.
Your event listener will be called on the Main/UI thread, so you should be sure to not perform any I/O in your callback.
The listener should be a global object that is registered as soon as possible after the API is initialized so you can start receiving events.
// register a selector to handle API events
[[KongregateAPI instance] setApiEventListener:self selector:@selector(onKongregateApiEvent:)];
...
-(void)onKongregateApiEvent:(NSString *)event {
NSLog(@"GOT EVENT FROM API: %@", event);
if ([event isEqualToString:KONGREGATE_EVENT_READY]) {
// API is Ready
}
}
KongregateAPI api = APIBootstrap.getInstance();
api.addEventListener(new KongregateEventListener() {
@Override
public void onEvent(String event) {
if (KongregateEvent.READY.equals(event)) {
// API is Ready
}
}
});
KongregateAPI api = KongregateAPI.GetAPI();
api.SetEventListener("Kongregate", "HandleKongregateEvent");
...
void HandleKongregateEvent(string eventName) {
switch(eventName) {
case KongregateAPI.KONGREGATE_EVENT_READY:
// API is Ready
}
}
If setting up an event listener is not feasible in your environment, then you can also poll manually for events using the pollEvents
method on the main API object. This method will return an array of pending event strings that you can iterate through and process manually.
// Setup a timer to poll through new events every 2 seconds. If calls
// the same onKongregateApiEvent selector used in the listener example.
static dispatch_queue_t _kongregateEventPollQueue = dispatch_queue_create("Kongregate Event Poll", DISPATCH_QUEUE_SERIAL);
_kongregateEventPollTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _kongregateEventPollQueue);
dispatch_source_set_timer(_kongregateEventPollTimer, dispatch_time(DISPATCH_TIME_NOW, 2ull * NSEC_PER_SEC),
2ull * NSEC_PER_SEC, 2ull * NSEC_PER_SEC);
dispatch_source_set_event_handler(_kongregateEventPollTimer, ^{
for (NSString* event in [[KongregateAPI instance] pollEvents]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self onKongregateApiEvent:event];
});
}
});
dispatch_resume(_kongregateEventPollTimer);
// Setup a timer to poll through new events every 2 seconds. If calls
// the same handleEvent method used in the listener example.
final Activity main = this;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
main.runOnUiThread(new Runnable() {
@Override
public void run() {
for (String event : mAPI.pollEvents()) {
handleEvent(event);
}
}
});
}
}, 0, 2000);
Not Supported. Use the listener method.