defold / extension-html5

Example showing how to create a native extension for HTML5
MIT License
13 stars 4 forks source link

JS callbacks #1

Open TonyStarkBy opened 7 years ago

TonyStarkBy commented 7 years ago

Can you add example of code how call lua function from JS and from lua call JS function?

mathiaswking commented 7 years ago

There is no way to call a Lua function directly from JS (I can't think of one at least)

There is however an example to run Lua code by passing data to the app in the html code: Search for "SetTextData". This in turn calls the custom js library MYTESTLIBRARY.SetTestData(txt), which in turn sets the data. That data is later picked up by the engine, and when the lua function html5nativeext.get_data() returns that string, you can use "loadstring()" to run that code (which is Lua code).

As for calling JS from Lua, you can either use our new module html5.run() or take a look at emscripten_run_script()

AGulev commented 7 years ago

It something like waiting changes in update function?

hmm.. What about cwrap or ccall ? https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html

mathiaswking commented 7 years ago

Well although ccall/cwrap does seem like it's one step closer to what you want, I'm still not sure how that would work in terms of running on the main thread of the engine, or rather, when the correct Lua context is active.

TonyStarkBy commented 7 years ago

As for calling JS from Lua, you can either use our new module html5.run()

Ok, it can be used.

There is no way to call a Lua function directly from JS (I can't think of one at least)

Hmm... So current solution is to implement "hand made function call queue?" based on "on_update" function? :(((

I was expecting that with native extension I could make something like this: https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

mathiaswking commented 7 years ago

@TonyStarkBy In the example link you gave, it was an example of calling JavaScript from the engine? I couldn't see an example of how to interact with the engine from the index.html side?

Btw, I also posted a small example on our forum

AGulev commented 7 years ago

too many Update calls in lua is not good decision especially in html5 target, as I think. And this is contradicts of engine reactive paradigm.