RPGMakerMV [ES6] Strings

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
557
Reaction score
797
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,778
Reaction score
1,207
First Language
Portuguese - Br
Primarily Uses
RMMZ
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
533
Reaction score
235
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,778
Reaction score
1,207
First Language
Portuguese - Br
Primarily Uses
RMMZ
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,616
Reaction score
1,978
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
533
Reaction score
235
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:

Latest Threads

Latest Posts

Latest Profile Posts


When you thought you'd be finished with the grid, and spend ~8-9 hours on an arrow. How is this more difficult to than player movement?

Still some bugs hiding in there, but it's nearly complete.

I'm having it strictly follow the path a user draws, because there will be booby trap and other similar mechanics where you want to avoid specific cells.
I feel like banging my head against a wall every time I want to find that post that shows all the plugin header codes. Then I think "next time I find it, I'm going to bookmark it". Then I think "wait a minute ... maybe I bookmarked it last time because it took me so long to find it". Check bookmarks, and there it is :)
... a few of my main characters standing around doing nothing lol. I mostly wanted to see them together on screen.

Forum statistics

Threads
107,824
Messages
1,032,451
Members
139,975
Latest member
Lucimountfort
Top