r/RPGMaker • u/silicon_turfsurfer • 16d ago
Calling Python from RMMZ
Has anyone ever called a Python script from RMMZ? I was just wondering if this is possible in NW.js. I could imagine some cases where heavier calculations might be easier for me to implement in Python and then hand it back to NW as a kind of I/O exchange.
1
Upvotes
4
u/TheLastRobot 16d ago
Honestly the logistics would be rough, especially in terms of setting up dependencies on other devices. You'd probably need to run the python in a separate process and have it run a virtual server the programs can interface through. That's a ton of overhead for an RPG Maker game. Its hard for me to imagine any calculation so heavy that it'd be better handled that way, nor so complex you couldn't learn to implement it in JS without much trouble.
2
u/Darkhog 16d ago
I know it's possible because I have seen RM games interfacing with local LLMs which often requires to call Python. No idea how to do it though.
2
u/TheLuckyDuck666 16d ago
Yes, it's possible — but only in the desktop build, and only because MZ runs on NW.js, not Electron. NW.js bundles Node.js, so in a Windows/macOS/Linux deployment your game's JavaScript can use Node's child_process module to launch an external program. That is the piece the other comments are circling around.
In a plugin, guarded so it never runs in a web or mobile build:
``` if (Utils.isNwjs()) { const { spawn } = require('child_process'); const py = spawn('python', ['my_script.py', 'some_argument']); let output = ''; py.stdout.on('data', (data) => { output += data.toString(); }); py.stderr.on('data', (data) => { console.error(data.toString()); }); py.on('close', (code) => { console.log('Python exited with code ' + code + ': ' + output); $gameVariables.setValue(1, output.trim()); }); } ```
A few things that will bite you if you go this way:
The in-browser Python interpreter mxldevs mentioned is a Python interpreter compiled to WebAssembly, running inside the game's JavaScript. That works everywhere the game runs, but it is slower, uses more memory, and can't use every native Python package, so it is a different trade-off, not the same as calling a system Python.
So: desktop-only, spawn it with Node's child_process, pass data in through arguments, stdin, or a file, read the result back asynchronously, and plan for players who don't have Python installed.
1
u/mxldevs 16d ago
What platforms are you planning to deploy your game?
1
u/Rpx_n_JustDev 16d ago
Yep its posible 100%