So my solution is a little over-engineered, but I'm wearing some pretty fancy pants today, so I couldn't help it. I don't even really know where to start explaining it, so I guess I'll just mention what I was shooting for:
1.) I liked that some of the previous entries had it set up so that the NPC image (on the map) changes based on which monster is randomly selected. But I didn't like that they required a unique event page for each image/monster. Partially because that's just not how I roll, and partially because it makes it more of a pain to extend/edit the system later on. Imagine if you had multiple copies of that event in your game, and then you suddenly decided that you wanted to add a new monster... you'd have to go back and add new pages to every copy of the event, and edit your random generator command (on every event) to account for the increased range.
So my highest priority goal was to make an event that gives a unique map image to each monster, without needing a new page for each monster, and one that I can add or remove monsters without having to actually edit the event.
2.) The other thing is that I noticed that all of the existing entries share one common opportunity for improvement... Basically, it's that the events are written in a way that doesn't support multiple instances to exist simultaneously. What I mean is, if you copy and paste the event, so that there are two random monsters on the map, it won't work. They're written to rely on hard-coded global variables, so whatever happens to one monster will affect the other monster as well. I didn't feel that that is a realistic approach, because I think that if I were to make a game with randomly respawning monsters, I'd probably have more than just one monster in my game.
So my second highest priority goal was to make it so that the event is fully self-contained, so that it can be freely copied and pasted throughout my game, and as many instances as I want can be running at any given time, without affecting each other.
3.) As I mentioned in a previous post, most of the current entries are handling the respawn time in a way that I feel doesn't really conform to the rules of the challenge.
So my third goal was to make the timer work in a way that the monster will respawn in exactly 5 minutes of game time, regardless of whether I stay on the map, leave the map, enter battle, enter menus, etc.
In order to achieve the first goal, I had to make my event have a way to automatically know which image to use for each monster. I decided to tackle this through the use of note tags. For any enemies that you want to be included in the random selection, you have to go into the database and add two note tags. For example:
The only required note tags are the
<mapImg> and
<mapImgIndex> tags. The first one is the filename (without the extension) that contains the character image that you want to use, and the second one is the index of the character image (since a character file can contain sprites for up to 8 characters, you have to tell it which one to use). The top left character is 0, and it increments to the right, and then goes to the second row and continues to increment.
The
<faceImg> and
<faceImgIndex> note tags are optional. But if you include them, the event will automatically put the monster's face image into a message when you talk to it.
That's basically all you've gotta do. The event itself doesn't need to ever be edited in any way. You can add and remove monsters, and change their images, simply by using those note tags in the database. The event will then automatically parse your database and compile a list of enemies that have a
<mapImg> note tag. Then it will parse your troops and compile a list of all troops that contain at least one enemy that has said note tag. Then it will randomly select one troop from the list. Then it will randomly select one monster from that troop, and it will set itself to use the character image of that monster.
Then when you talk to the monster, it will show you a message and it will automatically display the face image of that monster, if you specified one. And if you're an MZ user, it will also automatically grab the monster's name from the database, and show that on the message as well.
Spoiler: Monster Talking Example
You can ignore most of the stuff in that screenshot, as that map is just a scratchpad I use to test various scripts/events sometimes. The 3 NPCs on top of the water are the only relevant ones. They're all copies of the exact same event, but as you can see in the screenshot, they each spawned with a different image. And upon talking to to them, they will all show an appropriate face image and name, and will initiate a battle with an appropriate troop (that contains at least one of that monster). And they will each have their own independent respawn timer.
I did this using 3 event pages, some self switches, 1 switch, 2 variables, and 1 common event. I'm pretty bummed about the switch, variables, and common event, as I initially didn't plan to use them. I had thought I could get it all done using just an event with 3 pages, but then I noticed a possible bug that can happen in a specific scenario, so I had to apply a bit of a band-aid. I'd have preferred to have used a very very tiny plugin instead, but rules are rules.
I'll go ahead and paste pictures of the events right now. Then I'll come back and edit the post with commented code to try to better explain what's going on:
Page 1 does a bunch of hoodoo (which I probably over-complicated) in order to parse the database and randomly select an appropriate troop as well as its representative enemy. It then hacks the self switch system to save that information for later usage, and it also sets up the image information for Page 2, so that it will display the correct NPC on the map. It also turns on a switch to make sure that my common event (will show later) is running. Oh, and before I forget, you need to put a
<randomMonster> tag in the note box of your event.
Page 2 uses the hacked self switch to figure out which face and name to display in the message, and displays said message, then initiates battle (using another hacked self switch to know which troop was randomly generated), and then saves the current game time + 5 minutes into another hacked self switch, before moving to page 3.
Page 3 just continually checks whether or not a respawn is necessary. I put a 20 frame wait between each check, so the parallel event shouldn't really have any notable impact on performance. It wasn't necessary to use a loop here, and I normally wouldn't, but I chose to do so, in order to demonstrate how one can use a script call to break out of an event command loop, and also how to use a script call to wait.
Like I mentioned earlier, I don't like this Common Event, but I had to use it. The previous event has an issue where, if you leave the map while it's on page 2, and then come back, it won't be displaying the correct NPC image anymore. Without being able to use any plugins, a common event was the least annoying solution that I could come up with. What it does is it detects when the map has changed, and then it iterates through each event on the map, looking for any that has the <randomMonster> note tag and that is also on Page 2, and it ensures that the correct image is being used. This common event shouldn't really harm performance, as I made it as low impact as possible. But I still am unhappy that I had to use it. I'll probably cry about it for 3 days and 2 nights.
You can copy and paste the map event as many times as you want, and each one will work independently. You don't need to make additional copies of the Common Event. The one Common Event will cover as many copies of the map event as you want.
Here are the script calls that I used, with comments added to try to explain each block of code:
Spoiler: Page 1 Script
JavaScript:
// Parse database for enemies that have a mapImg note tag
var possibleEnemies = $dataEnemies.filter(enemy => !!enemy?.meta.mapImg);
// Parse database for troops that contain those enemies
var possibleTroops = $dataTroops.filter(troop => {
return troop?.members.some(member => {
return possibleEnemies.some(enemy => enemy.id === member.enemyId)
})
});
// Select a random troop from that list, and save the random troop into a
// self switch, so we can access it from other pages
const randomTroop = possibleTroops[Math.randomInt(possibleTroops.length)];
$gameSelfSwitches._data[[this._mapId, this._eventId, 'randomTroop']] = randomTroop;
// Iterate through the members of our selected troop, to make a list of enemies
// that we have image data for
possibleEnemies = possibleEnemies.filter(enemy => {
return randomTroop.members.some(member => member.enemyId === enemy.id)
});
// Select a random enemy from that list, set up the image data for page 2,
// and save the random enemy to a self switch, so we can access it from
// other pages
const randomEnemy = possibleEnemies[Math.randomInt(possibleEnemies.length)];
if (randomEnemy) {
$gameMap.event(this._eventId).event().pages[1].image.characterName = randomEnemy.meta.mapImg;
$gameMap.event(this._eventId).event().pages[1].image.characterIndex = parseInt(randomEnemy.meta.mapImgIndex) || 0;
}
$gameSelfSwitches._data[[this._mapId, this._eventId, 'randomEnemy']] = randomEnemy;
Spoiler: Page 2 Scripts
First script call:
JavaScript:
// Grab our randomly selected enemy from the self switch, and extract the face
// data
const randomEnemy = $gameSelfSwitches._data[[this._mapId, this._eventId, 'randomEnemy']];
const face = randomEnemy.meta.faceImg || '';
const faceIndex = parseInt(randomEnemy.meta.faceImgIndex) || 0;
// Build the message that we will show to the player. I added a check for MZ
// before trying to set the speaker's name, to keep this compatible between
// both MV and MZ
$gameMessage.setFaceImage(face, faceIndex);
$gameMessage.setBackground(0);
$gameMessage.setPositionType(2);
if (Utils.RPGMAKER_NAME === "MZ") $gameMessage.setSpeakerName(randomEnemy.name);
$gameMessage.add("I'm gonna keel you, sucka!");
this.setWaitMode("message");
Script call used in the control variables command (right before the Battle Processing):
JavaScript:
$gameSelfSwitches._data[[this._mapId, this._eventId, 'randomTroop']].id
Last Script Call:
JavaScript:
// Save the current play time + 5 minutes into a self switch, so we can access it
// from the next page
$gameSelfSwitches._data[[this._mapId, this._eventId, 'respawnTime']] = $gameSystem.playtime() + 300;
Spoiler: Page 3 Script
JavaScript:
// If the current playtime exceeds our target playtime, break out of the event loop
// otherwise, wait 20 frames
if ($gameSystem.playtime() >= $gameSelfSwitches._data[[this._mapId, this._eventId, 'respawnTime']]) {
this.command113();
} else {
this.wait(20);
}
Spoiler: Common Event Script
JavaScript:
// Iterate through all events on the map, and correct images for any that have
// a randomMonster note tag and are also currently on event page 2
for (const event of $gameMap.events()) {
if (event.event().meta.randomMonster && event._pageIndex === 1) {
const randomEnemy = $gameSelfSwitches._data[[this._mapId, event._eventId, 'randomEnemy']];
event.setImage(randomEnemy.meta.mapImg, parseInt(randomEnemy.meta.mapImgIndex));
}
}
Here's a video. I have it set to a 10 second respawn time for demonstration purposes:
▶ YouTube video