Why does contains or match does not work with two tags?

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Can someone explain this?


For example, in my event, I did a comment:


<sound: 4>
<picture: 6>


Now, I am told that we can use .contains or .match. WhenI do this:


if (this.page().list.parameters[0].match(/<sound:\s*(\d+)?>/im)) {
var sound = this.page().list.parameters[0].match(/<sound:\s*(\d+)?>/im)[1];
this.eventSoundNumber = Number(sound);
}


It won't actually read the value and place it to my new variable eventSoundNumber. In my understanding, it should be able to since it is matching the expression. When I use contains it just do the same. Is there a way to check the <sound: x> tag while keeping <picture: x> or vice versa?
 

taarna23

Marshmallow Princess
Global Mod
Joined
Jul 20, 2012
Messages
2,402
Reaction score
4,966
First Language
English
Primarily Uses
RMMZ
Javascript does not have .contains OR .match when dealing with an array (which I assume you are using).


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Methods_2


There IS a .includes and a .findIndex


However, I was under the impression we couldn't use things introduced in ECMAScript 2016. Is this incorrect?


Edit: It's worth noting I'm still learning myself, but I'm 99% sure you want includes instead of contains. The rest, I have no clue!
 
Last edited by a moderator:

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
856
First Language
English
Primarily Uses
RMMV
I just use meta to grab data from notes ... so much easier.

Code:
// $dataMap.events[eventId].meta.propName;

$dataMap.events[1].meta.picture;
$dataMap.events[1].meta.sound;
 
Last edited by a moderator:

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I just use meta to grab data from notes ... so much easier.



// $dataMap.events[eventId].meta.propName;

$dataMap.events[1].meta.picture;
$dataMap.events[1].meta.sound;


OMG this works! thanks Exhydra! It looks easier to use!
 

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
856
First Language
English
Primarily Uses
RMMV
Also, meta is anywhere there is a note. $dataItems, $dataWeapons, and so on. 
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
556
Reaction score
794
First Language
English
Primarily Uses
RMMV
Meta works but be careful.


Also concerning  match, match doesn't return anything, only whether there is a match or not against the regular expression.


If you want to get data from using a regular expression.


You want to use the .exec method.


After that you need to take your lastIndex property,  and set it to 0 after evaluation. It will get the digit you want from sound if the expression matches.
 

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
856
First Language
English
Primarily Uses
RMMV
Actually, I just realized that meta is saved per event, not per page. So if you want to grab and apply data per page, you will need to use a method similar to the one in the first post. Here is one I just whipped up ... should work alright.


this.page().list.forEach(function(data) {
if (data.code == 108 || data.code == 408) {
var tmpMatch = data.parameters[0].match(/<(sound|picture):\s*(\d+)?>/i);
if (tmpMatch) {
switch (tmpMatch[1]) {
case 'sound':
this.eventSoundNumber = Number(tmpMatch[2]);
break;
case 'picture':
this.eventPictureNumber = Number(tmpMatch[2]);
break;
}
}
}
}, this);


That will search through the entire page to search for matches.


I would suggest using more unique identifiers, though. 'sound' and 'picture' are pretty generic, and might be used by another plugin developer on accident.
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,100
Reaction score
13,705
First Language
English
Primarily Uses
RMMV
meta is for notes.  I thought you said you were using comments in an event?


Try this:

Code:
var re = /<sound:\s*(\d+)?>/im;
var match = re.exec(this.page().list[i].parameters[0]);
if (match) {
  this.eventSoundNumber = Number(match[3]);
}
 
Last edited by a moderator:

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
856
First Language
English
Primarily Uses
RMMV
I think @Milena was looking for a way to pass the data ... either notes or comments would work fine. I find notes is easier for simple data passing, but -- for example -- if you want to change the opacity of an event during the setup phase of the page you will need to use comments.
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,100
Reaction score
13,705
First Language
English
Primarily Uses
RMMV
Ah.  I just assumed she wanted it in event comments as that's the way the first post is worded.


Events also have notes, so you can use .meta without having to make something read them.  Unfortunately though, they're all on a single line in a very small-to-read/edit box.  It shouldn't stop you from being able to use .meta on the event, but it'll be a pain to add the notes or to edit them or look through them, without copying it all into a word processor or something.
 

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
856
First Language
English
Primarily Uses
RMMV
Yeah, I agree that entering data into the note field in an event is a pain. I use the field as an 'event global', and comments for 'event local'. As for usage of notes, though ... meta is nice because all of the code is in the base. So, all someone has to do is nab whatever from the 'meta' property.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
@Shaz - thanks for the suggestion. I might use it when I am going to get comment tag values in the event per page. The thing is sometimes, I jsut feel that comment tags are far extensive so I can add many things into it and have each variable grab their values.


@Exhydra - Thank you too for the suggestion. I will bookmark this thread, it is very useful for me and I might get back on it again in the future.
 

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

Latest Threads

Latest Posts

Latest Profile Posts

I should realize that error was produced by a outdated version of MZ so that's why it pop up like that
Ami
i can't wait to drink some ice after struggling with my illness in 9 days. 9 days is really bad for me,i can't focus with my shop and even can't do something with my project
How many hours have you got in mz so far?

A bit of a "sparkle" update to the lower portion of the world map. :LZSexcite:
attack on titan final season is airing tomorrow, I'm excited and scared at the same time!

Forum statistics

Threads
105,881
Messages
1,017,227
Members
137,607
Latest member
Maddo
Top