-
Notifications
You must be signed in to change notification settings - Fork 3
How to use and run C or Cpp code in RPG Maker MZ ?
We assume that RPG Maker MZ corescript v1.1.1 is used.
Install all of them. And then open terminal (You can use cmd on Windows or install MINGW64 on your machine). Use npm to install modules :
$ npm install -g node-gyp nw-gyp node-api node-addon-api nan
or install to local
$ npm install node-api node-addon-api
where required versions are
- node-gyp v7.1.2
- nw-gyp v3.6.5
- node-api v0.0.1
- node-addon-api v3.1.0
- nan v2.14.2
We need to modify a few files. This is a workaround to bypass some errors because many latest tools are unsupported by nw-gyp and nw.js.
- Open and Edit ~\AppData\Roaming\npm\node_modules\nw-gyp\lib\configure.js [Link]
... // don't care
Line 176: gyp.opts.msvs_version = '2015' ====== change to ======> gyp.opts.msvs_version = '2019'
... // don't care
Line 179: defaults['msbuild_toolset'] = 'v140' ====== change to ======> defaults['msbuild_toolset'] = 'v142' // check and change by yourself
... // don't care
Line 181: variables['msbuild_path'] = path.join(vsSetup.path, 'MSBuild', '15.0','Bin', 'MSBuild.exe') ====== change to ======>
variables['msbuild_path'] = path.join(vsSetup.path, 'MSBuild', 'Current','Bin', 'MSBuild.exe') // check and change by yourself
... // don't care
In nw-gyp configuration, if you have such errors:
gyp info spawn args ]
File "<string>", line 1
import sys; print sys.byteorder
^
SyntaxError: invalid syntax
gyp: Call to 'python -c "import sys; print sys.byteorder"' returned exit status 1 while in binding.gyp. while trying to load binding.gyp
gyp ERR! configure error
- Open and Edit ~\.nw-gyp\0.44.5\config.gypi [Link]
... // don't care
Line 179: 'v8_host_byteorder': '<!(python -c "import sys; print sys.byteorder")',
====== change to ======> 'v8_host_byteorder': '<!(python -c "import sys; print (sys.byteorder)")',
... // don't care
If you have installed both python 2 and python 3, this error maybe occurs.
Create an "example" folder and enter, use npm init
to initialize and configure. Besides, we have files as following :
- filename: example/hello.cpp
#include <napi.h>
#include <string>
std::string hello()
{
return "Hello World !";
}
Napi::String _hello(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
std::string result = hello();
return Napi::String::New(env, result);
}
Napi::Object Init(Napi::Env env, Napi::Object exports)
{
exports.Set(Napi::String::New(env, "hello"), Napi::Function::New(env, _hello));
return exports;
}
NODE_API_MODULE(example, Init);
- filename : example/binding.gyp
{
"targets": [
{
"target_name": "example",
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"sources": [
"hello.cpp"
],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
}
]
}
- filename : example/package.json
{
"name": "example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"install": "node-gyp rebuild"
},
"author": "",
"license": "ISC",
"gypfile": true,
"dependencies": {
"node-addon-api": "^3.1.0",
"node-api": "0.0.1"
}
}
$ nw-gyp configure --target=0.44.5
Now a build folder is generated.
Open example/build/example.vcxproj with Visual Studio 2019 and "Build"->"Build Project" (Shortkey : F7). The node is built and placed at example/build/<Debug|Release>/example.node
Open RPG Maker MZ editor and playtest. Run the game and open DevTools. Use
var foo = require('<path-to-example-node>/example.node')
foo.hello()
It should output "Hello World !"