MV plugin that controls switches based on current tileset id

Hello there,

the basic idea of this plug-in is that you can determine switches to be automatically turned on/off depending on the tilesets id. And make it possible to set these switches and tilesets in the parameter setting to make entire development process much smoother.

If there is a similar plug-in, I'll be glad to know about that. and here is the part where I got stuck to implement:
1) how can I check if the parameter conditions are met, and perform the logic without less memory leakage.


/*:
* @plugindesc Toggle switches based on the player's current tileset ID.
* @param OnSwitches
* @type struct<SwitchTileset>[]
* @default []
* @param OffSwitches
* @type struct<SwitchTileset>[]
* @default []
*/
/*~struct~SwitchTileset:
* @param SwitchID
* @type switch
* @desc The switch ID to be toggled.
* @param TilesetIDs
* @type string
* @desc Tileset IDs separated by commas. Use hyphen for range (e.g., "1,3" or "1-3").
*/

(function() {
var parameters = PluginManager.parameters('TilesetSwitches');
var onSwitches = JSON.parse(parameters['OnSwitches'] || '[]').map(JSON.parse);
var offSwitches = JSON.parse(parameters['OffSwitches'] || '[]').map(JSON.parse);

var _Game_Map_update = Game_Map.prototype.update;
Game_Map.prototype.update = function(sceneActive) {
_Game_Map_update.call(this, sceneActive);
this.updateSwitches();
};

Game_Map.prototype.updateSwitches = function() {
var tilesetId = $gameMap.tilesetId();
console.log('Current Tileset ID:', tilesetId);

// Process OnSwitches
onSwitches.forEach(function(setting) {
if (checkTilesetMatch(tilesetId, setting.TilesetIDs)) {
console.log('Turning on switch:', setting.SwitchID);
$gameSwitches.setValue(Number(setting.SwitchID), true);
}
});

// Process OffSwitches
offSwitches.forEach(function(setting) {
if (checkTilesetMatch(tilesetId, setting.TilesetIDs)) {
console.log('Turning off switch:', setting.SwitchID);
$gameSwitches.setValue(Number(setting.SwitchID), false);
}
});
};

function checkTilesetMatch(tilesetId, tilesetIDs) {
var ranges = tilesetIDs.split(',');
return ranges.some(function(range) {
if (range.includes('-')) {
var bounds = range.split('-').map(Number);
return tilesetId >= bounds[0] && tilesetId <= bounds[1];
} else {
return tilesetId == Number(range);
}
});
}
})();




thank you!
I'm not sure what exactly is the problem here. Is there some problem with the code you posted, and if so, what is it?

If all you want to know is whether this would cause any memory leaks - it shouldn't. It doesn't do anything that could possibly cause that.

Though I would recommend calling updateSwitches in Scene_Map.prototype.onMapLoaded rather than in update, since it doesn't need to run on every frame. And then also call it whenever you change the tileset, if you ever use multiple tilesets on one map.
could you please add a bit on what the purpose and use for these switches would be?

Because in usual cases you know the tileset ID when creating the map, and could handle this simply with an autorun to set the switches on the map. And even using the change tileset command you could simply include that in the same event after the change
Did you write this code?

It seems unusual that you'd know how to write all of that but not be able to figure out how to check whether the parameter conditions are met.

It also, as Andar says, doesn't really need a plugin. It's very simple to have an Autorun event that you paste onto each map and change the switch ID inside.
Did you write this code?

It seems unusual that you'd know how to write all of that but not be able to figure out how to check whether the parameter conditions are met.

It also, as Andar says, doesn't really need a plugin. It's very simple to have an Autorun event that you paste onto each map and change the switch ID inside.
could you please add a bit on what the purpose and use for these switches would be?

Because in usual cases you know the tileset ID when creating the map, and could handle this simply with an autorun to set the switches on the map. And even using the change tileset command you could simply include that in the same event after the change
I'm not sure what exactly is the problem here. Is there some problem with the code you posted, and if so, what is it?

If all you want to know is whether this would cause any memory leaks - it shouldn't. It doesn't do anything that could possibly cause that.

Though I would recommend calling updateSwitches in Scene_Map.prototype.onMapLoaded rather than in update, since it doesn't need to run on every frame. And then also call it whenever you change the tileset, if you ever use multiple tilesets on one map.

Thx all for the reply!
I wanted to minimize efforts of manually adding map parallel events controlling certain switches that desired to be turned on / off in certain maps sharing the same tileset ids. Because I’m using lots of common parallel events to oversee weather, lighting, tints, keymapping, etcs that need to be changed depending on tileset id.

Since my current project is designed to be a semi-open world game, there are tons of maps (with different tile types) to control, so I wanted to minimize mistakes and efforts to manually control switches that only needed to be turn on/off in certain tile id.

Since I'm more of a game artist myself, I asked gpt to make this code for me, but it seems the code was not working for my current and new projects after this console logs:

console.log('Current Tileset ID:', tilesetId);

so I was about to repair the code myself, but since I'm far away from coding, maybe adding events in each map would be much more time-saving like Turan said.
I asked gpt to make this code for me
AI generators, by definition, generate things - they make it up. The majority of code that comes out of them is entirely non-functional because it refers to functions or variables that just don't exist at all.

If ChatGPT can't consistently solve grade-level math problems or write convincing English, I'm not sure how it seems like a good idea to ask it to write code for you 😛

But aside from that, it's against forum rules to ask for troubleshooting with AI code.

maybe adding events in each map would be much more time-saving like Turan said.
That's how I would do it - I wouldn't bother writing a plugin for it. I would make one Autorun event, it turns on a switch then has Erase Event. Copy and paste that onto each map, edit the Control Switches command to change the ID.

If you need help with a line of code that will turn off all other map switch IDs, you can ask for that and it's easily provided.
First of all, we programmers usually don't like to assist with AI-generated code. You already have a fully-functional code, so your statement "it has memory leaks" was confusing because the code has no memory leak at all. Now, that we know the code was AI-generated, we can fully understand your question.

For the future, please add to your original post when your code is AI-generated, thank you.

When you want to stick with your code, at least change the following:
  • Remove console.log, you don't need it
  • Right now, the code refreshes your map 60x in a second which is way too much. Only flip a switch or change a game variable when the content was changed.
First of all, we programmers usually don't like to assist with AI-generated code. You already have a fully-functional code, so your statement "it has memory leaks" was confusing because the code has no memory leak at all. Now, that we know the code was AI-generated, we can fully understand your question.

For the future, please add to your original post when your code is AI-generated, thank you.

When you want to stick with your code, at least change the following:
  • Remove console.log, you don't need it
  • Right now, the code refreshes your map 60x in a second which is way too much. Only flip a switch or change a game variable when the content was changed.
AI generators, by definition, generate things - they make it up. The majority of code that comes out of them is entirely non-functional because it refers to functions or variables that just don't exist at all.

If ChatGPT can't consistently solve grade-level math problems or write convincing English, I'm not sure how it seems like a good idea to ask it to write code for you 😛

But aside from that, it's against forum rules to ask for troubleshooting with AI code.


That's how I would do it - I wouldn't bother writing a plugin for it. I would make one Autorun event, it turns on a switch then has Erase Event. Copy and paste that onto each map, edit the Control Switches command to change the ID.

If you need help with a line of code that will turn off all other map switch IDs, you can ask for that and it's easily provided.
Opps, my bad. sorry, I wasn't aware of that.
Your reply helped me a lot, thanks for the help!

I think I'm gonna stick with adding events on maps like Turan said. Thanks!

Original thread: https://forums.rpgmakerweb.com/threads/plugin-that-controls-switches-based-on-current-tileset-id.170737/