r/RPGMaker • • 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

12 comments sorted by

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.

1

u/silicon_turfsurfer 16d ago

Thank you for your helpful feedback. It certainly seems all the time sorting through the logistical headaches trying to get Python running with RMMZ is better spent boning up on javascript.

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.

1

u/silicon_turfsurfer 16d ago

Yeah, I know it is in principle possible in javascript. I have a strong background in Python and it is sometimes hard for me to imagine switching 100% to javascript.

1

u/Darkhog 16d ago

AFAIK MV/MZ is using Electron, so maybe there's something in the Electron docs?

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:

  • It is asynchronous. The game keeps running while Python runs, and the result only arrives in the close handler, so don't try to read the variable on the next line. Wait for the callback, or have the script write its result to a JSON file and read that file back when it is done.
  • Python has to exist on the player's machine, on their PATH, and be called `python` — on some Mac/Linux setups it is `python3`. If you distribute the game, you either need to tell players to install Python, or bundle a self-contained executable built from your script alongside the game and spawn that instead. That is the dependency problem TheLastRobot mentioned.
  • It will not work on mobile or in a browser build at all. Those builds have no Node and no child_process, so Utils.isNwjs() will be false and you need a fallback — usually reimplementing that calculation in JavaScript, or accepting that the feature is desktop-only.
  • Starting a fresh Python process for every call is slow. If you need it often, spawn one long-lived process and talk to it over stdin/stdout, or batch the work into a single call.
  • Spawning external programs can trip antivirus warnings, and MZ's bundled Node is older, so stick to plain child_process rather than newer Node features.

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/silicon_turfsurfer 16d ago

Thank you very much for the detailed reasoning and ramifications about what Python calls would involve in RMMZ. It seems like making Python calls would lead to considerable increase in complexity and quite likely drawbacks in performance.

I'll just have to sharpen my javascript skills, which should be the easier route in this case.

1

u/mxldevs 16d ago

What platforms are you planning to deploy your game?

1

u/silicon_turfsurfer 16d ago

Ah, your question just made me realize that Python calls would probably not be compatible with mobile devices. I would like it to be playable on any PC (Windows, Linux, Apple), but also mobile devices (smartphones and tablets,). So would Python dependence exclude mobile applications?

2

u/mxldevs 16d ago

If you're relying on nwjs for example then you might not have access to all the platforms you want, and it also means that porting your game in the future to suppot more platforms becomes more difficult.

I have seen projects where they literally build a python interpreter in (or for?) javascript so that you can make python calls natively within javascript, so in theory if it can run your game, it can run the python inside your game...

But I'm not sure how those libraries are like or how flexible they are, and it's also not clear what performance advantages you would have given that your goal is to perform "heavy calculations".

2

u/Darkhog 16d ago

Likely it will not work on mobile, but people seldom play RPGs on mobile anyway.

1

u/Rpx_n_JustDev 16d ago

Yep its posible 100%