MV Iterate through all Yanfly Self Variables on map

I am using Yanfly's self-Variables to make plants grow. I am also using Galv's Event spawner to allow player to choose which crop to plant. So, I don't "know" the event id. I assume I need to iterate through all Events on the Map (once/day) and find which ones have been watered, then grow them.

All I'm trying to do is iterate through the game events and I have searched for an hour and all I'm getting is AI generated "code" that doesn't work and no actual human-written code...
This is what I have so far...

//below code is placed in parallel event that runs once/day
for (var eventId = 1; eventId < $gameMap.events().length; eventId++) {
var e = $gameMap.events(eventId);
//Yanfly code below DOES work. It is only the above iteration that I can't figure out.
if (this.getSelfVariableValue(4, e, 23) === 1) this.setSelfVariableValue(4, e, 23, 2); //grow1
else if (this.getSelfVariableValue(4, e, 23) === 3) this.setSelfVariableValue(4, e, 23, 4); //grow2
//...and so on
}

Note: I haven't actually solved the problem, but I was misunderstanding it.
$gameMap.events().length gets the amount of events
but this.getSelfVariableValue(4, eventNum, 23) gets the initialized idNumber of the Event
Unfortunately, the idNumber could be higher than the total amount of events on screen (after you delete some!) so the newer crops I create aren't being iterated over. So, waht I need to figure out is how to find the number of the Event in $gameMap.events() and what idNumber it has, not what postioin in the array it has...

Since I already "closed" this question, no one might see it, so I will try and figure it out and post the answer here if I ever do...
Uh.. Now, I've spent 4 hours working on this and the crops won't even grow anymore. I don't know how to get help on this question. Should I resubmit the question again?

In summary: This is my code now. That doesnt even work anymore...

for (let eventId = 1; eventId <= $gameMap.events().length; eventId++) {
let id = $gameMap.event(this._eventId); //actual id# of events stored on map?
// let id = $gameMap.events(eventId).eventId(); //didnt work either
let num = this.getSelfVariableValue(4, id, 23); //map=4, checkEveryEvent, SelfVar=23

if (num==70 || num==72 || num==74 || num==76 || num==78 || num==80 || num==82 || num==84 || num==86 || num==88 || num==90 || num==92)
this.setSelfVariableValue(4, eventId, 23, num+1); //growing
else if (num > 92 && num < 99) this.setSelfVariableValue(4, eventId, 23, num+1); //93,94,95: harvest crop, 96,97,98: harvest seed
else if (num > 98) this.setSelfVariableValue(4, eventId, 23, 0); //ReSeed crop
}

Any ideas or should I resubmit the entire question?
Since I already "closed" this question, no one might see it, so I will try and figure it out and post the answer here if I ever do...
just as a heads up, only moderators can close threads. when you report threads, its put in a queue and one closes it for you. I saw your report earlier but was going to leave the thread open for a while in case you needed it. in the event that one of your threads are closed and its not as solved as you thought it was, you can report it to be reopened.

Unfortunately, the idNumber could be higher than the total amount of events on screen (after you delete some!) so the newer crops I create aren't being iterated over. So, waht I need to figure out is how to find the number of the Event in $gameMap.events() and what idNumber it has, not what postioin in the array it has...
i had peeked into the plugin earlier before your report and thought you solved your issue. It looked like spawned events were immediately added to the map's _events list and their IDs were set to the length of that list at the time they were added. Then, the way the plugin unspawns those events is to make the index for that event null so that the list length is maintained.


let id = $gameMap.event(this._eventId); //actual id# of events stored on map?
// let id = $gameMap.events(eventId).eventId(); //didnt work either
when you say these didnt work, did you find out if its doing anything? did your code give an error of any kind? if you made a spawned event that when triggered just spits out its ID in the console the same way you did above, does it log an expected value??
I gave up... deleted all my code... started from scratch and solved the problem:

for (let mapEvent = 1; mapEvent <= $gameMap.events().length; mapEvent++) {
let id = $gameMap.event(mapEvent)._eventId;
let grow = this.getSelfVariableValue(4, mapEvent, 23); //map=4, checkEveryEvent, SelfVar=23
if (grow > 0) {
console.log("ev:"+mapEvent+", id:"+id+", grow:",grow);
if (grow==70 || grow==72 || grow==74 || grow==76 || grow==78 || grow==80 || grow==82 || grow==84 || grow==86 || grow==88 || grow==90 || grow==92)
this.setSelfVariableValue(4, mapEvent, 23, grow+1); //growing
else if (grow > 92 && grow < 99) this.setSelfVariableValue(4, mapEvent, 23, grow+1); //93,94,95: harvest crop, 96,97,98: harvest seed
else if (grow > 98) this.setSelfVariableValue(4, mapEvent, 23, 0); //ReSeed crop
}
}

the code I needed was this: let id = $gameMap.event(mapEvent)._eventId;

I found it by trying like 50 variations and reading the errors in the Debugger until one worked...
I still have to solve the "null events on deletion" issue, but I will work on that myself for now.

Thanks for offering to help. This question can remain closed. I am slowly learning the forum posing rules too!
JavaScript:
if ( ( grow > 69 && grow < 93 && grow % 2 === 0 ) || ( grow > 92 && grow < 99 ) ) {
    this.setSelfVariableValue( 4, mapEvent, 23, grow + 1 );
} else if ( grow > 98 ) {
    this.setSelfVariableValue( 4, mapEvent, 23, 0 );
}

I was typing something but it seems like you figured it out. Just edited my post above to give you something more concise.

Side note, if you generate events with Galv's plugin, you should put <plant> or something in its note tag at the top of the event so when you generate it, you can just filter anything that has that tag:
JavaScript:
const events = $gameMap.events().filter( event => event.event().meta.plant );
const eventIds = events.map( event => event.eventId() );
This will go through all the events and give you only the events with the tag <plant> in its notes. The .map() method will give you an array of event IDs of those corresponding events.

Edit:
So, let's say you did spawn events that have the note tag <plant> on them already. You can then simplify your code to this (edit of edit - you don't need the map, just put it in the for loop to reduce the number of loops we are doing):
JavaScript:
const events = $gameMap.events().filter( event => event.event().meta.plant );
for ( const event of events ) {
    const id = event.eventId();
    const grow = this.getSelfVariableValue( 4, id, 23 );
    if ( ( grow > 69 && grow < 93 && grow % 2 === 0 ) || ( grow > 92 && grow < 99 ) ) {
        this.setSelfVariableValue( 4, id, 23, grow + 1 );
    } else if ( grow > 98 ) {
        this.setSelfVariableValue( 4, id, 23, 0 );
    }
}
Not sure if I'm supposed to keep replying on this post, but the advice from bass2yang is quite useful, so thanks! I will try that out.

The main issue I have now with Galv's plugin is when I delete an Event, it does count as null, and the next event I create still references that Event? which has null values and throws an error when iterating over all events... No idea yet if it is Galv's plugin or Yanfly's self-variables I am using IN the created events.

EDIT: So, I tried out the code you provided and not ONLY did it work, but the null-value problem has dissappeared TOO... Wow! I hope you get paid well for your skill. You solved BOTH of my problem in a few minutes, that I'd been failing to solve for 5 hours...

I've tested it for about 20 grow cycles and it all seems to work properly! Thanks again. Seriously, that is amazing to me!
Not sure if I'm supposed to keep replying on this post, but the advice from bass2yang is quite useful, so thanks! I will try that out.

The main issue I have now with Galv's plugin is when I delete an Event, it does count as null, and the next event I create still references that Event? which has null values and throws an error when iterating over all events... No idea yet if it is Galv's plugin or Yanfly's self-variables I am using IN the created events.

EDIT: So, I tried out the code you provided and not ONLY did it work, but the null-value problem has dissappeared TOO... Wow! I hope you get paid well for your skill. You solved BOTH of my problem in a few minutes, that I'd been failing to solve for 5 hours...

I've tested it for about 20 grow cycles and it all seems to work properly! Thanks again. Seriously, that is amazing to me!
You are welcome! Glad it worked out.

I've had my fair share of failures privately and publicly trying to work through something for hours and hours, only to wake up the next day to solve it in a few minutes (which was awesome), or not solve anything at all (very frustrating), haha. And that's okay, we all learn and grow.

The joys of coding.
the code I needed was this: let id = $gameMap.event(mapEvent)._eventId;
Just a note from my side. Only use underscore-variables (_eventId) when there's no other way. You usually want to use the method, in this case: eventId().

Code:
$gameMap.event(mapEvent).eventId()

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.

Original thread: https://forums.rpgmakerweb.com/threads/iterate-through-all-yanfly-self-variables-on-map.179655/