Read event note from script JS

Hi all!
I need to read the event note from script js.

For event note I mean the field right by the field 'Name' in the event editor.

I tried with:

var txt = $gameMap.events()[$gameMap.eventIdXy(xPos, yPos)].event().note;

where xPos and yPos are the event x/y position coordinates.

What I'm doing wrong?

thanks a lot!
Moved to: Learning JavaScript Learning JS is where you can get advice for how to write/fix lines of JS code. (MV Deployment is for issues with exporting to mobile devices, etc.)
events() returns a zero-based array, so you need to subtract 1 from the event ID:
Code:
var txt = $gameMap.events()[$gameMap.eventIdXy(xPos, yPos) - 1].event().note;
Alternatively you could use something like this:
Code:
var txt = $gameMap.eventsXy(xPos, yPos)[0].event().note;
...though it'd be safer to add a check for "no event" in there, e.g.
Code:
var evt = $gameMap.eventsXy(xPos, yPos)[0];
var txt = evt ? evt.event().note : "";
Moved to: Learning JavaScript Learning JS is where you can get advice for how to write/fix lines of JS code. (MV Deployment is for issues with exporting to mobile devices, etc.)
I'm sorry! I was wrong! xD


events() returns a zero-based array, so you need to subtract 1 from the event ID:
Code:
var txt = $gameMap.events()[$gameMap.eventIdXy(xPos, yPos) - 1].event().note;
Alternatively you could use something like this:
Code:
var txt = $gameMap.eventsXy(xPos, yPos)[0].event().note;
...though it'd be safer to add a check for "no event" in there, e.g.
Code:
var evt = $gameMap.eventsXy(xPos, yPos)[0];
var txt = evt ? evt.event().note : "";

Wow thank you very much! I'll try that soon!
So i can create a foreach loop to list all the 'event's properties' right?

Thanks a lot
So i can create a foreach loop to list all the 'event's properties' right?
Not sure what you mean by this...I guess you want to iterate over all the event's meta properties, i.e. notetags? 😅

Note that JS's "for each...in" has been deprecated:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for_each...in

If you meant the array method forEach, note that the note property is just a string. Also, the meta property is a non-array object; however, I think you could get its keys and forEach over those, if you like, e.g.
Code:
Object.keys(evt.meta).forEach(...etc
Not sure what you mean by this..
I've not a list of all event properties (like .note property) so I would to loop to have it and be able in future to call them like notetag this time 😀

But I don't want to go OF 😀

Thanks a lot!!!
Not sure what you mean by this...I guess you want to iterate over all the event's meta properties, i.e. notetags? 😅

Note that JS's "for each...in" has been deprecated:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for_each...in

If you meant the array method forEach, note that the note property is just a string. Also, the meta property is a non-array object; however, I think you could get its keys and forEach over those, if you like, e.g.
Code:
Object.keys(evt.meta).forEach(...etc

I tried
Code:
Object.keys(evt.meta).forEach(e => console.log(e+'\n');

But it does not works 🙁
I tried
Code:
Object.keys(evt.meta).forEach(e => console.log(e+'\n');

But it does not works 🙁
You're missing a closing bracket at the end, and the '\n' is redundant since the console starts a new line by default after printing. Try this:
Code:
Object.keys(evt.meta).forEach(e => console.log(e));
Also make sure the variable evt is defined beforehand. 😅

If that doesn't produce the expected result (I'm still unsure what you're trying to do), here's an example. Let's say event 1 has this in its notebox:
Code:
<tag1: 5>hello this is a test<cheese: tasty>
Then:
  • This code:
    Code:
    $gameMap.event(1).event().note
    ...will return this string:
    Code:
    <tag1: 5>hello this is a test<cheese: tasty>

  • This code:
    Code:
    $gameMap.event(1).event().meta
    ...will return this object:
    Code:
    { tag1: " 5", cheese: " tasty" }
    The object's keys are "tag1" and "cheese", and their values are " 5" and " tasty" respectively.

  • This code:
    Code:
    var meta = $gameMap.event(1).event().meta;
    Object.keys(meta).forEach(function(m) { console.log(m); });
    ...will print this to the console:
    Code:
    tag1
    cheese

  • This code:
    Code:
    var meta = $gameMap.event(1).event().meta;
    Object.keys(meta).forEach(function(m) { console.log(m + ':' + meta[m]); });
    ...will print this to the console:
    Code:
    tag1: 5
    cheese: tasty
You can verify all of this by entering it into the console when on the map during play. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/read-event-note-from-script-js.114631/