Using WebAssembly and C++



  • Hey, I just got this game recently and JavaScript is really kicking my butt. I've done quite a bit of programming in C++ and I keep reading about how it is possible to compile C++ to WebAssembly. I just don't understand how I would interact with the in-game objects through C++ code. Like, is there an API or tutorial that I can check out? Could someone maybe post some example code or point me in the right direction?



  • WASM sets up an array, which is treated as the memory for the WASM code. You have to copy the in-game objects into this array. And wasm supports only basic types (int, uint of various lengths). There's probably code in emscripten which will wrap all of this up for you and copy javascript objects in and out, but I've only used WASM with byte data.

    There's some example code here that uses C++ https://github.com/screepers/cppreeps



  • @wtfrank said in Using WebAssembly and C++:

    WASM sets up an array, which is treated as the memory for the WASM code. You have to copy the in-game objects into this array. And wasm supports only basic types (int, uint of various lengths). There's probably code in emscripten which will wrap all of this up for you and copy javascript objects in and out, but I've only used WASM with byte data.

    There's some example code here that uses C++ https://github.com/screepers/cppreeps

    Alright, thanks for you help!


  • YP

    maybe you should look into typescript .. I found it much easier to get started with then javascript .. using classes, inheritance and stuff. I know all that is also possible with pure javascript but typescript helps you to prevent making many stupid errors ,)

    https://github.com/screepers/screeps-typescript-starter

    also look for help in slack



  • Yeah if what you like about C++ is the type-checking that catches errors early, then typescript is a strong recommendation. I also came from a C++ background and after getting endlessly frustrated with stupid errors making it onto the mmo server, I moved to typescript and never looked back. I found several serious bugs just by adding the types to my code to covert it from js to typescript.