Status
Not open for further replies.

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
152
Reaction score
17
First Language
Italian
Primarily Uses
RMMV
Hi, dear forum,
I need to check for a number in a string and then put that number into another variable.
I tried using this script:

Code:
var num = parseInt($gameVariables.value(27).match(/\d{4}/)[0]);
$gameVariables.setValue(184, (num>=1000 && num <= 9999) ? num : 0);

Variable 27 is where the string is stored, and variable 184 is the one where the number should be inserted.

It works, but it seems to check only for a 4 digit number (or \d{3} or \d{2}.........).
Now, basically, I need to upgrade this code so it can check for any kind of number (without considering its length) in the string.
Is that possible?
 

Featherbrain

Prehistoric Gamer
Veteran
Joined
Jan 12, 2020
Messages
160
Reaction score
377
First Language
English
Primarily Uses
RMMV
Try this:

JavaScript:
// Basic example:
var txt = "getnumber1234-and-5";
var num = txt.replace(/[^0-9]/g,'');
console.log(num);
// Expected output: "12345"

// RPG Maker version:
var txt = $gameVariables.value(27);
var num = txt.replace(/[^0-9]/g,'');
$gameVariables.setValue(184, num);
 

Nolonar

Veteran
Veteran
Joined
Feb 18, 2018
Messages
262
Reaction score
367
First Language
French, German
Primarily Uses
RMMZ
It works, but it seems to check only for a 4 digit number (or \d{3} or \d{2}.........).
Now, basically, I need to upgrade this code so it can check for any kind of number (without considering its length) in the string.
Is that possible?

Yes, you're looking for the pattern \d+, which matches at least 1 digit.
Other patterns include:

  • \d{1,3} matches at least 1 digit, but no more than 3 digits.
  • \d{2,} matches at least 2 digits.
  • \d{,4} matches no more than 4 digits.
  • \d{5} matches exactly 5 digits (what you are currently using).
  • \d? matches a digit if it exists (equivalent to \d{0,1}).
  • \d* matches any number of digits, including no digits (equivalent to \d{0,}).
 

Featherbrain

Prehistoric Gamer
Veteran
Joined
Jan 12, 2020
Messages
160
Reaction score
377
First Language
English
Primarily Uses
RMMV
Using that method you also need to change the second line, because it's checking whether num is a 4-digit number (in the range of 1000-9999) and setting the game variable to 0 if it isn't.

Try this, which should set the variable to the value of num if it exists, or 0 if it doesn't (e.g. if no digits were found in the original string).

$gameVariables.setValue(184, num || 0);
 

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
152
Reaction score
17
First Language
Italian
Primarily Uses
RMMV
Thank you both! The problem is solved!^^
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
43,344
Reaction score
15,158
First Language
English
Primarily Uses
RMMV

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.

Latest Threads

Latest Profile Posts

Changed my avatar, goodbye Alan Sugar, hello George Carlin (one of my favorite human beings ever)
If you still don't subscribe our Polish channel please consider it :)
1.png
Who wants to see my review of the worst star wars movie? This movie has all the excitement of being on Jury Duty of the most boring case ever about trade negotiations.
A lot to learn to make a game. Can I handle it?
I've been working on status effects since last thursday, and today I finished the core mechanic of Zombie, Teleport, Curse and Recall. Zombie and Curse are lacking a few things to make them complete atm. Teleport though is the "HAHA" spell for some of my mobs, but can "NOPE" at them with Recall.

Forum statistics

Threads
115,169
Messages
1,087,777
Members
149,711
Latest member
GHench
Top