Read game variable as JS code?

For example, lets say that in some part of my game I let the player write what ever he wants(It then is saved in a Game Variable).

Would it be possible to then read whats saved inside that variable as a JS script and execute it?
Read in what context, what exactly do you want to do?
To read it to the player in a message box, just type \v[x] in the Show Text event command, where x is your variable ID#.
Read in what context, what exactly do you want to do?
To read it to the player in a message box, just type \v[x] in the Show Text event command, where x is your variable ID#.
I just notice now that i forgot to say for which reason xd

I meant read as execute.
You mean if the player types in something like:
"$gameParty.gainGold(99999);" ?

And you want to evaluate it as Javascript.

In a script call, use:
Code:
eval($gameVariables.value(X));

Obviously, as I've demonstrated this allows a player to hack your game.
You mean if the player types in something like:
"$gameParty.gainGold(99999);" ?

And you want to evaluate it as Javascript.

In a script call, use:
Code:
eval($gameVariables.value(X));

Obviously, as I've demonstrated this allows a player to hack your game.
Well it's not so bad. I wish to make a terminal, which will be hard to find and even more to learn the commands, where players may write things to get hidden information and stuff that may not be so clear during gameplay.


By the way, I'm having some problems with it now. I've made a system where player could write JS in a variable using one command and then execute it using another(/write_JS and /read_JS). But for some weird reason, when I try to manipulate it on the read part, things go wrong.

Image:
https://i62.servimg.com/u/f62/15/27/47/75/112.png
https://i62.servimg.com/u/f62/15/27/47/75/213.png

So i'm saving whats written using a simple +=1 system. When player writes a JS, its saved on $gameVariable( value of variable 17+1), and it adds +1 to variable 17 which starts on 17, then when read, system will copy the value saved in that variable to variable 13 and execute. In other words, it saves each code from gamevariable 18 until infinite. BUT when i try to read that, since player thinks i'm saving it in an order that starts with 1, I must change the value given after "/read_JS" command to its value + 17. But no matter what I do(And i tried both simple way, using "Controll Variable" event command, and script way, adding +17 in front of the code), it adds 17 before or after the number, never sum.

Could you help?
I think I understand what you're trying to do. Is this correct?
  1. A player would type in:
    • /write_JS $gameMessage.add('Hello World');
  2. Variable #17 used to have a value of 17, now it has a value of 18.
  3. That string gets saved into variable #18
  4. The player types in:
    • /read_JS 1
    • This is because to them, the Hello World command is the first one they typed, so it is command #1
    • Now you need to execute the first command they typed, which is actually not #1 but in fact it is #18
No problem.

Your issue is just a simple type conversion issue between strings and numbers, and it happens all the time.

Open the console (press F8 in playtest) and type in the following and note the difference.
Code:
console.log(17 + 1);
//This is a number 18
console.log(17 + "1");
//This is a string 171

In Javascript, the + symbol is used for adding numbers but it is also used for concatenating strings. So when it sees 17 + "1", it defaults to string concatenation and that's why you are getting a string "171".

You'll have to convert the "1" into a 1 by using the parseInt function.
Code:
$gameVariables.setValue(13, 17 + parseInt($gameVariables.value(13).slice("/read_JS ".length)));
Rather than allowing the player to write JS directly into the console, why not develop a slightly different custom syntax for the player to write and then convert it to JS to execute in the code so that the player can't manipulate it? That way they'd only be able to execute code you've allowed in your player syntax.
It's all working fine, Thank you ! But.... Commands that should pause the game and wait ending before execute another thing (Like this Game message) are not doing so.
The game is showing the message and instantly executing the next action. Is there a way around that?

Rather than allowing the player to write JS directly into the console, why not develop a slightly different custom syntax for the player to write and then convert it to JS to execute in the code so that the player can't manipulate it? That way they'd only be able to execute code you've allowed in your player syntax.

Honestly I'm fine with player being able to write any code he wants, since it's not going to be a RPG, so it doesn't matter if he get gold or whatever using that system. My game is going to be a horror game, so unless player knows a lot of what he's doing he won't hack into nothing important xd
I agree with Trihan, better to define the finite amount of use cases of what the player is allowed to do and design for that. Strings can be very long and someone could re-write anything about the game, if I was playing your game I would make it so the player never takes any damage 😉 But, it's your game, so it's your choice about what to do.

There's a thing called the Game Interpreter which essentially takes the Event Commands that you use and interprets them into Javascript for the engine to run. It also handles everything that has to do with waiting (waiting during a message, during a move route, during an animation), so if something like that is executed outside of the Game Interpreter, then the waiting won't happen.

Original thread: https://forums.rpgmakerweb.com/threads/read-game-variable-as-js-code.91330/