I'm trying to do something like what is pictured but I don't know the correct way to write the scriptcall portion.
Is there a way to assign the text of the Choice selected to a variable?
RPG Maker MZ SupportLooking at your screenshot, you would just add 3 Variable Operations, one for each Select Option.
Tip:
The commonly used command to set a Variable is
Code:
When Red:
Variable OP: Set 1 to "Red"
When Blue:
Variable OP: Set 1 to "Blue"
...
Tip:
The commonly used command to set a Variable is
$gameVariables.setValue(id, value)Yes, I'm aware that I can individually set variables.
For example:
But if I have lots of choices, or even choices that are variable strings themselves instead of static text such as choice 1: \v[501] choice 2: \v[502] choice 3: \v[503] for example, setting each individually becomes tedious.
So I was hoping for a way to select any choice and apply the text within the selected choice to a variable as in first picture I posted. I simply don't know the format for getting the choice text.
Create Plugin with any name like this:
Replace the 1 with your Variable Id. Every time the Player picks a Choice, its Text Value is stored in this Variable. You can then do whatever you want.
Let me know whether it's working for you.
Code:
(function() {
const alias = Game_Message.prototype.onChoice;
Game_Message.prototype.onChoice = function(n) {
const value = this.choices()[n];
$gameVariables.setValue(1, value);
alias.call(this, n);
}
})()
Replace the 1 with your Variable Id. Every time the Player picks a Choice, its Text Value is stored in this Variable. You can then do whatever you want.
Let me know whether it's working for you.
Thank you very much! This works perfectly and will save me (and likely others) a great deal of time!
I apprectiate you stopping to help. Have a good day!
After further experimentation I've figured out something when using variables for choice text!
While they will display correctly in the game's user interface, behind the scenes in the console the choice is still set to the formula calling the data, not the actual data itself.
Meaning if I set var 501 to "Fire" then write \v[501] as the name of a choice, selecting that choice does not actually save "Fire" to the variable it instead saves "\v[501]" .
So if I use the functionality of the provided .js to get the selected choice's text into a variable and then use it inside a message window it will show "Fire" (as long as var 501 still holds that text), but if I use it in a scriptcall (such as to call a video) RPG Maker won't search for "movies\fire" it instead searches for "movies\\v[501]"
I wonder if there is a way to remedy this?
(and I hope I explained that properly... This picture is a visual)
Does this work (100 here is the ID of the variable where you store the choice in the plugin, also your show choices variables have to be 3 digits like 501 etc)?
After the show choices:
After the show choices:
$gameVariables.setValue(100, $gameVariables.value(100).substr(3, 3));
$gameVariables.setValue(100, parseInt($gameVariables.value(100), 10));
name of video: $gameVariables.value($gameVariables.value(100))
Yes, that's true. The choices window renders \v[501] to the Variable's value, but the text value is still "\v[501]". I think the best way to convert \v[501] to its value is this snippet:
Code:
const string = '\\v[501]';
const variableId = Number(string.toLowerCase().replace('\\v[', '').replace(']', ''));
const value = $gameVariables.value(variableId);
name of video: $gameVariables.value($gameVariables.value(100))
gives me 501
so i needed to plug that in as a variable like so:
name of video: $gameVariables.value($gameVariables.value($gameVariables.value(100)))
It works though! Now thanks to you and Aerosys I have a plug and play infrastructure for choices where I never even have to put anything in the choice action section!
This is a huge timesaver for something like selecting a song to play where there is a folder full of files and the only difference in the script running would be the filename of the song.
Original thread: https://forums.rpgmakerweb.com/threads/is-there-a-way-to-assign-the-text-of-the-choice-selected-to-a-variable.160424/