paradust7 / minetest-wasm

Experimental Minetest build for WebAssembly/Emscripten
https://minetest.dustlabs.io/
MIT License
99 stars 23 forks source link

How should I embed JavaScript code in this project? #3

Closed lychees closed 2 years ago

lychees commented 2 years ago

I'm trying to use embedded JavaScript code to allow players to jump to other pages by clicking on a specific Entity NPC when running the game in the browser.

I have implemented an NPC through this API. [Mobs Redo](https://forum.minetest.net/viewtopic.php?t=9917)

Currently, clicking on this NPC will send an in-game message via minetest.chat_send_player(name, text) in Lua API. Q1

Then I wanted to implement a jump to another page by modifying the Lua API (embedding JavaScript code in the game's C++ code).

I wanna embed JavaScript code in a function in the C++ code of the Lua API and then call it in the Lua script.

Something like: C++ int ModApiXX::l_jump_to_page_web(lua_State *L) { NO_MAP_LOCK_REQUIRED; EM_ASM([Some JavaScript code]); return 0; } Lua minetest.jump_to_page_web(XXX)

What should I do? How should I use Emscripten macros to embed JavaScript code?

I already have an Emscripten build environment and have successfully compiled the original version once.

paradust7 commented 2 years ago

You have the right idea. But you need to run the javascript on the main thread, using:

MAIN_THREAD_ASYNC_EM_ASM(...javascript to redirect page....);

Because the server runs Lua on a pthread (a webworker).