Is there anyway to comment out a part of a function through a patch? I find myself overwriting entire functions just to comment out one line and cannot help but feel there has to be a better way...
As Mac15001900 said, you can't exactly comment things out, but you can sometimes find workarounds to achieve a similar effect. For example, say that you want this function to *not* create a 'Save' command:
JavaScript:Window_MenuCommand.prototype.makeCommandList = function() {
this.addMainCommands();
this.addFormationCommand();
this.addOriginalCommands();
this.addOptionsCommand();
this.addSaveCommand();
this.addGameEndCommand();
};
Rather than overwriting it and removing the
this.addSaveCommand(); line, you could simply leave that line in, and overwrite the
Window_MenuCommand.prototype.makeCommandList function so that it's an empty function that does nothing:
JavaScript:Window_MenuCommand.prototype.addSaveCommand = function() { };
You wind up overwriting a function either way, but this way is less likely to lead to compatibility issues with other plugins.
For another example, take a look at this function:
JavaScript:Game_CharacterBase.prototype.setPosition = function(x, y) {
this._x = Math.round(x);
this._y = Math.round(y);
this._realX = x;
this._realY = y;
};
Suppose that, for whatever reason, you want it to *not* change the value of
this._realX. Instead of overwriting that function and commenting the line out, you could just patch the function and undo the effects of that line afterward:
JavaScript:const _Game_CharacterBase_setPosition = Game_CharacterBase.prototype.setPosition;
Game_CharacterBase.prototype.setPosition = function(x, y) {
const tempRealX = this._realX;
_Game_CharacterBase_setPosition.apply(this, arguments);
if (this._realX !== tempRealX) {
this._realX = tempRealX;
}
};
It's not any less work, but it has the potential to play nicer with other plugins. Pretty frequently, you can find workarounds such as these. But sometimes you just can't avoid overwriting a function. It's something that can only be determined on a case by case basis.