Calling a totally different program within RPG Maker

Hi, is it possible/practical to have a script call another javascript program which will run so to speak on top of the RPG Maker rather than trying to integrate with it (as this seems super tricky for even the most simple of things)?

The idea would be to pass some parameters from RPG Maker to the game, have it run something in Javascript in the same window, have that new program pass parameters / results back, pop the screen back to the RPG Maker system and continue running.

Is that possible or practical?
It is possible but extremely difficult for the same reason it is anything but practical.

just think about it:
what you describe is basically what malware does to inject itself into a website (making that website call the malware program without it being part of that website).
So most antivirus and internet protection programs are specifically designed to detect and block such external access.

yes, you can tell the browser running the RM-Game and the installed protection programs to allow this because you know it is no malware.
But do your players know the same? Especially if they just downloaded your program without knowing you, you can bet that most people will not risk malware and block the access instead.
Technically, every plugin is a "JavaScript program", and calling them is trivial.

But if you're trying to launch a different RPG Maker project from within your RPG Maker project, that's going to be a lot more difficult.

Most programs nowadays make use of application-specific protocols. For example, if you try to call https://www.google.com, you're using the https protocol, which will open a browser and tell it to navigate to Google. And if you try to call steam://run/123456789, you're using the steam protocol which will open Steam and tell it to run the game with the ID 123456789.

This is also how websites can open apps on your PC. For example, if you go to the Microsoft store on the web, it opens a link using the msstore protocol. The Apple AppStore has a similar protocol. And if I'm not mistaken, this is how Android applications call each other (Google calls it "intent" instead of "protocol").

Obviously, your computer needs to know which app can or cannot handle certain protocols. Unfortunately, I don't know how to do it from JavaScript, or whether that's even possible in the first place. I also don't know whether you can retrieve any results back from calling a protocol.
Technically, every plugin is a "JavaScript program", and calling them is trivial.
I am with Nolonar here. If your goal is to just execute some function to which you pass some parameters and do something with the return value, there is really no need to go through all that trouble and write some convoluted code to open another window and have it run there. Just make it a plugin. Even javascript that was not intended as an RPG Maker plugin can sometimes be integrated with relative ease.
I think your game and your other application can communicate via REST. Your game sends HTTP requests to your app, which implements a web server to answer them.

You need to find a way to start the app, as this communication only works when the app is already running. Right now, I don't have an answer to that.

In the end, you need to estimate whether this scenario is worth the set up.
I need to clarify. I'm not trying to call a different Windows App from Javascript. I'm not trying to call another instance of RPG Maker. I want to make the browser set up a new canvas, run some code on it, and then eventually return to the calling Javascript program (RPG Maker game which is running),

I don't know enough about how RPG Maker, Javascript and Browsers work yet in order to know if there are barriers to this.

Let's suppose for argument's sake I had the source code to a tetris type game which is written in javascript and runs in a browser. Could I turn it into a plugin or a script call, run the game and have the last Tetris score returned to the script as a variable that RPG Maker can use? This is not exactly what i want to do but it illustrates the kind of thing I need. In other words, the new Javascript in browser game runs (maybe in the same window, maybe not, but ideally the same one), and its source code is slightly modified so that the high score gets passed back to the calling program somehow. Hopefully it can be done without calling a REST API.
I need to clarify. I'm not trying to call a different Windows App from Javascript. I'm not trying to call another instance of RPG Maker. I want to make the browser set up a new canvas, run some code on it, and then eventually return to the calling Javascript program (RPG Maker game which is running),

I don't know enough about how RPG Maker, Javascript and Browsers work yet in order to know if there are barriers to this.

Let's suppose for argument's sake I had the source code to a tetris type game which is written in javascript and runs in a browser. Could I turn it into a plugin or a script call, run the game and have the last Tetris score returned to the script as a variable that RPG Maker can use? This is not exactly what i want to do but it illustrates the kind of thing I need. In other words, the new Javascript in browser game runs (maybe in the same window, maybe not, but ideally the same one), and its source code is slightly modified so that the high score gets passed back to the calling program somehow. Hopefully it can be done without calling a REST API.
I would think this is probably a bit more complicated than it sounds due to the nature of everything running within the context of pixi.js's framework and RMMZ's own native framework. If I had to give a recommendation on how to accomplish it, I'd probably say the other game/service/app would have to also be a pixi.js app so you could potentially enable/add the whole pixi.js stage of the other game ontop of this one (and probably pause rendering for RMMZ for the duration).

Possible? yeah.
Feasible? less so.
You may as well just spin up your own game from scratch at this point.
When it's a browser game (i.e. a game that is played with your default browser, Chrome, Edge, ...) then I would say no chance, as Andar already explained.

When you have two running desktop apps, then they can communicate via REST. It's quite easy to learn how to build a simple nodejs webserver. But you need to find out how to integrate such a web server into your RPG Maker game.
Thanks for bearing with me. I know I can make API calls to an external node.js server because I've already done this, I know you can set up a node.js server on windows but how to automate it? And it wouldn't work on mobile. But even if this was already in place how would you pass control to the other process?

I don't yet grasp why everything is so difficult. When an RPG Maker game runs on my PC, I can press F8 and look at Javascript, so it is running in a browser. Why is it hard to save every element on this browser the game is running on, push it onto some kind of stack, clear the screen, run code that does something else, and then later pop the stack and restore the DOM?

I don't mind if the whole thing is a plugin - the game source code is in the .js files of the plugin - but to me the whole problem seems to be around how jolly hard it is to do anything with the screen. The simplest things, like writing a message to the screen, seem to require many lines of code to implement in javascript as some of these plugins reveal. Not to mention getting keyboard input or drawing something...

Or is it possible to effectively SAVE THE GAME without user input, using a call, and then do something else, and then LOAD THE GAME. I don't know. I can only think at a fairly high level, having been out of programming for 20 years. And only now relearning programming just as much as I need to.

If i could get developers at a price I could afford I would - but the more I know, the better I can operate anyway.
I don't yet grasp why everything is so difficult. When an RPG Maker game runs on my PC, I can press F8 and look at Javascript, so it is running in a browser. Why is it hard to save every element on this browser the game is running on, push it onto some kind of stack, clear the screen, run code that does something else, and then later pop the stack and restore the DOM?
Hmm, this is different from what I understood from you in the beginning. This means you don't have 2 programs that run standalone, but you stay in the same program that just swaps to a different screen.

Well, you can add a new element to the dom; this is how the FPS counter is implemented, for example. But, you can't add another variable container. The variables from game1 and game2 will likely battle each other.

I don't mind if the whole thing is a plugin - the game source code is in the .js files of the plugin - but to me the whole problem seems to be around how jolly hard it is to do anything with the screen. The simplest things, like writing a message to the screen, seem to require many lines of code to implement in javascript as some of these plugins reveal. Not to mention getting keyboard input or drawing something...
That is, my friend because a game does not consist of multiple elements but only one: the canvas. We don't use the common html5 / CSS / js technology, but we use the API from rmmz_windows, etc.

The code of an exported game is huge, like several thousand lines of code.

You can check if DataManager.saveGame(fileId) and DataManager.loadGame(fileId) works for you. However, they don't freeze or dump your game, I think, they save only the game-relevant information (actors, level, hp, player's position).
Is it possible to save everything on this one canvas element, then do something totally different on this canvas which has basically nothing to do with RMMZ, and then restore the canvas back to where it was and continue?
I was looking for a shortcut. If none of this is possible, where is the best information on actually writing to the screen and collecting information from mouse and keystrokes?
I'm not sure what you try to do... I mean, what your plan is... Are you trying to run ads inside your project or something?

I think it is okay if you run ads in your game, just make sure you keep in mind that there are tight laws for this, at least in Europe. Mainly related to the cookies.

If it is not ads you try to add, then mind me asking what it is? Just out of curiosity?
It isn't ads.
Ah, you told me a little what you try to do in a DM. I get why you don't want to post it publicly. Keep some ideas a surprise for the player, right?

I was curious though, cause honestly it seemed like a mallware or fishing idea. 😛 glad to read that is not the case.

However, what you try to do, can be done inside RPG maker. It is probably even easier than opening a 2nd application on top of RPG maker.
So, from what I've read so far, I think what you're trying to do is very easy. Let's say you have a fully-featured Tetris game (code and all), that works as intended as a standalone app. The easiest solution would be to put it in an iframe to embed it. That's how sites embed YouTube videos and so on.

Now, getting data out of that iframe is a bit more tricky due to security concerns. After all, you wouldn't want some shady webpage to embed your e-banking site in an invisible iframe, then retrieve your session token from the iframe (assuming you're still logged in), which it could then use to impersonate you and steal money directly from your bank account. Fortunately, from what little research I've done, it seems that you can circumvent this security mechanism using window.postMessage().

In your example, you'd have to edit your Tetris app to make use of window.postMessage() to post the appropriate data (the last Tetris score) to the appropriate target (your RPG Maker game). Once your RPG Maker game receives that message, it could then hide or remove it.

For example, if your game is hosted on https://www.my-game.com and the Tetris app is hosted on https://www.my-game.com/tetris, then the Tetris app would have to send the score like this:
JavaScript:
window.postMessage(score, "https://www.my-game.com");

As for your RPG Maker project, in order to receive that data, you'll need to add an event listener, like this:
JavaScript:
let score = null;
window.addEventListener("message", event => {
  if (event.origin !== "https://www.my-game.com/tetris")
    return; // We're only interested in messages from the Tetris app.

  score = event.data;
});
Note that event listeners are asynchronous. This means that RPG Maker will continue to run without waiting for Tetris to respond. If you wish to suspend the RPG Maker application while you wait for the Tetris score, you can either
  • add a busy-wait:
    JavaScript:
    while (score === null) await new Promise(r => setTimeout(r, 100));
    will check every 100 milliseconds if the score has been set.

  • make use of the async-await pattern (I don't recommend it when you're a beginner, as it is very likely to confuse you). If you're curious, you'd have to define an async function:
    JavaScript:
    async function getTetrisScore() {
      return new Promise(resolve => {
        window.addEventListener("message", event => {
          if (event.origin !== "https://www.my-game.com/tetris")
            return;
    
          resolve(event.data);
        });
      });
    }
    and call it like this:
    JavaScript:
    let score = await getTetrisScore();
    Note that my JavaScript (and especially async-await skills) has become a bit rusty and I make no guarantee that the above code works.

  • or come up with some other creative solution.
So, from what I've read so far, I think what you're trying to do is very easy. Let's say you have a fully-featured Tetris game (code and all), that works as intended as a standalone app. The easiest solution would be to put it in an iframe to embed it. That's how sites embed YouTube videos and so on.

Now, getting data out of that iframe is a bit more tricky due to security concerns. After all, you wouldn't want some shady webpage to embed your e-banking site in an invisible iframe, then retrieve your session token from the iframe (assuming you're still logged in), which it could then use to impersonate you and steal money directly from your bank account. Fortunately, from what little research I've done, it seems that you can circumvent this security mechanism using window.postMessage().

In your example, you'd have to edit your Tetris app to make use of window.postMessage() to post the appropriate data (the last Tetris score) to the appropriate target (your RPG Maker game). Once your RPG Maker game receives that message, it could then hide or remove it.

For example, if your game is hosted on https://www.my-game.com and the Tetris app is hosted on https://www.my-game.com/tetris, then the Tetris app would have to send the score like this:
JavaScript:
window.postMessage(score, "https://www.my-game.com");

As for your RPG Maker project, in order to receive that data, you'll need to add an event listener, like this:
JavaScript:
let score = null;
window.addEventListener("message", event => {
  if (event.origin !== "https://www.my-game.com/tetris")
    return; // We're only interested in messages from the Tetris app.

  score = event.data;
});
Note that event listeners are asynchronous. This means that RPG Maker will continue to run without waiting for Tetris to respond. If you wish to suspend the RPG Maker application while you wait for the Tetris score, you can either
  • add a busy-wait:
    JavaScript:
    while (score === null) await new Promise(r => setTimeout(r, 100));
    will check every 100 milliseconds if the score has been set.

  • make use of the async-await pattern (I don't recommend it when you're a beginner, as it is very likely to confuse you). If you're curious, you'd have to define an async function:
    JavaScript:
    async function getTetrisScore() {
      return new Promise(resolve => {
        window.addEventListener("message", event => {
          if (event.origin !== "https://www.my-game.com/tetris")
            return;
    
          resolve(event.data);
        });
      });
    }
    and call it like this:
    JavaScript:
    let score = await getTetrisScore();
    Note that my JavaScript (and especially async-await skills) has become a bit rusty and I make no guarantee that the above code works.

  • or come up with some other creative solution.
Thank you, this approach seems quite helpful. I wonder what others think.
Is it possible to save everything on this one canvas element, then do something totally different on this canvas which has basically nothing to do with RMMZ, and then restore the canvas back to where it was and continue?
I can't really imagine a need for that. There's no rule that says that you can only have one canvas on a page. You can hide the RPG Maker canvas (using its 'hidden' attribute), and then create a new canvas in the same place to do your other stuff. Then when you're done, you can remove the new canvas, and un-hide the RPG Maker canvas.

Though if the external game (or whatever it is) isn't something that was coded by you, then the iframe thing would probably be better, since it would encapsulate the code better, to make sure there's no accidental namespace pollution or something.

Original thread: https://forums.rpgmakerweb.com/threads/calling-a-totally-different-program-within-rpg-maker.151180/