Game Message and Choice dynamic text code Tutorial

Discussion in 'RPG Maker MV Tutorials' started by Aryam777, Mar 14, 2017.

  1. Aryam777

    Aryam777 Villager Member

    Messages:
    17
    Likes Received:
    10
    First Language:
    Polish
    Primarily Uses:
    RMMV
    Messages and Choices via Scripts and Dynamic Text Codes

    It has been very difficult to find an answer for this, as well as to find a cohesive explanation of choice scripting. Google searching and copy pasting always makes the console to dump an exception on you... While trying to overcome the problem I noticed many rpg's still have it... Disappearing messages before choices, having to make choices based on how well the player has to remember the characters stats...
    Or having to write "If this then write message "he can't use this", If that then write message "he can't use that"...
    That's the problem with Text Codes. They're inherently non-dynamic.
    You can display a variable's value using text codes... But what if you want to use that variable to display something else, like the proper Icon? \i[x]... But you have to type in the 'x'. The 'x' can't be a variable. It's got to be hard coded into your message.
    Well no more. :)

    First and foremost the RPGMMV Command you need to use is the last one... The dreaded "Script" command. But you should not fear it. Sometimes all it does is let you reach for some variable or make a simple calculation here and there in more simple and neat ways than a plugin would.
    All script commands are separated with ";". Which means you can and unfortunately have to place multiple commands in one line, because the "Script" command space is limited to only 12 lines of code... But each line can be as long as you like. :)
    Now writing like this:
    *code*; *code*;
    is much more messy than like this:
    *code*;
    *code*;
    But fear not. You can write your code neatly in a notepad and right before copy pasting it into your "Script" command you can make a double copy of your code somewhere below and safely BACKSPACE through all your new lines. Then copy and paste that mess you made into the "Script" command. It will work as in javascript code new lines are completely meaningless and serve no purpose but to make to code look nicer. :)
    And if you have two copies of your code, you can always make bigger and more difficult changes in the first, neat copy of it before making a mess of it, that will fit inside the "Script" command window. :)

    So first we will start with scripting up a message window with dynamic text codes. Dynamic text codes can allow you to use different variables depending on some conditions. Like use a previously picked item and it's icon in a text. In this way:

    First a player has to pick an item, what you can prepare through Event Command - Pick Item
    DynTCode1.jpg

    This will result in the following question screen:
    DynTCode0.jpg

    Now simply use the variable that stored the item's ID to point an item text code to the right item ID in the database with the \ii command:
    DynTCode2.jpg

    This will implement your item's ID value within the \ii[x] text code yielding the following result:
    DynTCode3.jpg

    Now for the explanation and script calls displaying the text with a dynami variable:

    Start your script with this:
    $gameMessage.setFaceImage('Actor1',0);
    This line sets the face image attached to the message. First argument is the file's name (without extension),
    and the other is faces index counting from 0-8.

    $gameMessage.setBackground(1);
    This sets the background of the message window as follows: 0 - bordered window, 1 - dimmed borderless, 2 - completely transparent.

    $gameMessage.setPositionType(2);
    This sets message window's position on screen: 0 - top, 1 - center, 2 - bottom

    var charName = $gameActors.actor(1).name();
    var itemName = $gameVariables.value(5);
    var msgIconID = $gameVariables.value(7) * 20 + $gameVariables.value(10);

    With this you can set variables for things you want to use within your displayed text or even within choices. Those can be results of formulas, and in any format, and can contain text. Those variables can contain icon ID's too if you want to use icons in your text. There can be as many as you like. Just separate every new variable and it's value with a ' ; '.
    Also you can store text in game variables by using the standard interface's "Control Variables" command and simply picking "Script" and in the script line just type your text within
    "quotes". Text codes don't always work when stored here tough.

    Okay, time to start displaying the actual message. This can work together with all plugins including Yanfly's Extended Messages:

    $gameMessage.add(charName + " can't use \\i[" + msgIconID + "] " + itemName + " here...\nWhat's he going to do now!!!?");
    Let's analyze this...:
    • You can place any variable within your text by simply ending the "quotation" adding a '+' sign and the name of the variable. Ten another '+' sign and start "quotations" for another message.

    • You can use dynamic text codes by placing a variable within it as if you were placing it within a tex. CAUTION: you have to add two ' \ ' for a text code (since it will be going through two interpreters - javascript's and the RPGMMV's - and both use ' \ ' as a text code marker). So in a this codes text $gameMessage.add("\\i[" + msgIconID + "]") would display the 7th icon from the icon sheet, right within your text. You can replace 'i' with 'ii' or any other plugin text code and it will work too!

    • One more important thing! If you're not using and word wrapping plugins you need to mark new lines in your text. That's where the '\n' text code comes in. It's not an RPGMMV text code, it's a java script sort of "text code". That's why we use only one '\' here. '\n' replaced by a <new line> sign. That's why we need to add double '\'s for text codes. Since java script ALSO replaces '\something' with an operation or a cool twist and '\\' mean - replace with a single '\' which is what we want to pass to the game interpreter to mark a text code like "\i[7]"

    • Now if you want to wrap things up for a dynamic text code message. End of script means the end of message, waiting for button input and then closing the message. After this message you can continue adding new messages or commands through RPGMMV UI Events menu.





    ...But what if you want a choice list to appear with the message still present? And with dynamic text codes in those choices? Like this:
    Data.jpg
    Here's how you have to continue your script for both the RPGMMV Show Choices event command used right after your message Script and through scripting the choices yourself to make them dynamic. This was the part I found was very hard to do due to a lack of consistent info on scripting that:


    Option 1: Use RPGMMV UI's Show Choices Event command
    DynCCode1.jpg
    First part of the code is the script similar to what we've already used to display an "item can't be used here message" with a slight modification at the end and we will learn to use that modification together with the second part which is the Show Choices Event Command. The following has to be used to avoid messages disappearing before the Choice Window appears.

    If you want RPGMMV Show Choices command to appear with your message still open add the following:
    this._index++;
    This mysterious line will tell the game code interpreter (which is accessed by "this") to move one step ahead to your "Show Choices" event command which should be next on the event command list, before the end of your message script (before closing it).

    this.setupChoices(this.currentCommand().parameters);
    This line will tell the interpreter to load all choice window parameters up from the position (hence "currentCommand") the interpreter just moved to, and to display them.

    this.setWaitMode('message');
    The use of this line is very important and complex to explain. If you want your choice branches to execute you need to add this. Without that choices will be purely cosmetic. When you just setup choices for the interpreter it still waits for your messages input, NOT choice's. And giving the game this button input also means another 'this._index++' command... And since the interpreter is already on your choice list it immediately moves to the next step without executing the choice code. Adding the "setWaitMode('message')" line forces the interpreter to wait for your game's choice list input instead of message input (the value 'message' (with the quotes) here is non-optional and can't be something else). And choices input means - do "*code*;" and THEN after that code "this._index++;" so your choice branch code gets executed. :)
    That's the end of your script. And both dynamic message and non dynamic choices should work fine.


    Option 2: Use Choices Menu With Dynamic Text Codes Via Scripts
    DynCCode2.jpg
    Marked in red is the part of the script that we will use at the end of the script to substitute the regular Show Choices Event Command, with some dynamic choices that can contain icons and data that changes depending on variables and formulas.
    Since we're at it this tutorial will also explain HOW to use the scripts to control Choices menu without it conflicting with anything else. The marked area will have the same effect as in the previous option... Except here instead of writing down every choice in the Show Choices Event command, the choice list uses variables that can be modified as those choices.
    Take the variable "str" for example.
    In line 1. you can see it's declaration and that it consists of text and the value of a variable that holds the strength's icon and another variable that holds it's name.
    In line 2. setChoices function uses that variable as the first choice. It will include a pointed icon and a pointed name. The choice can be built out of any number of variables you like, as the variables used for choices are stored in text form. Just don't make them too long.
    Further along is an explanation of all the steps you need to take to use a fully customiseable Choice Window using scripts.


    1. That's where it all starts being tricky and the part I couldn't find cohesively introduced anywhere on the interned... Even the famed "RMMV Script Calls List", which besides this part is utterly wonderful and I advise anyone to familiarize with it as it takes away... basically all limitations of the built in GUI options. :)
    If you want to script dynamic choices with adjustable icons, values, and text of your game's variables,
    replace Option 1's code like this...


    var ch0 = "no...";
    var ch1 = "destroy \\i[" + msgIconID + "] " + itemName + " in blind rage";
    var ch2 = "use on \\p[2]";
    var cancel = "Cancel";

    This is a good place to set up your choices options within variables. Same rules as for the message applies.

    $gameMessage.setChoices([ ch0, ch1, ch2, 'cry', cancel], 1, 4);
    2. This prepares the choice window.
    - The things within [brackets] are your variables that make the choice options. YOU CAN break the 6 choice limit here and add more! Instead of variables you can add 'as is' choice options within
    'quotes', like 'cry' which is the
    4th choice here.

    - The first number after the [brackets] means the default choice... And since choice numbers start from '0' in this case '1' would mean that destroying the item option would be the default one. :)
    - The second number after the [brackets] is the canceling choice. Here you can type '-1' (without the quotes) as a value which means cancelling is not allowed for this choice. In here we have a '4' which means it will be the 5th choice (choices count from 0!) - "Cancel" which later on simply closes the window.

    $gameMessage.setChoiceCallback(function(responseIndex) {$gameVariables.setValue(100, (responseIndex+1));} );
    3. NOW this is the final and the hardest part. Here's where you determine what code will each choice execute. The easiest way for non-scripters or those who are not Lunatic Mode fans would, however, be to input this line as I just did here.
    Basically what this code does is to store which choice the player picked within a 100th game variable (you can change this number to any empty game variable you have, just remember to name it to avoid making a mess), which you can use in the next RPGMMV Command the "Conditional Branch..." which will be dependent on the value of that variable.
    Remember that the choices are numbered from 0, so the "
    responseIndex+1" value will change that to the choices being numbered from 1. You can remove the "+1" and do your conditional branch from a "0" (remember the results are choice NUMBERS not it's text).

    However... IF you ARE a lunatic mode fan (as I am) you can place within the
    {brackets} of this line any piece of code you like, involving many commands.
    Like this:

    $gameMessage.setChoiceCallback(function(responseIndex) {
    if ($gameVariables.value(100) == 1) $gameTemp.reserveCommonEvent($gameVariables.value(77));
    if ($gameVariables.value(100) == 2) {$gameVariables.setValue(50, 7); $gameVariables.setValue(500, itemName)}
    } );

    OR do the same thing using a 'switch' statement, like this:
    $gameMessage.setChoiceCallback(function(responseIndex) {
    switch (responseIndex) {
    case 1: $gameTemp.reserveCommonEvent($gameVariables.value(77)); break;
    case 2: {
    $gameVariables.setValue(50, 7);
    $gameVariables.setValue(500, itemName); }
    break;
    default: $gameTemp.reserveCommonEvent($gameVariables.value(70));
    } );

    The "break;" line otside of your code means to exit the "switch" statement. Without "break;" int the "case: 2" code, even after executing the code for "case: 2" the code wouldn't stop and continue executing also for "code: 3" which can be useful sometimes, but not usually. :)
    The "default:" line is a special "case" of the "switch" statement meaning "if none of the above cases have applied do this". If you input the "case:" for every possible choice, however, this in not needed.
    If you use the "
    if" statements you should make an "if" for every possible choice number or picking that choice will do nothing.

    4. And finally:
    this.setWaitMode('message');
    This line's meaning is the same as in Option 1. and remember, the value 'message' (with the quotes) can't be changed to anything else!


    There I hope it will help somewhat to avoid choices confusion and help display data for the choice which many RPGs could use!
    If you have any advice to change this explanation to become more clear please do share. :)
    Cheers fellow RPG makers! :D






    SAMPLE CODE TO COPY AND PASTE:

    EDIT: Ok here's some example template code you can test in your projects. Just copy paste it into your notepad or notepad++ to edit the code and play with it to your liking. Then you can copy-paste it to your Map Event's Script command or a Common Event's Script command. Just remember to trim the number of lines this code has to fit into the Script box!
    This code will show you a message using the 1st face from the file "Actor1.png", 2nd actor's name (probably Theresa), and the 7th icon form your project's icon set.
    Right after the message is written you will get a choice menu containing your choices, one of which will contain the same data as the displayed message.

    This template needs two things to work "as is":

    1. To set a value for the 7'th game variable - this value will be the inex of an icon you want displayed in your message... You can ofcourse change this variable's number to any other variable number you want. Best pick something that's not being used already.

    2. To add a Conditional Branches Event Command from the Event menu, that will utilize the 8'th game variable (clear that variable or change the code to utilize another one). To test the results you should make one condition for any choice index you like
    Choosing "no..." will set the variable to 1
    Choosing "destroy sth sth..." will set the variable to 2
    Choosing "use it on sb..." will set the variable to 3
    Choosing "cry" will set the variable to 4
    Choosing "Cancel" will set the variable to 5
    You do not need to make a Conditional Branch for every result. Results you won't do any code for will just cause your game to do nothing. You can omit "Cancel" here, for example, as it's only supposed to close the window and do nothing.

    An example of how to prepare the Event Command list would be this:
    SampleCode.jpg
    This will set the 7th variable to 7 before running the script...
    And after running it will display "Item destoryed..." text if the player chose option 2.
    "choice result" in the Conditional Banch here (after the If) is the 8th game variable.

    You also shouldn't have Actor 2 cleared for this code to work. In the default project configuration it's Theresa, but it can be a different character... This Actor just can't be empty.

    OK, now here's the script code:

    $gameMessage.setFaceImage('Actor1',0);
    $gameMessage.setBackground(1);
    $gameMessage.setPositionType(2);

    var charName = $gameActors.actor(2).name();
    var itemName = $dataItems[1].name;
    var msgIconID = $gameVariables.value(7);

    $gameMessage.add("I'm afraid " + charName + " can't use \\i[" + msgIconID + "] " + itemName + " here...\nBetter try something else.");

    var ch0 = "no...";
    var ch1 = "destroy \\i[" + msgIconID + "] " + itemName + " in blind rage";
    var ch2 = "use it on \\p[1]";
    var cancel = "Cancel";

    $gameMessage.setChoices([ ch0, ch1, ch2, 'cry', cancel], 1, 4);

    $gameMessage.setChoiceCallback(function(responseIndex) {$gameVariables.setValue(8, (responseIndex+1));} );

    this.setWaitMode('message');





    Here is a trimmed version fitting the Script Box (it's the exact same code as above, just without some line breaks)... You can copy-paste this as-is into a script box and it will work (just set the game variable 7 before running this script, it can be a step above it in the event menu or it will result in "undefined".
    Also do something with the results stored in game variable 8 to check if it works.):

    $gameMessage.setFaceImage('Actor1',0);$gameMessage.setBackground(1);$gameMessage.setPositionType(2);
    var charName = $gameActors.actor(2).name();var itemName = $dataItems[1].name;var msgIconID = $gameVariables.value(7);
    $gameMessage.add("I'm afraid " + charName + " can't use \\i[" + msgIconID + "] " + itemName + " here...\nBetter try something else.");
    var ch0 = "no...";var ch1 = "destroy \\i[" + msgIconID + "] " + itemName + " in blind rage";var ch2 = "use it on \\p[1]";var cancel = "Cancel";
    $gameMessage.setChoices([ ch0, ch1, ch2, 'cry', cancel], 1, 4);
    $gameMessage.setChoiceCallback(function(responseIndex) {$gameVariables.setValue(8, (responseIndex+1));} );
    this.setWaitMode('message');
     
    Last edited: Mar 15, 2017
    #1
    WickedWolfy, dahlys, Poryg and 4 others like this.
  2. mlogan

    mlogan Global Moderators Global Mod

    Messages:
    13,607
    Likes Received:
    7,520
    Location:
    Texas
    First Language:
    English
    Primarily Uses:
    RMMV
    I'm going to approve this, but do you think you could provide a screenshot or two of what this actually looks like in game, so people can better understand what you are teaching them?
     
    #2
  3. Aryam777

    Aryam777 Villager Member

    Messages:
    17
    Likes Received:
    10
    First Language:
    Polish
    Primarily Uses:
    RMMV
    Thanks for the advice. I have reviewed the post and fixed some spelling errors and added some more explanations and pictures. I will also soon add a copy-paste script template to play with in someone's new project.
     
    #3
  4. jameswestbrook351

    jameswestbrook351 DecayingDev Veteran

    Messages:
    58
    Likes Received:
    13
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    I don't fully understand this post, but from what I am reading this may allow me to script it so that I can hide/show certain choices depending on switches and variables. Definitely going to read further until I figure this out.
     
    #4
  5. dahlys

    dahlys Meatbun Veteran

    Messages:
    91
    Likes Received:
    86
    Location:
    California, USA
    First Language:
    English
    Primarily Uses:
    RMMV
    I was trying to add a message in the middle of a for loop and the dynamic text syntax was killing me :(
    And then your post solved all my problems XD Thank you~
     
    #5
  6. Rhino

    Rhino ~Inactive~ Veteran

    Messages:
    482
    Likes Received:
    781
    First Language:
    English
    Primarily Uses:
    RMMV
    Hello, just thought I'd share something I discovered with you!

    I tried copying what was written for the select item command, before realising that \ii can only be used if you've got Yanfly's Message Core. The other solutions, using var itemName = $dataItems[x].name;var msgIconID = $gameVariables.value(n); aren't dynamic, since the item name is using an item ID, and the msgIconID variable is taking the value of the variable to be it's icon, (which is in this case is the Item ID) rather than the icon index of the item stored within that variable.

    But after playing around I found you could use a variable within the []'s, so came up with this solution;
    var itemName = $dataItems[$gameVariables.value(n)].name;
    var msgIconID = $dataItems[$gameVariables.value(n)].iconIndex;

    This'll take the item name and icon from the variable set with item ID (e.g. via a select item command.)

    If you don't want to use a script call, you can set these two to variable:scripts and use \v[x] for name and \i[\v[x]] for icon.

    I admit that this was a struggle for me as a non-codey person, so to make it more accessible here's a copy of my script that can be copied +pasted in! (Make sure that you've set the variable beforehand.)
    Code:
    $gameMessage.setBackground(0);$gameMessage.setPositionType(2);
    var itemName =  $dataItems[$gameVariables.value(7)].name;var msgIconID = $dataItems[$gameVariables.value(7)].iconIndex;$gameMessage.add("You've chosen a\\i["+ msgIconID +"]"+ itemName +"!");
     
    #6

Share This Page