Is it possible to generate a troop encounter by using script calls?

I don't have any time left today to search through google and the code of RPG maker MV to find my answer so i'm hoping someone can help me answer this and show me how.

My earlier assumption was that a troop was generated from the info stored in $dataTroops[id] and so I made a line of code to test it in the debug console:
Code:
var b = [1,2];var posX = [281,251];var posY = [444,343];
for(i=0;i<b.length;i++){
var c = $dataTroops[25].members.length;$dataTroops[25].members.push({});
$dataTroops[25].members[c][c]={enemyId:b[i],hidden:false,x:posX[i],y:posY[i]}
}
And called the battle like this:
Code:
BattleManager.setup(25, true, true);
$gamePlayer.makeEncounterCount();
SceneManager.push(Scene_Battle);
But I simply get the an empty troop encounter (As it is in the database).

Any help/answer is appreciated.
Code:
$dataTroops[25].members[c][c]={enemyId:b[i],hidden:false,x:posX[i],y:posY[i]}
You have two cs there. You should only have one.
Code:
$dataTroops[25].members[c][c]={enemyId:b[i],hidden:false,x:posX[i],y:posY[i]}
You have two cs there. You should only have one.
If I would change that then the structure as I see it in the debug console will differ in comparison with an troop created in the database.
If you type this in the debug:
Code:
$dataTroops[someID].members
You will see that they have put the troop member's info inside an object named "0"
Also the object is stored in an array thus at array index [0] is an object named "0".

I'm still willing to test your method, however currently I can't it has to wait till tonight or tomorrow.
No, it's not in object called 0! It's an object that's in the first place inside an array and hence it has an index 0.
Not sure if it helps, but there are plugins that allow you to "summon" enemies from other troops into a battle. This means you could start a battle and summon the necessary troops on startup, which would kinda allow you to do this.
No, it's not in object called 0! It's an object that's in the first place inside an array and hence it has an index 0.
Well....... It works must have gotten confused somewhere.

Thank you very much for pointing it out to me now I can continue with it.

Now i'm really gone gotta go to work someone else is doing dinner now.
Not sure if it helps, but there are plugins that allow you to "summon" enemies from other troops into a battle. This means you could start a battle and summon the necessary troops on startup, which would kinda allow you to do this.
This would be nice but not entirely what i'm searching for but i'd like to still have a look at it.

Poryg was right about what the problem was so I can continue.
Still thanks for offering a hand though.
Moved to: Learning JavaScript
Learning JS is a good board to ask questions about how MV's default codebase works so that you can replicate and modify stuff on your own.

Additionally: Double-post notice: @jetboost
If you need to quote multiple posts, use the Multi-Quote function (click on the "+ Quote" link below each post, then click the "Insert Quotes" button below the box where you type your response).
Battles are instances. I'm going to give you a rough idea of exactly what you need:
Code:
// rpg_manager.js
BattleManager.setup = function(troopId, canEscape, canLose) {
    this.initMembers();
    this._canEscape = canEscape;
    this._canLose = canLose;
    $gameTroop.setup(troopId);
    $gameScreen.onBattleStart();
    this.makeEscapeRatio();
};

// rpg_object.js
Game_Troop.prototype.setup = function(troopId) {
    this.clear();
    this._troopId = troopId;
    this._enemies = [];
    this.troop().members.forEach(function(member) {
        if ($dataEnemies[member.enemyId]) {
            var enemyId = member.enemyId;
            var x = member.x;
            var y = member.y;
            var enemy = new Game_Enemy(enemyId, x, y);
            if (member.hidden) {
                enemy.hide();
            }
            this._enemies.push(enemy);
        }
    }, this);
    this.makeUniqueNames();
};

To script a battle you'll have to call both BattleManager and Game_Troop from the scene but to get a default battle you'll have to copy and paste with a few changes.

All of the "this" in the BattleManager.setup function becomes "BattleManager" and all of the "this" in the Game_Troop.setup function must become "$gameTroop".

The "$gameTroop.setup(troopId);" inside BattleManager.setup must then be replaced by all of the Game_Troop.setup's code.

You'll get something roughly like this:
Code:
//You are now missing parameters troopId, canEscape, canLose. Those must be filled in with values
var troopId = 2; // This is the base troop
var canEscape = true;
var canLose = true;

BattleManager.initMembers();
BattleManager._canEscape = canEscape;
BattleManager._canLose = canLose;

// Game_Troop.prototype.setup
$gameTroop.clear();
$gameTroop._troopId = troopId;
$gameTroop._enemies = [];

$gameTroop.troop().members.forEach(function(member) {
    if ($dataEnemies[member.enemyId]) {
        var enemyId = member.enemyId;
        var x = member.x;
        var y = member.y;
        var enemy = new Game_Enemy(enemyId, x, y);
        if (member.hidden) {
            enemy.hide();
        }
        $gameTroop._enemies.push(enemy);
    }
}, $gameTroop);
$gameTroop.makeUniqueNames();
// 

$gameScreen.onBattleStart();
BattleManager.makeEscapeRatio();

Now things will get tricky. Inside the $gameTroop.troop().members.forEach is where enemies are generated and pushed into the current instance. To add enemies you'll have to generate enemies and push them in. It can be done like so, as an example:
Code:
//You are now missing parameters troopId, canEscape, canLose. Those must be filled in with values
var troopId = 2; // This is the base troop
var canEscape = true;
var canLose = true;

BattleManager.initMembers();
BattleManager._canEscape = canEscape;
BattleManager._canLose = canLose;

// Game_Troop.prototype.setup
$gameTroop.clear();
$gameTroop._troopId = troopId;
$gameTroop._enemies = [];

$gameTroop.troop().members.forEach(function(member) {
    if ($dataEnemies[member.enemyId]) {
        var enemyId = member.enemyId;
        var x = member.x;
        var y = member.y;
        var enemy = new Game_Enemy(enemyId, x, y);
        if (member.hidden) {
            enemy.hide();
        }
        $gameTroop._enemies.push(enemy);
    }
}, $gameTroop);
$gameTroop.makeUniqueNames();
// 

// Manual generation of an enemy Id 3 at location 300, 300 on the battle screen 
var enemyId = 3;
var x = 300;
var y = 300;
var enemy = new Game_Enemy(enemyId, x, y);
if (false) { // Do you want the enemy to be hidden?
    enemy.hide();
}
$gameTroop._enemies.push(enemy);
//

$gameScreen.onBattleStart();
BattleManager.makeEscapeRatio();

Now that setup is done just run:
Code:
SceneManager.push(Scene_Battle);

A bit of warning because you are injecting directly into the instance the injection will be deleted when the instance is deleted and because the instance won't match the database, default troop event calls and other plugins will most likely be buggy.
@Adventurer_inc. Not for nothing, but all these shenanigans got automated by the OP creating a new entry inside $dataTroops and hence are not needed. The only thing worth noting is that the $dataTroops gets reset to its normal state after game restart, but that's why there's the troop definition.
@Poryg True, but it seemed like jetboost was trying to call a battle and inject enemies into a battle. The process I went through is exactly how you would do it, messy as it is. No script edits, no plugin, and little conflicts; just clean in one way and out the other. It can now be easily automated however you want.
Thread closed IgnoreMe

Original thread: https://forums.rpgmakerweb.com/threads/is-it-possible-to-generate-a-troop-encounter-by-using-script-calls.111960/