Check a range of numbers in a string

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 there,
I'm using a free input text plugin which can store a string in a variable.
I need to check in script box of a conditional branch if in that string there is a numeric value in a range and, if there is, store it in another variable.

In a more practical way it can be used for birthdays.
The player can write a year (that will be stored by the free input plugin in the first variable as a string; "I was born in 1970").
Then we should check that variable to see if that numerical value is in the range (idk, let's say 1920-2020); if yes, store that number in another variable (that will contain just that number to be called later), otherwise do stuff.

Is it possible?
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,369
Reaction score
1,537
First Language
English
Primarily Uses
RMMV
This is pretty easy using the match function:

1594815145350.png

Note that the pattern provided here will only match a single 4-digit number and store it in variable 1 if it's 1920-2020. You may need something more robust depending on your requirements.
 

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
87
Reaction score
6
First Language
Italian
Primarily Uses
RMMV
This is pretty easy using the match function:

View attachment 151449

Note that the pattern provided here will only match a single 4-digit number and store it in variable 1 if it's 1920-2020. You may need something more robust depending on your requirements.
So...uhm...
I should do a script call containing

Code:
let testString = $gameVariables.value(x)
testString
testString.match(/\d{4}/)
let matches = testString.match(/\d{4}/)
matches
matches[0]
year = parseInt(matches[0])
and then put in the script box of the conditional branch the
Code:
(year >= 1920 && year <=2020)
thing?
Is that right?
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,369
Reaction score
1,537
First Language
English
Primarily Uses
RMMV
You don't need all the code I pasted, that was just for demonstration purposes to show the steps I took to get there.
 

Kuro DCupu

Trust me, I'm a veteran RMer
Veteran
Joined
Jul 6, 2014
Messages
446
Reaction score
1,402
First Language
Indonesia
Primarily Uses
RMMV
Based on demonstration above, I tested it and it works:
1594821708300.png
1594821846341.png

Here:
Code:
var year = parseInt($gameVariables.value(1).match(/\d{4}/)[0]);
$gameVariables.setValue(2, (year>=1920 && year <= 2020) ? year : 0);
It retrieves text from variable 1, extracts the first match of a four-digit number, and store it in variable 2.
Note that if it's not within range, it will return 0.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,369
Reaction score
1,537
First Language
English
Primarily Uses
RMMV
Glad you got it sorted!
 

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
87
Reaction score
6
First Language
Italian
Primarily Uses
RMMV
Based on demonstration above, I tested it and it works:
View attachment 151450
View attachment 151451

Here:
Code:
var year = parseInt($gameVariables.value(1).match(/\d{4}/)[0]);
$gameVariables.setValue(2, (year>=1920 && year <= 2020) ? year : 0);
It retrieves text from variable 1, extracts the first match of a four-digit number, and store it in variable 2.
Note that if it's not within range, it will return 0.
Thanks you both^^
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
7,001
Reaction score
4,743
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

ESAMarathon on Twitch, now streaming "Eat Girl". Yep, that's the title of a game... Apparently it's a Pacman knockoff.... Which is of course the only logical conclusion one would get from a name like "Eat Girl". :kaopride: I can't believe anybody would think anything else! :kaoback:
Super stoked i just finished my first town in my project, by finished i mean i can always add more decorative aesthetics and the NPCs don't talk yet but the mapping is complete and all the important chess pieces are present!
My brain: Hey, I have an idea how to make the transition to the main story quest in The Wastes more natural!
Me: Good!
My brain: You need to remake the hotel you start out in, it's not realistic enough.
Me: Ok... This was unexpected, but I can do it.
My brain: Now make each hotel floor 5 times as large to match the main part. Oh, you also need to make a bunch of new npcs to fill in the space on these maps.
Me: Crap.
Should be able to release Haxe MV/MZ next weekend.
It look that somehow MZ tracks are messed up (for example battle4 is obviously a theme, castle2 is a ship, ship1 is a scene and so on..). Maybe they just named them after with some ambiguity.

Forum statistics

Threads
100,618
Messages
977,837
Members
132,228
Latest member
zhengdddddd
Top