MZ How to transfer the player to a specific region id on another map?

I've been working alone and with the assistance ChatGPT to make a personal plugin that transfers the player to a specific region on a different map.

The code is correctly finding the coordinates of the region, so theoretically we can transfer the player correctly. But I keep getting the error "rmmz_managers.js:2036 TypeError: Cannot read property 'scrollType' of null"

TransferToRegionError01.PNG


This code is based off lots of troubleshooting attempts, if any of it looks weird.

JavaScript:
async function transferPlayerToSpawnMap(layout, spawnMapId) {
        console.log(`Transferring player to spawn map: ${spawnMapId}`);
        const regionId = Number(PluginManager.parameters(pluginName)["SpawnArrivalRegionID"]);
        console.log(`Spawn Arrival Region ID: ${regionId}`);


        for (let y = 0; y < layout.length; y++) {
            for (let x = 0; x < layout[y].length; x++) {
                if (layout[y][x] && layout[y][x].mapId === spawnMapId) {
                    try {
                        console.log(`Getting coordinates for region ID: ${regionId} on map ID: ${spawnMapId}`);
                        const coordinates = await getRegionCoordinates(spawnMapId, regionId);
                        console.log(`Coordinates found for region ID: ${regionId} on map ID: ${spawnMapId} - `, coordinates);


                        if (coordinates.length === 0) {
                            console.error(`No coordinates found for region ID: ${regionId}`);
                            return;
                        }


                        const { x: targetX, y: targetY } = coordinates[Math.floor(Math.random() * coordinates.length)];
                        console.log(`Transferring player to coordinates (${targetX}, ${targetY}) on map ID: ${spawnMapId}`);
                        
                        // Add delay here before transferring the player
                        setTimeout(() => {
                            $gamePlayer.reserveTransfer(spawnMapId, targetX, targetY, 2, 0);
                            SceneManager.goto(Scene_Map);
                        }, 500); // 500ms delay
                        
                        return;
                    } catch (error) {
                        console.error(error.message);
                    }
                }
            }
        }


        console.error("No valid spawn map found for transfer.");
    }


    function getRegionCoordinates(mapId, regionId) {
        return new Promise((resolve, reject) => {
            const map = $dataMapInfos[mapId];
            if (!map) {
                reject(new Error(`Map ID ${mapId} not found.`));
                return;
            }


            DataManager.loadMapData(mapId).then(() => {
                console.log("Loading map data for map ID: ", mapId); // Add this log
                const coordinates = [];
                console.log(`Map width: ${$dataMap.width}, Map height: ${$dataMap.height}`); // Add this log
                for (let x = 0; x < $dataMap.width; x++) {
                    for (let y = 0; y < $dataMap.height; y++) {
                       // console.log(`Checking region ID at (${x}, ${y})`); // Spam
                        if ($gameMap.regionId(x, y) === regionId) {
                            coordinates.push({ x, y });
                        }
                    }
                }


                if (coordinates.length > 0) {
                    resolve(coordinates);
                } else {
                    reject(new Error(`No coordinates found for region ID: ${regionId}`));
                }
            }).catch(reject);
        });
    }


    DataManager.loadMapData = function(mapId) {
        if (mapId > 0) {
            const filename = 'Map%1.json'.format(mapId.padZero(3));
            this.loadDataFile('$dataMap', filename);
        } else {
            this.makeEmptyMap();
        }
        return new Promise((resolve, reject) => {
            const checkDataLoaded = () => {
                if ($dataMap) {
                    resolve();
                } else {
                    setTimeout(checkDataLoaded, 100);
                }
            };
            checkDataLoaded();
        });
    };

I understand the basics of Javascript and have been working with ChatGPT for the more complex stuff, like above. But it isn't working so I thought I'd reach out to the community, if anyone knows how to get this to work without the error.

Thanks!
You'll have a hard time finding someone here to troubleshoot AI code.

If you're fine with doing the stuff when transferring into the new map (during the fadeout phase) there's probably easier scripts to do that, or you could spawn an empty event to the region + get its coordinates + transfer the player there.
To do it in one transfer, you'd need some kind of structure to load the map ahead of time, wait for it to be loaded, filter the target tiles, then perform the transfer.

Instead, I'd suggest simply using 2 transfers:
  1. Transfer event: Fadeout Screen, Transfer Player to (0, 0) on target map.
  2. Autorun on target map: various "If (Condition): Transfer Player, Else" blocks. Then Fadein Screen.
This worked for me back in RM2k3, anyway.

If you specifically want to do it by scanning regions, a Script command like this may work (untested):
JavaScript:
const regionId = 1;
// Get all tiles on map with matching region ID.
const a = [];
for (let x = $gameMap.width(); x--;)
  for (let y = $gameMap.height(); y--;)
    if ($gameMap.regionId(x, y) === regionId)
      a.push({ x, y });
// Transfer to a random matching tile, if any were found.
if (a.length) {
  const { x, y } = a[Math.randomInt(a.length)];
  // Transfer Player, direct designation, no direction change, no fade.
  this.command201([0, $gameMap.mapId(), x, y, 0, 2]);
}
This would go in the second event, before fading the screen back in.
You'll have a hard time finding someone here to troubleshoot AI code.
Yeeah I originally wasn't going to post asking for help at all cause I've been using AI, but I sucked up my pride and hit the Post button. Haven't been flamed yet!

If you're fine with doing the stuff when transferring into the new map (during the fadeout phase) there's probably easier scripts to do that, or you could spawn an empty event to the region + get its coordinates + transfer the player there.
I'm giving this a go! Thanks for the idea!
  1. Transfer event: Fadeout Screen, Transfer Player to (0, 0) on target map.
  2. Autorun on target map: various "If (Condition): Transfer Player, Else" blocks. Then Fadein Screen.
Samesies as above. I appreciate your help in this!!

Original thread: https://forums.rpgmakerweb.com/threads/how-to-transfer-the-player-to-a-specific-region-id-on-another-map.168951/