MZ How would I change the self-switch of a spawned event, from another spawned event?

I am developing a store system with spawned events. The items themselves "spawn" on the counters, and then the NPCs "spawn" at the door, walk to the counter, purchase the item, and then walk away.

I am using Pocket Events to spawn the items on the counters. I am using Eli's Self Variables to track what counter they are spawning on. Visustella Events and Movement Core. (I know VS has self variables but they weren't working for me). And KageDesu's ABS plugin to spawn the NPCs.

selfvariablehelp01.PNG

Using "Crystal" as an example, here I'm setting the item's "self variable" (21) to variable 14, which is what counter it's being placed on (1, 2, 3, etc.). This works fine. ✓

Then I spawn an NPC at the shop.

selfvariablehelp02.PNG

They roll a random number, check if that counter has an item or not, then they walk to that counter. I have to use "Move to: x/y" instead of "Move to: event" because the event (the item) is spawned and so it doesn't have an ID. So, not super ideal, but this works fine too ✓

The problem I'm having is that I want the item to disappear after the NPC has walked to it.

selfvariablehelp03.PNG

Right now, the NPC turns on "customerArrived" when they stop walking to the counter. They wait a sec and then they leave the room.

selfvariablehelp04.PNG

Then the "Crystal" item checks if this is the counter the NPC went to, and then erases itself once "customerArrived" is on. Technically, this whole thing works ✓

But! It only works when there's only 1 customer.

I am trying to spawn more than 1 customer at a time. The problem is the second spawned NPC overwrites variable 24 (which counter they're going to) from the first NPC, disabling the page condition from the 1st chosen item. So then the 2nd chosen item "erases" when the 1st chosen item should, and then nothing happens to the 1st chosen item.

I hope that makes sense, lol! Here is a gif of it in practice (all placeholder graphics, pls don't judge lol):
Spoiler: Gif
selfvariablehelp04.gif


So what I'd like to do is use self variables/self switches to control the individual spawned item events, from the individually spawned npc events. It's doin me a heckin confuse because they technically don't have event IDs.

Any ideas?

Edit: So I found that the NPC spawn does have this code I can use:
Code:
uAPI.getLastSpawnedEnemy(); - return last spawned Enemy (Game_Event object) or null
But I'm not sure how I would use it...
event (the item) is spawned and so it doesn't have an ID.
That is where you are wrong, and where you have to proceed.

ALL events have IDs - the engine cannot function with events that have no IDs.
It might be that you don't know that ID because the spawn function is a bad one that doesn't provide it, but that doesn't change that it exists - and you need that ID to access that event's self-switches.

if you have only one event per position, you can use "get location info" to get the ID of the event on that location into a variable and use it from there. Otherwise you have to analyse your spawn event plugin to identify how to get the ID reported by the spawn function.

The problem is the second spawned NPC overwrites variable 24 (which counter they're going to) from the first NPC
the solution here is to give that event self-variables as well, and store their target in that self-variable.
for this you'll need to know the event ID (but see above, should already be solved when you get here again) and you'll need to check that a single item is not sold twice in any way (might be the complex part of the solution).
Edit: So I found that the NPC spawn does have this code I can use:
Code:
uAPI.getLastSpawnedEnemy(); - return last spawned Enemy (Game_Event object) or null
But I'm not sure how I would use it...
If that returns a Game_Event object, then you can simply do:
Code:
uAPI.getLastSpawnedEnemy().eventId()

Put that into a variable or use it directly in other script calls, whatever.

The function to set self switches is
Code:
$gameSelfSwitches.setValue([mapId, eventId, 'Switch Letter'], value);
Thanks @Andar and @ATT_Turan ! I've been at it all morning and I feel soo close. "Get Location Info" worked to get the spawned enemy's Event ID, so thank you for that!

So my plan right now is to set the Spawned NPC's self variable "Item" to its chosen item ID (based off the counter they are going to). And then the Spawned Item checks to see if a Spawned NPC's "Item" matches its own ID (also in a self variable). If it does match, then the Spawned Item puts the Spawned NPC's Event ID into a self-variable. When that Spawned NPC flips to Self Switch A, then the Spawned Item should Erase.

That last step isn't happening though.

Here's the logic:
selfvariablehelp05.PNG

Spawning NPCs. Waiting 120 frames should give the Event NPC enough time to set its self variables before the next NPC spawns. "spawnedNPCEventID" is 27.

selfvariablehelp06.PNG

This is the NPC that spawns. I am setting a self-variable "28" (npcItemChose) to the item ID that is on the counter. Counter 1 Item ID is 16 and Counter 2 Item ID is 22.

selfvariablehelp07.PNG

This is a spawned item. It autoruns this page when it's placed down. It's putting it's own id into a self-variable. In this case, it's item 3.

selfvariablehelp08.PNG


[parallel] Then it's checking to see if the Spawned NPC's Item Chose is equal to my Item ID. Variable 27 is the event ID of the spawned NPC. Variable 28 is the item it grabbed earlier (from either counter 1 or 2). If it's equal, then it's setting the Event ID into a self variable 29.

selfvariablehelp09.PNG

[parallel] And then finally I'm checking if their matched NPC's self switch turns to A. In this screenshot I don't have "erase event," instead I'm changing the graphic to see if it works. This is the step that isn't working.

Edit: I changed some things around but it still won't work.
selfvariablehelp10.PNG
selfvariablehelp11.PNG

The text box "it matches" shows up while the guy is walking to the counter. And then it switches graphic. But it doesn't say "doneso" after the NPC has switched self-variable to A.

Edit 2: I got it!! I was using Eli's Self Variables wrong! I had to put:
this.setSv(29, uAPI.getLastSpawnedEnemy().eventId())
instead of $gameVariables
It is working as expected now ^___^

Original thread: https://forums.rpgmakerweb.com/threads/how-would-i-change-the-self-switch-of-a-spawned-event-from-another-spawned-event.148200/