RPG Maker MV

RPG Maker MV

Not enough ratings
Show conditional text using string variables
By MAKAIROSI
In rpg games we often have more stats that gain levels than just the level of a character. The character may also have "swordsmanship", "archery", "cunning", etc. All these may need their own xp to levelup, and, since the combinations are so many, you can't put "Show Text" commands inside conditions. In this guide we will learn how to simply create a text that has conditional information with a single "Show Text" command without using any plugins and (almost) without using any script.
   
Award
Favorite
Favorited
Unfavorite
Introduction
So i was making a game and i came upon a problem. I wanted to show xp gains depending on what the player was doing at the time, but the "show text" command doesn't support conditions inside it and if i put it inside a condition, then i would have too many combinations. So i had to come up with an idea of how to integrate conditional text inside my text window. I am making this guide to show my solution to you, hoping it will help you in your own games.

In this example we will have 4 stats that gain levels. "Swordsmanship", "Archery", "Cunning" and "Tactics". The reason we have 4 is because showing all combinations would then need 16 "show text" commands. However, we will make it with just one text window.
1. Setting up initial integer variables
Create an Autorun event that we will use for setting up our initial variables. This event will only be used once, so end it with a "self-switch A=true" and make a second page with "self-switch A" as a condition, action trigger and nothing inside

Now, back to the first page:

VswordLevel=1
VarchLevel=1
VtactLevel=1
VcunLevel=1

These are the starting levels of our skills. Let's assume they always start with level 1.

VswordXp=0
VarchXp=0
VtactXp=0
VcunXp=0

These are the variables that hold our current xp for each skill. I assume they start at 0 xp.

VswordCost=40
VarchCost=40
VtactCost=40
VcunCost=40

These are the xp costs for our skills to levelup. So when these are reached, the skill will levelup.

VbaseXpGain=5

This is how much xp each skill will gain for each action.

For the next variables we will also need their IDs. So i will provide them below:

ID11: VswordAdd=0
ID12: VarchAdd=0
ID13: VtactAdd=0
ID14: VcunAdd=0

You don't need to set up the ones that are 0 since they are automatically set to 0 at the beginning of the game. I am showing them to show which variables we will be using and their IDs.
2. Setting up initial string variables
A string variable is a variable that holds a series of letters instead of numbers. The question that comes in mind is "how on earth do i make a variable hold a string?". Well the answer is pretty simple.

Control Variables -> Vstring1 -> Script -> "Enter string here" (with quotes)

Now the variable, when called, will show:
(text window starts)
Enter string here
(text window ends)

So now let's set these up. The first string variable we need is an empty string, so that it can be called whenever we don't want to show anything. Now, the string can never be empty (unfortunately, when it's empty it just displays a 0), so we will just put the default color code inside:

ID:20 VstringEmpty="\\c[0]"

Note the double backslashes there. They need to be double, because a double backslash in a text window is transformed to a single visible one. When the string is called in a text window, the single one will initiate a text code.

Now we will need to set up string variables for when it is time to show a skill xp gain.

ID: 21 VstringSword="Swordsmanship +\\v[11] XP"
ID: 22 VstringArch="Archery +\\v[12] XP"
ID: 23 VstringTact="Tactics +\\v[13] XP"
ID: 24 VstringCun="Cunning +\\v[14] XP"

Note that just like color codes, you can include other variables inside, but always with double backslashes. Now they will display our "Add" variables for each different skill.
3. Variables of Variables
Now we have to make a variable whose ID is another variable. So let's make the variables of the IDs first:

ID: 31 VidSword=20
ID: 32 VidArch=20
ID: 33 VidTact=20
ID: 34 VidCun=20

Remember, ID: 20 is an empty string variable. So now let's go on to make the variable we needed at first:

ID: 101 VshowSword="\\v[\\v[31]]"
ID: 102 VshowArch="\\v[\\v[32]]"
ID: 103 VshowTact="\\v[\\v[33]]"
ID: 104 VshowCun="\\v[\\v[34]]"

With this, our final text window, in the very end of this guide, will only have to show:

\v[101]
\v[102]
\v[103]
\v[104]

But we will end the guide with this. Before we do this, we need to set up a common event.
4. Common Event "Conditions"
Now we come to the crucial part. These conditions are the ones that choose to display text or leave it blank. Here is a list of what we know so far, that will help us better understand this:

ID11: VswordAdd=0
ID12: VarchAdd=0
ID13: VtactAdd=0
ID14: VcunAdd=0

ID: 21 VstringSword="Swordsmanship +\\v[11] XP"
ID: 22 VstringArch="Archery +\\v[12] XP"
ID: 23 VstringTact="Tactics +\\v[13] XP"
ID: 24 VstringCun="Cunning +\\v[14] XP"

ID: 31 VidSword=20
ID: 32 VidArch=20
ID: 33 VidTact=20
ID: 34 VidCun=20

Now to create the conditions:

IF VswordAdd>0, VidSword=21
ELSE VidSword=20

IF VarchAdd>0, VidArch=22
ELSE VidArch=20

IF VtactAdd>0, VidTact=23
ELSE VidTact=20

IF VcunAdd>0, VidCun=24
ELSE VidCun=20

In this manner, whenever we have xp to add to a specific skill, the specific variable ID will be called to show we did that, while if we don't have any xp to add, we just call the empty string, not showing anything.
5. Common Event "Levelup Checks"
We need this event to check whether any of our skills has leveled up. Simply:

IF VswordXp>=VswordCost

VswordLevel+=1
VswordXp-=VswordCost
VswordCost*=2
Show text->"Swordsmanship level up!"

IF VarchXp>=VarchCost

VarchLevel+=1
VarchXp-=VarchCost
VarchCost*=2
Show text->"Archery level up!"

IF VtactXp>=VtactCost

VtactLevel+=1
VtactXp-=VtactCost
VtactCost*=2
Show text->"Tactics level up!"

IF VcunXp>=VcunCost

VcunLevel+=1
VcunXp-=VcunCost
VcunningCost*=2
Show text->"Cunning level up!"
6. Seeing it in action
So now we have everything set up to see it in action. So let's say your character is at a series of decision making points:

Show text->"An enemy goblin appeared! You quickly grab for your..."
Show choices->Bow, Sword

When Bow
VarchAdd+=VbaseXpGain
Show text->"...bow and shoot it down!"

When Sword
VswordAdd+=VbaseXpGain
Show text->"...sword and slice it in half!"

Show text->"As you were battling it, another one came from behind you! You quickly thought that it would be wise to assume a better position, or to pretend you didn't see it! You..."
Show choices->Position, Pretend

When Position
VtactAdd+=VbaseXpGain
Show text->"...jump forwards to gain some distance, while turning around to face it!"

When Pretend
VcunAdd+=VbaseXpGain
Show text->"...pretend you didn't see it, so that you catch it off guard!"

Show text->"The goblin marches towards you with its weapon pointing at you! You decide to..."
Show choices->Bow, Sword

When Bow
VarchAdd+=VbaseXpGain
Show text->"...use your bow and shoot it down!"

When Sword
VswordAdd+=VbaseXpGain
Show text->"...use your sword and slice it in half!"

Run common event "Conditions"

Show text dim,top->"Fight Results"
Show text dim,middle->

\v[101]
\v[102]
\v[103]
\v[104]

VswordXp+=VswordAdd
VarchXp+=VarchAdd
VtactXp+=VtactAdd
VcunXp+=VcunAdd

Run common event "Levelup Checks"

VswordAdd=0
VarchAdd=0
VtactAdd=0
VcunAdd=0

Et voila! Now this simple 4-line text covers ALL combinations of xp adding, even if you had 100 choices one after the other. You could make a huge battle, with potions that raise xp gain, with specific level requirements for specific actions, with time travel enabling you to go backwards in your choices if you have enough energy, etc etc. It would all end to a single text window of the fight's results.
Some Notes
Always remember that in string variables, ANY text codes must have a double backslash "\\".

EDIT: Except at least "\n" (some plugins use this). In my situation, i use a plugin that allows larger item descriptions, changing lines using "\n". If i put a string variable inside the description, then the string variable must be like this: "Type: Sword \n \\c[18]Attack 16 \n \\c[23]Defense 5" etc.

You could work with Yanfly's message plugins to create a large message window of results, since it supports up to 16 lines. This could mean that you can have up to 16 different skills displayed there. The logic is the same as the one we followed in this example.

Yanfly's plugins would also enable you to Disable or Hide Choices, depending on conditions - for example if your swordsmanship skill is too low. You could go wild with these conditions. For example, a cunning option which uses your sword would require cunning but would also award sword xp.

You could also work with other plugins that allow events at battle-end to incorporate this logic in your actual battles! A swordsmanship skill can then do what you want it to do and add swordsmanship xp additionally!

Always remember to initialize your variables so that they aren't added in the next instance. In my opinion, you should always have START variables, that hold the initial values, so that you don't always have to initialize them with numbers. Also, this could come really handy if you want to change a variable temporarily, like xp gain potions, that would raise your xp gain for this battle, but their effect would wear off after it. Then, instead of doing xpgain=5, you would just do xpgain=xpgainSTART

You could also add color codes to the strings to better show the skills you are showing. For example, cunning could be purple, so in the appropriate string you could add a \\c[31] at the start. Alternatively, you could always include all the xp gain strings, but keep them white if they don't show any change, or green when you gain xp. Although the code would need some transformation, the logic is pretty much the same.

To enhance gameplay, it's not wise to always have the player know which stat they are affecting by which option. Drinking a health potion just before you die might be "cunning" while drinking a health potion early on might be "tactics". The point is to categorize truly tactical and cunning actions and assign them according to the player's playing style. For the player, it would be intuitive that drinking a potion early on means thinking ahead (therefore "tactics"), without having to see it written.

These skills may unlock additional options, or even passages, in the game map. "Cunning" for example could unlock passages. "Tactics" could unlock starting a fight with buffs, since the party assumed a more tactical position (for example "this cliff is perfect as an advantage point! Let's draw them here!")

Typos:

As always, i could have made typos, which are crucial when writing guides like this which show code - or at least pseudo code. A single typo could make the whole thing not work. So please, if you find any, tell me.
7 Comments
Resi ♥ 1 Apr @ 11:45am 
Oh yes in my game that "Perch Lure" is actually an item that has to be attached to a Lures Box that the main character has. If it's not "Augmented" to the Lure Box, the bonus doesn't actually occur. That's done in code checks, I just used "has Item" as a simple event replacement for someone else who might consider items as passive checks.
MAKAIROSI  [author] 1 Apr @ 11:24am 
This is good! The way I wrote the guide was without having to write any JS - if you do, things become much simpler really.

Something that I'd add to yours is that at the line "if party has <itemName>" this will translate to if they have it in their inventory, and not equipped - meaning it's a passive effect.

Personally, I prefer to have passive effects in a separate category in skills, rather than as an item.

But if it works, it works!
Resi ♥ 1 Apr @ 10:53am 
Decided to reverse order of my comments for readability.
This guide is very useful (I mean this in a good way) on how to do rote-level Javascript coding within the RPG Maker editor itself. People should note that what they're doing is a form of coding, as you could achieve everything inside of an event from a script call. For instance, I made a plugin that runs a function for that map. The plugin is literally extra code I wrote for only one map to use. A plugin - for 1 map - in 1 game - entirely for the developer and no one elses.

A game's systems should be structured, but they do not need to be and may not always be able to used anywhere. Your systems should serve a purpose and much like the leveling system created here in this wonderful guide you should have a framework for knowing how to write a level formula as you will be creating a whole new stat that the player can technically access and manipulate, so it should come with code that address the new stat specifically.
Resi ♥ 1 Apr @ 10:50am 
Speaking of, I have an addition to add. I have Fishing and Mining levels in my game. This is the EXP formula I use and the event structure (function) that adds a new level. It has a carry-over for excess EXP. It also tracks the total EXP gathered during play.
Resi ♥ 1 Apr @ 10:50am 
◆Comment:Grant EXP
◆Control Variables:#0035 Fish EXP to Grant += EXPtoGive
◆If:Party has Perch Lure
◆Control Variables:#0035 Fish EXP to Grant *= 1.5

:End
◆Comment:Make sure to round the number after making the multiplication (if any done)
◆Comment:(Math.ceil(number) - Round UP always, Math.floor(number) - Round DOWN always, Math.round(number) - Round up/down based on closes to 0 or 1)
◆Control Variables:#0035 Fish EXP to Grant = Math.ceil(Number($gameVariables.value(35)))
◆Comment:Grant the EXP to this current level's progress.
◆Control Variables:#0032 Current Fishing EXP += Fish EXP to Grant
Resi ♥ 1 Apr @ 10:50am 
: : LEVEL UP
◆If:Current Fishing EXP ≥ Fish EXP Needed to Next
◆Wait:35 frames
◆Play SE:Saint5 (90, 100, 0)
◆Play SE:Flash1 (90, 100, 0)
◆Show Balloon Icon:Player, Heart
◆Control Variables:#0013 ✰ Fishing Level += 1
◆Comment:Add total
◆Control Variables:#0033 Total Fishing EXP += Current Fishing EXP
◆Comment:Get remainder
◆Control Variables:#0036 Fish Remainder EXP = $gameVariables.value(32) - $gameVariables.value(34)
◆Comment:Reset Current EXP
◆Control Variables:#0032 Current Fishing EXP = 0
◆Comment:Add remainder
◆Control Variables:#0032 Current Fishing EXP += Fish Remainder EXP
◆Comment:Get new needed EXP
◆Control Variables:#0031 Fishing Level Formula = (($gameVariables.value(13) - 1) + $gameVariables.value(13)) * 30
◆Control Variables:#0034 Fish EXP Needed to Next = Fishing Level Formula
◆Comment:Show Level up message


:End
Resi ♥ 1 Apr @ 10:50am 
REFERENCE: Variables
\v[13] = ✰ Fishing Level
\v[31] = Fishing Level Formula
\v[32] = Current Fishing EXP
\v[33] = Total Fishing EXP
\v[34] = Fish EXP Needed until Next Level
\v[35] = Fish EXP to Grant
\v[36] = Fish Remainder EXP (This is for level ups)

UNREFERENCED:
\v[???] = EXPtoGive
(This is because in my system, this is done with a cute JavaScript trick)
MY TRICK:
// After determining the item, you save its ID to a variable, then you can get information from it !!!!
var item = $dataItems[$gameVariables.value(55)];
var exp = item.meta.FishEXP.split(",");
// FishEXP is a unique <notetag> on the item in the database
// It goes like <FishEXP:33,55>
// Min number and max number.
// A random number between min and max is chosen as how much EXP is earned.
$gameVariables.setValue(35, Math.random() * (Number(exp[1]) - Number(exp[0])) + Number(exp[0]));
// So now the amount of EXP the player will earn is set here.