Help merging two scripts

Status
Not open for further replies.

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
87
Reaction score
6
First Language
Italian
Primarily Uses
RMMV
Hi,
I'm using these two scripts in conditional branches (with a free input text plugin that store the string inserted by the player in a variable):

1) let text = $gameVariables.value(x).toLowerCase(); text.indexOf("word 1") < text.indexOf("word 2")

2) (function(){ words = ["word"]; for(i=0;i<words.length;i++){if(!RegExp("\\b"+words+"\\b").test($gameVariables.value(x).toLowerCase())){break}};if(words.length===i)return!0})()

1) allows me to check if a given "word 1" occurs in the string before a given "word2"

2) allows me to check if in the string occurs a very specific word: i.e. "head" BUT NOT "headache".

Now...I need to merge these two: I need to check if a very specific "word1" occurs before a given "word2".
Someone more skilled than me in javascript can help? :kaocry:
 

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,419
Reaction score
672
First Language
Portuguese - Br
Primarily Uses
RMMV
Hi there!
Let me see if I understand you correctly before I try to help.

You want to check in a conditional branch the value of a variable that is a string.
And if that string is a word, like:
'head'

But, if this variable value is 'headache', do you want to check first if it is 'head'?
Did I get it?

Maybe you can try to explain what you are trying to do in your game than your javascript code?
Like, is this a password thing?
(Also, try to put your javascript code inside the code tag, it helps for reading ^^
 

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
87
Reaction score
6
First Language
Italian
Primarily Uses
RMMV
Hi there!
Let me see if I understand you correctly before I try to help.

You want to check in a conditional branch the value of a variable that is a string.
And if that string is a word, like:
'head'

But, if this variable value is 'headache', do you want to check first if it is 'head'?
Did I get it?

Maybe you can try to explain what you are trying to do in your game than your javascript code?
Like, is this a password thing?
(Also, try to put your javascript code inside the code tag, it helps for reading ^^
The variable can contain a whole phrase.
I.e. Var(x)="Today my head hurts, I have an headache."

Now, with script 1) I can check if "hurts" come first than "have". Or "Today" comes first than "my" etc etc.

Script 2) can check if the phrase contain "head" (but it won't return true for "headache").

The problem is that if I use the 1) script to check, for example, if "head" comes before another given word, it actually check even for compunds words like "headache" and I don't want this happen.
I want it checks ONLY for "head" like script 2) does.
It must check if only "head" exist in the phrase (ignoring "headache") and comes before "word2".
From here the need to "hybridate" the 2 scripts...
I hope this is more clear now...
 
Last edited:

Zevia

Veteran
Veteran
Joined
Aug 4, 2012
Messages
637
Reaction score
346
First Language
English
Primarily Uses
RMMV
Try this, replacing x and y with the variable numbers containing the words you want to check and 'Some phrase' with whatever phrase you want to check the words for (or another $gameVariables.value(z), with z being the phrase you want to check):

Code:
const word1=$gameVariables.value(x);const word2=$gameVariables.value(y);new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('Some phrase');
As an example:
Code:
// returns true because "head" occurs before "splitting", ignoring case sensitivity
const word1='HeAd';const word2='SpLiTTiNg';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('ToDaY my HeaD HURTS, I haVe A sPlITTinG HeaDAcHe'); 

// returns false because "splitting" does not occur before "head", ignoring case sensitivity
const word1='SpLiTTiNg';const word2='HeAd';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('ToDaY my HeaD HURTS, I haVe A sPlITTinG HeaDAcHe');
 

Wavelength

Edge of Eternity
Global Mod
Joined
Jul 22, 2014
Messages
5,404
Reaction score
4,801
First Language
English
Primarily Uses
RMVXA

Moved to Learning JavaScript


This board generally has the best answers for questions like this.
 

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
87
Reaction score
6
First Language
Italian
Primarily Uses
RMMV
Try this, replacing x and y with the variable numbers containing the words you want to check and 'Some phrase' with whatever phrase you want to check the words for (or another $gameVariables.value(z), with z being the phrase you want to check):

Code:
const word1=$gameVariables.value(x);const word2=$gameVariables.value(y);new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('Some phrase');
As an example:
Code:
// returns true because "head" occurs before "splitting", ignoring case sensitivity
const word1='HeAd';const word2='SpLiTTiNg';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('ToDaY my HeaD HURTS, I haVe A sPlITTinG HeaDAcHe');

// returns false because "splitting" does not occur before "head", ignoring case sensitivity
const word1='SpLiTTiNg';const word2='HeAd';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('ToDaY my HeaD HURTS, I haVe A sPlITTinG HeaDAcHe');
YAY! This is what I need, just a thing; it seems not work when I try to check two differents words as "word1".
Like this:

Code:
const word1=("x" || "y");const word2=("z");new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('Some phrase');
There is a way to do that?
For the rest it works perfectly
Thank you^^
 
Last edited:

Zevia

Veteran
Veteran
Joined
Aug 4, 2012
Messages
637
Reaction score
346
First Language
English
Primarily Uses
RMMV
Short answer: Try assigning something like const word1='x|y' instead, using the | character to separate each word you want to check, but leave it all as a single String, e.g. 'x|y|z|a|b'.

Since we're in the Learning JS board now, a longer answer:

In programming, the || (or) and && (and) operators are supposed to be used with Boolean (true/false) statements only. In a lot of languages, ("x" || "y") would actually throw errors instead of executing.

JavaScript has a feature called "type coercion", however, where if you use unexpected values for an operation, the language attempts to coerce (convert) them into the appropriate values. So the way JS handles || statements is to say, "Use the first value that's true or close enough to true". The question, then, is "Is the String 'x' true or false?" As a general rule, anything that isn't an empty String (''), undefined, null, or the number 0 is true as far as JS is concerned.

So JS will look at that statement and say, "OK, of the values 'x' and 'y', use the first one that's true. Well, 'x' is close enough to true, so we'll use that," and word1 will always equal "x".

There is a very slight change we can make, however, for your purposes:
Code:
// returns true because either a or b or c occur before d
const word1='a|b|c';const word2='d';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('c d a b e');

// returns false because neither a nor b nor c occur before d
const word1='a|b|c';const word2='d';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('d e a b c ');
In a regular expression (the RegExp object we're making), you can use the | character to act as an "or" in a manner more akin to how you might expect ("x" || "y") to work. So we're checking if a or b or c occur before the word d.

If you want to use $gameVariables.value for all your possible words, there are a couple ways to do so:
Code:
// concatenate all of them in using template literals
const word1=`${$gameVariables.value(x)}|${$gameVariables.value(y)}|${$gameVariables.value(z)}`;

// concatenate all of them using a plus sign
const word1=$gameVariables.value(x) + '|' + $gameVariables.value(y) + '|' + $gameVariables.value(z);

// Use the Array join method to do it
const word1=[$gameVariables.value(x), $gameVariables.value(y), $gameVariables.value(z)].join('|');
Since that can get a bit lengthy, if you run out of room, you could also assign $gameVariables.value to a short variable as an alias:
Code:
// with template literals
const _=$gameVariables.value;const word1=`${_(x)}|${_(y)}|${_(z)}`;

// with plus signs
const _=$gameVariables.value;const word1=_(x) + '|' + _(y) + '|' + _(z);

// With Array join
const _=$gameVariables.value;const word1=[_(x), _(y), _(z)].join('|');
 

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
87
Reaction score
6
First Language
Italian
Primarily Uses
RMMV
Short answer: Try assigning something like const word1='x|y' instead, using the | character to separate each word you want to check, but leave it all as a single String, e.g. 'x|y|z|a|b'.

Since we're in the Learning JS board now, a longer answer:

In programming, the || (or) and && (and) operators are supposed to be used with Boolean (true/false) statements only. In a lot of languages, ("x" || "y") would actually throw errors instead of executing.

JavaScript has a feature called "type coercion", however, where if you use unexpected values for an operation, the language attempts to coerce (convert) them into the appropriate values. So the way JS handles || statements is to say, "Use the first value that's true or close enough to true". The question, then, is "Is the String 'x' true or false?" As a general rule, anything that isn't an empty String (''), undefined, null, or the number 0 is true as far as JS is concerned.

So JS will look at that statement and say, "OK, of the values 'x' and 'y', use the first one that's true. Well, 'x' is close enough to true, so we'll use that," and word1 will always equal "x".

There is a very slight change we can make, however, for your purposes:
Code:
// returns true because either a or b or c occur before d
const word1='a|b|c';const word2='d';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('c d a b e');

// returns false because neither a nor b nor c occur before d
const word1='a|b|c';const word2='d';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('d e a b c ');
In a regular expression (the RegExp object we're making), you can use the | character to act as an "or" in a manner more akin to how you might expect ("x" || "y") to work. So we're checking if a or b or c occur before the word d.

If you want to use $gameVariables.value for all your possible words, there are a couple ways to do so:
Code:
// concatenate all of them in using template literals
const word1=`${$gameVariables.value(x)}|${$gameVariables.value(y)}|${$gameVariables.value(z)}`;

// concatenate all of them using a plus sign
const word1=$gameVariables.value(x) + '|' + $gameVariables.value(y) + '|' + $gameVariables.value(z);

// Use the Array join method to do it
const word1=[$gameVariables.value(x), $gameVariables.value(y), $gameVariables.value(z)].join('|');
Since that can get a bit lengthy, if you run out of room, you could also assign $gameVariables.value to a short variable as an alias:
Code:
// with template literals
const _=$gameVariables.value;const word1=`${_(x)}|${_(y)}|${_(z)}`;

// with plus signs
const _=$gameVariables.value;const word1=_(x) + '|' + _(y) + '|' + _(z);

// With Array join
const _=$gameVariables.value;const word1=[_(x), _(y), _(z)].join('|');
Veeery helpful, thank you a lot!
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
7,006
Reaction score
4,744
First Language
Dutch
Primarily Uses
RMXP

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

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

Latest Threads

Latest Posts

Latest Profile Posts

*sigh* I'm so sick of this heat! :kaosigh::kaoeh:

Between Rona and MZ, might as well buckle down and get serious in dev
I did it. I finish the test after 2 days 6 different subjects. Now I'm freeeeeeee
Got a nice little platform coming along backwards compatible with MV.

Forum statistics

Threads
100,659
Messages
978,206
Members
132,280
Latest member
ju9SW
Top