Script for changing button functions

And I'm telling you that you can't do that because that's not how the key mapper works.

You also don't need to add code 16 because it already exists:
View attachment 218870
So you'd need to have your event check for Input.isTriggered("shift") and then in there call $gameTemp.reserveCommonEvent(22)


Now I am just confused. LOL

I wanted to create a new notepad, end it with JS in the name, and put the code for Shift key to call a Common Event.
Now I am just confused. LOL

I wanted to create a new notepad, end it with JS in the name, and put the code for Shift key to call a Common Event.
The code for detecting a press of the shift key is *already there* in the base engine, you don't need to do anything for that to work. If you want a plugin that makes shift call a common event, you still need to use the code I just gave you.
So you'd need to have your event check for Input.isTriggered("shift") and then in there call $gameTemp.reserveCommonEvent(22)


So make a new notepad, save it with the file name ending in js, and inside the notepad this code?

Input.isTriggered("shift")$gameTemp.reserveCommonEvent(22)
So make a new notepad, save it with the file name ending in js, and inside the notepad this code?

Input.isTriggered("shift")$gameTemp.reserveCommonEvent(22)
Well no, it's not quite that simply. A plugin needs to overwrite or alias an existing function of the engine in order for the code to know at what point you want that check to happen.
You also don't need to add code 16 because it already exists:
1646752961654.png

So on this, replace 16 with $gameTemp.reserveCommonEvent(22)?
So on this, replace 16 with $gameTemp.reserveCommonEvent(22)?
...not quite.

Where it would go in the code depends on where in the game you want shift to run the common event. If you want to do it on the map, you'll have to hook your plugin into Game_Map.prototype.update.
@Sakurra It seems perhaps the way keymapping works wasn't properly explained (mostly by me, ofc) 🙏

Actually, I know that already but it is my fault for not being clearer. Before I try the plugin, I'd like to try the note pad first.

I understood. But the instructions for the notepad/.js thing were only for the keymapping part. Remember I had said this whole thing occurs in two parts: first mapping the key to a "name" (AKA a string), and then linking that string to a function (i.e. calling the common event). You cannot directly connect the common event to the shift key with a single script call - you must connect the common event to the "name" that you have mapped the shift key to, in a separate script call. That isn't anyone's decision, it's just how these JS functions work.

That's why this -

Input.keyMapper[16] = 'CommonEvent22' ​

- won't work, no matter what code you put in there to call the common event. It's like trying to fit a square peg into a round hole, so to speak. The "string" between the single-quotes is not, and cannot be, a function (such as calling a common event). On its own, without being referenced by a separate script call, it does nothing. It basically just "names" the shift key, and this name can be anything you want - it's literally just something to refer to the key by in the second step.

The second script call (e.g. Input.isPressed('<string>')) is where you would check if the key associated with that name/string is being pressed. You would use this second script call in a conditional branch, so that if it returns true (i.e. the player is pressing the key), then you would have it call Common Event 22 as a result, in the way you normally would. That's where the parallel common event would come in, to constantly run this check, to "catch" every time the player presses it and then respond by calling Common Event 22. Without a parallel common event, there is no way to check for the button press and use the result of that check to call a common event, without more complicated scripting.

Personally, I don't know what that JavaScript way around this, that you could use in the notepad route, would be. To be clear, I know there is a way to do it all within a plugin (the notepad/.js thing is the equivalent of making a simple plugin), because clearly people have done it, as more complex plugins that do this do exist (such as the two linked in this thread). I just don't know how to do the crucial second step with code like that, because there's more to it than just putting a single "Input.isPressed('<string>')" script call, on its own, into the notepad. Believe me, I tried, and I even tried to manually write out a conditional branch to replace the parallel common event altogether, too - but it doesn't work. It just requires more scripting complexity than I currently possess the knowledge to figure out.

So unless somebody else finds this thread who does know how to write the correct code to make this second step happen, then you'll have to either do the parallel common event thing, or use that one free plugin. Because just trying to wedge the "$gameTemp.reserveCommonEvent(22)" script call into random parts of the core code almost certainly isn't gonna do anything (unless you get extremely lucky and put it in the right spot by pure coincidence), sadly.

Side note:
Spoiler
You also don't need to add code 16 because it already exists
The code for detecting a press of the shift key is *already there* in the base engine

Right, getting lost amidst all this I kept forgetting that the shift key is one of the keys that's already keymapped by default (to the string 'shift'). So for this particular key, you wouldn't have to do the first keymapping step at all, because the shift key already has a string you can reference it by in the second step (but all the keymapping stuff is still relevant for if you ever wanted to give a different key, that isn't already keymapped by default, a custom function).

But yes, in this particular case with the shift key, Trihan is right, there's no need to map it first. But we're still left with the crucial second step of actually having it call the common event, of which all my points above are still valid.

I hope this made any more sense than my previous explanations. I'm not the greatest (nor most concise) at explaining complicated things 😅 And it doesn't help that I've only barely dipped half of a toe into JS, and that this discussion took a turn toward way more JS-heavy stuff, outside my scope of confidence, haha~ So I only explained what I do understand, and then was honest about my limitations. I hope somebody comes along who does know how to script all of this and can help you! But if that doesn't happen, then the parallel event thing and the free plugin are still perfectly valid options to settle for that will work.
@Sakurra It seems perhaps the way keymapping works wasn't properly explained (mostly by me, ofc) 🙏



I understood. But the instructions for the notepad/.js thing were only for the keymapping part. Remember I had said this whole thing occurs in two parts: first mapping the key to a "name" (AKA a string), and then linking that string to a function (i.e. calling the common event). You cannot directly connect the common event to the shift key with a single script call - you must connect it to the "name" that you have mapped the shift key to, in a separate line of code. That isn't anyone's decision, it's just how these JS functions work.

That's why this -



- won't work, no matter what code you put in there to call the common event. It's like trying to fit a square peg into a round hole, so to speak. The "string" between the single-quotes is not, and cannot be, a function (such as calling a common event). On its own, without being referenced by a separate script call, it does nothing. It basically just "names" the shift key, and this name can be anything you want - it's literally just something to refer to the key by in the second step.

The second script call (e.g. Input.isPressed('<string>')) is where you would check if the key associated with that name/string is being pressed. You would use this second script call in a conditional branch, so that if it returns true (i.e. the player is pressing the key), then you would have it call Common Event 22 as a result, in the way you normally would. That's where the parallel common event would come in, to constantly run this check, to "catch" every time the player presses it and then respond by calling Common Event 22. Without a parallel common event, there is no way to check for the button press and use the result of that check to call a common event.

Personally, I don't know any JavaScript way around this that you could use in the notepad route. To be clear, I know there is a way to do it all within a plugin (the notepad/.js thing is the equivalent of making a simple plugin), because clearly people have done it, as more complex plugins that do this do exist (such as the two linked in this thread). I just don't know how to do the crucial second step with code like that, because there's more to it than just putting a single "Input.isPressed('<string>')" script call, on its own, into the notepad. Believe me, I tried, and I even tried to manually write out a conditional branch to replace the parallel common event altogether, too - but it doesn't work. It just requires more scripting complexity than I currently possess the knowledge to figure out.

So unless somebody else finds this thread who does know how to find the correct code to make this second step happen, then you'll have to either do the parallel common event thing, or use that one free plugin. Because just trying to wedge the "$gameTemp.reserveCommonEvent(22)" script call into random parts of the code almost certainly isn't gonna do anything (unless you get extremely lucky and put it in the right spot by pure coincidence), sadly.

Side note:




Right, getting lost amidst all this I kept forgetting that the shift key is one of the keys that's already keymapped by default (to the string 'shift'). So for this particular key, you wouldn't have to do the first keymapping step at all, because the shift key already has a string you can reference it by in the second step (but all the keymapping stuff is still relevant for if you ever wanted to give a different key, that isn't already keymapped by default, a custom function).

But yes, in this particular case with the shift key, Trihan is right, there's no need to map it first. But we're still left with the crucial second step of actually having it call the common event, of which all my points above are still valid.

I hope this made any more sense than my previous explanations. I'm not the greatest (nor most concise) at explaining complicated things 😅 And it doesn't help that I've only barely dipped my toes into JS, and that this discussion took a turn toward way more JS-heavy stuff haha~ I hope somebody comes along who does know how to script all of this and can help you! But if that doesn't happen, then the parallel event thing and the free plugin are still perfectly valid options to settle for that will work.
I know how to do it, but I figured OP wanted to figure it out for themselves. If they just want the code required, I'm happy to post it.
I know how to do it, but I figured OP wanted to figure it out for themselves. If they just want the code required, I'm happy to post it.

Oh that makes sense, and is nice to encourage room to learn and grow!

Maybe Sakurra would have better luck than I did trying to find it, haha.
So unless somebody else finds this thread who does know how to write the correct code to make this second step happen, then you'll have to either do the parallel common event thing, or use that one free plugin.


So, my next question is, is it possible with code to create a notepad with a common event called by pressing the Shift key? If so, what is the code please?
So, my next question is, is it possible with code to create a notepad with a common event called by pressing the Shift key? If so, what is the code please?
Do you want this to happen on the map?
Do you want this to happen on the map?

No, I want the Shift key globally as a Common event. If possible, the Shift key will call a Common event each time when pressed no matter where the player is.
I have a sloppy plugin to remove all key input, so you must redefine them.

another use is is ET_MapKey to use any key you desire or change midgame.
I really like this one, its better than ZE_KeyMapper, where you can defined
keys to do something else, where ET has full control (personally) as it
doesn't have any parameters and default stays the same.
No, I want the Shift key globally as a Common event. If possible, the Shift key will call a Common event each time when pressed no matter where the player is.
Right, but for the player to be somewhere they need to be on a map. 😛 I'm just making sure you don't need this to also work in battle or the menu.
Right, but for the player to be somewhere they need to be on a map. 😛 I'm just making sure you don't need this to also work in battle or the menu.
Nope, just a Common Event in the database.
So your plugin would look something like

JavaScript:
(function() {
    const _Scene_Map_update = Scene_Map.prototype.update;
    Scene_Map.prototype.update = function() {
        _Scene_Map_update.call(this);
        if (Input.isTriggered("shift") {
            $gameTemp.reserveCommonEvent(id here);
        }
    }
})();
if it ends up causing problems with your game, there is no backup
I just wanted to note that while there are good reasons not to edit the core Javascript files in your project, a lack of backups isn't really one of them – a copy of those files is stored in the main RPGMaker application folder, so if you did mess things up you could easily restore it from there.
I just wanted to note that while there are good reasons not to edit the core Javascript files in your project, a lack of backups isn't really one of them – a copy of those files is stored in the main RPGMaker application folder, so if you did mess things up you could easily restore it from there.
Agreed, the main reason not to edit them directly is just that you lose your changes when an update comes out.
So your plugin would look something like

JavaScript:
(function() {
    const _Scene_Map_update = Scene_Map.prototype.update;
    Scene_Map.prototype.update = function() {
        _Scene_Map_update.call(this);
        if (Input.isTriggered("shift") {
            $gameTemp.reserveCommonEvent(id here);
        }
    }
})();

So I can just create a notepad with this code, save as .js and that's it?

I just wanted to note that while there are good reasons not to edit the core Javascript files in your project, a lack of backups isn't really one of them – a copy of those files is stored in the main RPGMaker application folder, so if you did mess things up you could easily restore it from there.

I wasn't going to touch the main Rpgmaker application folder but this is good to know still, so thank you!
So I can just create a notepad with this code, save as .js and that's it?
Correct. You'll have to install it in the plugin manager, obviously.

Original thread: https://forums.rpgmakerweb.com/threads/script-for-changing-button-functions.144620/