RPGMakerMV [ES6] Strings

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
527
Reaction score
744
First Language
English
Primarily Uses
RMMV
You can find the post here.
Introduction

Strings are a part of almost any object-oriented programming language. Today, we're going to discuss some of the basics of strings, and what new features are available to us with strings when using ES6.

Strings

Strings are a sequence of characters and they are objects. These characters could be a sentence, a code, and many other things. JavaScript has its own way of creating strings. .In JavaScript, you can construct a string in three different ways; you can use double quotes ("Hello World"), single quotes ('Hello World'), or with ES6, backticks (Hello World). One other thing to note is that you can combine strings with variables


In JavaScript, you can construct a string in three different ways; you can use double quotes ("Hello World"), single quotes ('Hello World') which are considered string literals, or with ES6, backticks (Hello World) , which are template literals. Strings also some interesting properties.


One property of strings is that you can combine them with variables ("Hello World" + variable1). Not only that, you can also perform operations on strings because strings are objects. In this case, we'll be going over mostly the new operations introduced in ES6 that you can perform on strings.

Repeat

The repeat() method allows you to repeat the output of the string. For example, if you had a string: "Hello World", the repeat method would give you the output: "Hello WorldHello World". This can be useful if you wanted to create a text repeater.

Includes

The includes() method searches a string for another string and returns true if it's included in the string. This method is useful for searching for a particular word in a string, without using a regular expression to check for it.

IndexOf

The indexOf() method returns the index within the string of the first occurrence of another string, otherwise returns -1 if the value is not found. This isn't a new feature of the language but can be useful for extracting a substring from another string or counting the occurrence of a substring within a string.

StartsWith

The startsWith() method returns true or false if the string starts with the specified sequence of characters. You can also change where in the string to check if the string starts with the specified sequence of characters. This can be useful if you want to check for a case sensitive sequence of characters at the start of your string.

Code Example


Conclusion

Strings are something you'll use often when dealing with game text or displaying information in the game for players to see; they will be a staple part of a lot of JavaScript code that you write. And, the string class has a lot of valuable methods that can make your life easier when dealing with strings. Template literals are extremely powerful for formatting strings, plus combining them with variables, and string literals are important for just showing basic text.


As always, I hope that this post helps you understand strings a bit better, and improves your JavaScript code overall. If you have any suggestions or comments, please comment down below.
 
Last edited:

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,425
Reaction score
679
First Language
Portuguese - Br
Primarily Uses
RMMV
I love to use string literals on my codes.
Like:
let actorName = $gameActors.actor(1).name
let string = `The name of this dog is ${actorName}`

Nice job you are doing! Hope this can make more people interested in plugin developer for MZ. It's nice that the users have a lot of options ^^
 

Solar_Flare

Veteran
Veteran
Joined
Jun 6, 2020
Messages
503
Reaction score
220
First Language
English
Primarily Uses
RMMV
Did you know that this works too?

JavaScript:
let string = `The name of this dog is ${$gameActors.actor(1).name}`
Basically, the contents of a ${...} placeholder is an eval, so you can put arbitrary code in it. I actually don't recommend doing that, mind you... in fact, for actual strings shown onscreen to the user, I strongly recommend avoiding template literals because they would be impossible to translate (or at least, I haven't been able to find a way to translate them). Mind you, if that's not a concern for you, then I suppose you can do whatever is most convenient.
 

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,425
Reaction score
679
First Language
Portuguese - Br
Primarily Uses
RMMV
Did you know that this works too?

JavaScript:
let string = `The name of this dog is ${$gameActors.actor(1).name}`
Basically, the contents of a ${...} placeholder is an eval, so you can put arbitrary code in it. I actually don't recommend doing that, mind you... in fact, for actual strings shown onscreen to the user, I strongly recommend avoiding template literals because they would be impossible to translate (or at least, I haven't been able to find a way to translate them). Mind you, if that's not a concern for you, then I suppose you can do whatever is most convenient.
Yes I know :)
[EDIT] Yes I know that I can use it like you show, guess I answer you to shortly, sorry about that xD
But
Basically, the contents of a ${...} placeholder is an eval
This is not true, I mean for the "eyes" it seems so, but under the hood, is not even close.
Because eval() happens in run-time and the template runs in compile time.
When it was released(es6) concatenate strings were faster. But now there is some browsers that manage to do it faster than concatenating strings:
let color = 'pink'
'This flower is ' + flower // This flower is pink.
`This flower is ${color}` //

So I think, in today's browsers, there is no big difference between them(in terms of performance).
But I have to admit that I like to use them because the color that Visual Studio Code shows it hahaha xD
1594876239706.png
Isn't nice? xD

Also quite more easy(at least for me) to read and concatenate strings, also for multiline strings too.

for actual strings shown onscreen to the user, I strongly recommend avoiding template literals because they would be impossible to translate (or at least, I haven't been able to find a way to translate them).
Don't understand this part, what do you with translate? Like, translate to a language to another?

Here are some things that I found while I was trying to confirm what I said:
But I didn't find anything saying that it can be compared to eval(in terms of performance).
I found that, but it is from 2016 o_O
 
Last edited:

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,382
Reaction score
1,567
First Language
English
Primarily Uses
RMMV
There's also the format function that was added for MV in the core js:

Code:
let variable = "exciting";
let string = "This string is %1!".format(variable);
 

Solar_Flare

Veteran
Veteran
Joined
Jun 6, 2020
Messages
503
Reaction score
220
First Language
English
Primarily Uses
RMMV
This is not true, I mean for the "eyes" it seems so, but under the hood, is not even close.
Because eval() happens in run-time and the template runs in compile time.
While all this may be true, it still functions basically the same as eval from what I can tell. It might be an optimized eval somehow, but still.

Don't understand this part, what do you with translate? Like, translate to a language to another?
I'm talking about translating your game to a different language, for example French. I can't see any way to write code that will look up a template string in whatever the active language is, because it needs to have a reference to the current execution context in order to substitute code.
 
Last edited:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

How do you cure sleep problem? It has been 7 months and it's 3 AM now..
Darn you, Seals! Why do you gotta be so cute when on land?:kaoluv:
So dragonbones getting laggy xD
I even removed the fingers armatures.

Have to remake this guy after crashing ( corrupted file).
:rtear:

Although limited to only this shop for room decorations, I like this aspect for previewing items.

Forum statistics

Threads
100,837
Messages
980,130
Members
132,487
Latest member
AnthonyDewitt
Top