-
Notifications
You must be signed in to change notification settings - Fork 0
Basic usage
Here is a basic example of how you can use gca-js in your game. The API version of this example is an asynchronous version of the GCA API 2.0.
var gca = require('gca-js');
// Find and open the first adapter in your computer.
// If an adapter was found, start communication with it.
var adapter gca.getAdaptersList()[0];
if(adapter!=null) {
gca.startAdapter(adapter);
}
There are two approaches of obtaining the current state of the adapter and the controllers connected to them: A synchronous communication, and an asynchronous communication (not to be confused with synchronous JavaScript and asynchronous JavaScript)
In a synchronous approach, the application must communicate with the adapter in every tick or frame. The application will not respond until a transfer has completed.
readData performs a single communication transfer, and returns the current state of the adapter. It needs a callback function that processes that data. Although gca-js includes some sample callback functions, this callback function can be a custom one.
// This will occur at every frame.
document.onTick = function(event) {
gca.readData(adapter,function(data) {
gca.objectData(data))
});
}
In an asynchronous approach, the application constantly schedules communication transfers, although not necessarily every tick or frame. These communications will be executed on parallel via events, so the application will not stall even if it takes too long to get the status of the adapter.
pollData enables an asynchronous communication transfer, and will only execute the callback function once it receives the status of the adapter. As with the readData function, you may use a custom function as the callback.
// This function will only be called once.
gca.pollData(adapter,function(data) {
console.log(gca.objectData(data))
});
The stopAdapter function must be called when the application closes. This frees the adapter to be used in other applications, as well as being able to disconnect it safely.