Coherent UI  2.5.3
A modern user interface library for games
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
JavaScript

All communication between JavaScript and the game goes through the engine module.

There are two ways for invoking native code from JavaScript and vice-versa.

  • Events
  • Calls

Events

Events allow to call multiple handlers in both directions, but they can not return any value. To register a JavaScript handler for an event use the engine.on method. Detailed documentation for the events is in the Binding documentation.

Promises

Promises are used to return results from C++ to JavaScript. Coherent UI promises are modeled after the Promises/A specification. For samples how to use the promise objects returned by engine.call see JavaScript triggering C++.

Customizing Promises

Coherent UI has an implementation of promises, but it is possible to use any implementation that has deferred objects and they have resolve and reject methods. This allows you to use a promises library of your choice or one that better integrates with the rest of your code.

To change the promises implementation you have to define engineCreateDeferred function prior to including coherent.js script in your HTML. That function should return a new deferred object each time it is called.

To use JQuery Deferred your HTML should look like this:

<script type="text/javascript" src="jquery/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
engineCreateDeferred = jQuery.Deferred;
</script>
<script type="text/javascript" src="javascript/coherent.js"></script>

Or to use the when.js library:

<script type="text/javascript" src="when/when.js"></script>
<script type="text/javascript">
engineCreateDeferred = when.defer;
</script>
<script type="text/javascript" src="javascript/coherent.js"></script>
See Also
engine
Binding