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"
This code is based off lots of troubleshooting attempts, if any of it looks weird.
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!
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"
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!