MZ How to get event ID based on name via a variable

Hi! I'm trying to write a script that finds an event that matches a text variable and sets its I.D as its own variable. This is the code I'm trying to use. It keeps saying 'cannot read property 'id' of undefined.
Code:
const char = $dataMap.events.find(e => e && e.name == $gameVariables.value(10)).id;
if (char) $gameVariables.setValue(9, char.id);
you get the error because variables don't have an ID
you may try this:

JavaScript:
const char = $dataMap.events.find(e =>  e?.name == $gameVariables.value(10));
if (char) $gameVariables.setValue(9, char.id)

I don't think you need the ID there, it depends where it is called and if you need
the ID, than find the event ID in a different value to track down.

there was a script made for MV to grab names or note meta and use that.
on first occurrence or all of them and return the first or closest.

so I don't know how you use this event or what it does.
you get the error because variables don't have an ID
you may try this:

JavaScript:
const char = $dataMap.events.find(e =>  e?.name == $gameVariables.value(10));
if (char) $gameVariables.setValue(9, char.id)

I don't think you need the ID there, it depends where it is called and if you need
the ID, than find the event ID in a different value to track down.

there was a script made for MV to grab names or note meta and use that.
on first occurrence or all of them and return the first or closest.

so I don't know how you use this event or what it does.
okay it looks like that worked. thank you.
Hi! I'm trying to create a system that checks to see if a stored variable (variable 10) matches an event name - and for it to only execute an event if there is no match (in this case, using a plugin to spawn a character). Nothing I do seems to be working and I hate to pollute the forums but please any help is appreciated!


Code:
const char = $dataMap.events.find(e =>  e?.name == $gameVariables.value(10));
if (char) break;
} else {
Galv.SPAWN.overlap = 'chars';
Galv.SPAWN.event(1,$gamePlayer.x,$gamePlayer.y, true);
}
};
okay it looks like that worked. thank you.
nevermind. now it says cannot read property 'name' of null

Since your topics seem to be about the same subject, I've merged your threads.



nevermind. now it says cannot read property 'name' of null

please press f12 and show the error trace in the console tab via a screenshot if you encounter a crash. the snippet ShadowDragon provided wouldnt be able to produce that error as the find loop would skip over the null entry at the start of the array. it sounds like your error may be coming from elsewhere

Original thread: https://forums.rpgmakerweb.com/threads/how-to-get-event-id-based-on-name-via-a-variable.180694/