So I'm trying to make a troop randomizer script but to only be run on specific troops. I am having trouble finding where the code is for troop creation. Any help on where to start would be great.
MZ Trying to make troop randomize script
Learning Javascriptthere is no code for troop creation - the troops are created in the database, long before the game is ever launched.
there are two default ways to randomize troops, no need for javascript at all:
1)
you can make multiple different troops and have them all in the selection of what the player can engage (automatically by adding them to the random encounter list, or with control variable to get a variable troop id on evented encounters)
2)
you make a placeholder enemy in a troop, and have a troop event run on turn 0 to transform enemy with different enemies as random selection
there are two default ways to randomize troops, no need for javascript at all:
1)
you can make multiple different troops and have them all in the selection of what the player can engage (automatically by adding them to the random encounter list, or with control variable to get a variable troop id on evented encounters)
2)
you make a placeholder enemy in a troop, and have a troop event run on turn 0 to transform enemy with different enemies as random selection
@Andar Yeah i was doing the first one but it started to get a little out of hand with how many troops I was getting up to. I'll try the second one to see if I can make that work well enough.
EDIT:
So I tried the second option and it works but it looks really rough. Is there no way to make the random roll and transform happen before the screen fades in?
EDIT:
So I tried the second option and it works but it looks really rough. Is there no way to make the random roll and transform happen before the screen fades in?
Note that Transform Enemy caps current resources to their maximum, but doesn't apply full recovery. Consider giving the placeholder enemy the highest max HP/MP possible. Also, Turn 0 occurs after the emerge messages, so it may not be ideal.
For a plugin approach:
My idea here is just to do the usual setup, then check all enemies present and replace any that are tagged to be replaced.
[Edit: corrected
[Edit 2: corrected
For a plugin approach:
Game_Troop.prototype.setup (rmmz_objects.js) populates the troop's _enemies list. By default that's from the database data, like Andar says, but you could change that with a plugin. Maybe something like this (untested):
JavaScript:
/*:
* @target MZ
* @plugindesc Random enemy placeholders.
* @url https://forums.rpgmakerweb.com/index.php?threads/149648/
* @help Use a tag like this on enemies:
* <replace: 1, 2, 3>
* This enemy will be randomly replaced by enemy ID 1, 2, or 3 in-game.
*
* Free to use and/or modify for any purpose, no credit required.
*/
(() => {
'use strict';
const TAG_NAME = "replace";
const getRandomEnemyIDs = function(enemy) {
return enemy?.enemy?.().meta[TAG_NAME]
?.split(",").map(s => parseInt(s, 10)) ?? [];
};
const alias = Game_Troop.prototype.setup;
Game_Troop.prototype.setup = function(troopId) {
alias.apply(this, arguments);
for (let n = this._enemies.length; n--;) {
const nme = this._enemies[n];
const ids = getRandomEnemyIDs(nme);
if (ids.length) {
const r = Math.randomInt(ids.length);
this._enemies[n] = new Game_Enemy(
ids[r], nme.screenX(), nme.screenY()
);
if (nme.hidden)
this._enemies[n].hide();
}
}
this.makeUniqueNames();
};
})();
[Edit: corrected
(alias => { to (() => {.][Edit 2: corrected
nme.x to nme.screenX() (and likewise for y). Also corrected r to ids[r] when being passed to new Game_Enemy.]@caethyril This might just be what i was looking for. Thank you very much.
I don't know exactly what your game setup is like, or what you're trying to achieve. But is there a point/difference to doing this versus just making the different possible troops and giving them an equal weight in your random encounter table?
@ATT_Turan I'm trying to reduce the number of troops per area
The option caethyril offered looks like it will work, once I figure out the alias has already been declared issue. If i cant Ill just make more troops.
The option caethyril offered looks like it will work, once I figure out the alias has already been declared issue. If i cant Ill just make more troops.
Sorry, I had a typo, just edited my previous post to correct that. I guess you already figured it out?
@caethyril I hadn't yet got caught up working on some other things. That fix did the trick no more error, but the alternate enemies are not showing up. I got to get off to work so I'll work on it when I get home.
EDIT:
I had a chance to work with it a bit but for the life of me I cannot figure out why the enemies wont show up. Its weird because it does the replace but the new enemy isn't visible but is attackable and will attack.
I did test against other plugins and there is no conflict there.
EDIT:
I had a chance to work with it a bit but for the life of me I cannot figure out why the enemies wont show up. Its weird because it does the replace but the new enemy isn't visible but is attackable and will attack.
I did test against other plugins and there is no conflict there.
Sorry, looks like there were a couple more mistakes! That's what I get for not testing first. 😴
Game_Enemyposition isscreenX()andscreenY()rather thanxandy. The replaced enemies were being assignedNaNpositions, causing them to not display.
rshould be the index ofids, not the ID itself.
Looks like its working perfect. Thanks so much for the help!
Great, happy RPG Making! 👋
Original thread: https://forums.rpgmakerweb.com/threads/trying-to-make-troop-randomize-script.149648/