We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.

Purpose of prepareExternalInterface?

Home Forums Programming Purpose of prepareExternalInterface?

  • This topic has 2 replies, 2 voices, and was last updated 6 years ago by jem.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #3248

    I see that the standard V3D application code in v2.2 has a new function:
    function prepareExternalInterface(app){...}

    I understand how this can be used to register functions as properties of app.ExternalInterface. In prior versions had been registering functions in the runCode() function.

    I know there must be a very good reason that the team reorganized the code, but I am not a good enough programmer to see it. Would someone from the Soft8soft team explain what code should be in prepareExternalInterface(app) and what should be in runCode() and why?

    Thank you,

    Jeremy Wernick


    Hi, jem

    The main difference between prepareExternalInterface and runCode is that the first is called before scene loading and hence initialization of the Puzzles logic, and the last – is called after:


    // scene loading, initializaton/execution of the Puzzles logic code


    The code of the Puzzles logic can access app.ExternalInterface and call user-defined js functions, so they should already be registered by the time of the call, otherwise it will crash. To be sure that it will work properly you should register them in prepareExternalInterface.

    The crash didn’t always happen when using the “call JS function” puzzle block, but in certain configurations it did. For example, let’s say you have the following logic setup:
    puzzles setup

    In this setup the JS functions are executed immediately upon the logic initialization, that is before runCode, where they usually have been registered. This led to a crash, that’s why we introduced a special place like prepareExternalInterface to avoid such problems.

    On the other hand you can define a function via the puzzle block “when called from JS”:

    and call it from the application code like this:


    Considering the execution order, mentioned above you should call it in runCode, after it was created during the Puzzles initialization.

    So, basically, you need to register JS callbacks in prepareExternalInterface and do other stuff in runCode.

    You must be logged in to view attached files.

    Co-founder and lead developer at Soft8Soft.


    That was a very clear explanation. Thank you!

    Jeremy Wernick

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.