Change single event's image block

Sprite_Character.prototype.updateFrame = function() { if (this._character instanceof Game_Player) { this.updatePlayerFrame(); } else if (this._character instanceof Game_Event) { this.updateEventFrame(); };};Sprite_Character.prototype.updateEventFrame = function() { var pw = this.patternWidth(); var ph = this.patternHeight(); var sx = (this.eventBlockX() + this.characterPatternX()) * pw; var sy = (this.characterBlockY() + this.characterPatternY()) * ph; this.setFrame(sx, sy, pw, ph);};Sprite_Character.prototype.eventBlockX = function() { return eventBlockX;};Game_CharacterBase.prototype.changeIndex = function(BlockX, MaxPattern) { if (this instanceof Game_Player) { playerBlockX = BlockX; playerMaxPattern = MaxPattern; } else { eventBlockX = BlockX; eventMaxPattern = MaxPattern; };};I am using long sprite sheets, 8 x 32, and I was trying to make a script call to change the characters image (blockX). 

Game_CharacterBase.prototype.changeIndex = function(BlockX, MaxPattern) works for the player but when I use on an event it changes all the events because they are all linked to 'eventBlockX'. I have been trying to use 'this._character.eventId()' as a function parameter to only update the event that contains the script call but I cannot get it to work. I am wondering if anyone can throw me a bone here. Many hours. Many hairs lost. Thanks in advance.
The problem is most likely here:
 

Game_CharacterBase.prototype.changeIndex = function(BlockX, MaxPattern) { if (this instanceof Game_Player) { playerBlockX = BlockX; playerMaxPattern = MaxPattern; } else { eventBlockX = BlockX; //problem is here. Where is eventBlockX defined? Looks like defined in a way that would make it shared.. eventMaxPattern = MaxPattern; };};You have to look carefully at where eventBlockX is defined. It sounds like it is being defined in a location where it is being shared.

Example: 

//defining a variable outside of the function will allow it to be accessed inside of functions declared at the same scope of it.var sharedVariable = 0; function updateShared1() {   sharedVariable += 1;} function updateShared2() {  sharedVariable += 2;} updateShared1();updateShared2(); console.log(sharedVariable) //outputs 3However, if you want it to belong to an event, then you need to have the eventBlockX be set to a property of the event (probably in the initialize function), or through the use of define property:

Object.defineProperty(Game_Event.prototype, "eventBlockX", {    value: 0    writable: true})I hope this can help you find your error and correct it.

Lastly, use polymorphism instead of an if statement in the base class. (Override the function in Game_Event and/or Game_Player instead of putting it in the base class.)
Game_Event.prototype.initialize = function(mapId, eventId) { Game_Character.prototype.initialize.call(this); this._mapId = mapId; this._eventId = eventId; this.locate(this.event().x, this.event().y); this.refresh(); this._setBlockX = 0; this._setMaxPattern = 0;};Sprite_Character.prototype.eventBlockX = function() { return $gameMap.event(this._character.eventId())._setBlockX;};Game_CharacterBase.prototype.maxPattern = function() { if (this instanceof Game_Player) { return playerMaxPattern; } else if (this instanceof Game_Event) { return $gameMap.event(this.eventId())._setMaxPattern; };};Game_CharacterBase.prototype.pattern = function() { if (this instanceof Game_Player) { return this._pattern < playerMaxPattern ? this._pattern : 1; } else if (this instanceof Game_Event) { return this._pattern < $gameMap.event(this.eventId())._setMaxPattern ? this._pattern : 1; };};I can change the blockX now with '$gameMap.event(1)._setBlockX = #' but when I tried the same approach to the MaxPattern it just remains 0. 

I don't know if that is what you meant or not but if it works I will take it.

Whats wrong with putting it in the base class?
You're on the right track (although it is kind of awkward to name your variables with the word "set" in front of them).

Let me recap on what currently exists and what you're trying to do:

Currently, Sprite_Character decides on which sprite to use based on several parameters: characterIndex, characterPatternX, characterPatternY, patternWidth, patternHeight. From these parameters, first we decide what block of sprites to use, and then which frame from that set to look at.

The characterIndex is a value from 0 to 7, inclusive. The sprite sheet for characters normally is divided into 8 sections, four of which are on the first "block row", and four of which are on the second "block row". We can determine one of these sheets by using div and mod operations: we get the block column by characterIndex % 4, and the block row by character / 4 (where this is taken to be integer division).

Once we get into the block that we want, we use the pattern variable and the direction variable of a character to determine the characterPatternX and characterPatternY, which will give us the unique frame of size patternWidth and patternHeight to use from the sprite sheet. To do this, we note that pattern is a value from 0, ..., maxPattern - 1, where maxPattern is defined on the character object to be 3. However, when we actually read the pattern, we get the following loop: 0, 1, 2, 1, 0, 1, 2, 1, 0, ....

With that said, from what I can tell about your question is: You just want to use a sprite sheet that has more block columns. If that's the case, then the thing you need to be updating is just how the characterIndex is being used to choose the block. the pattern shouldn't be updated at all.

If, however, you are trying to change the length of a character animation (or how it animates), then that's a different question altogether. (The 3 that you see in the pattern function is not maxPattern or maxPattern - 1 in any situation, and is set up this way because the way they want to animate the character using their sprite setup.)

Can you better explain what you are trying to do? Otherwise, I can only advise you to be careful with your Javascript.
I am using sprite sheets with 8 directions (8 rows) and 32+ columns. The columns contain the character animations for walking, attacking, idle, ect. The length of the character animations vary. For example, changing the event's blockX to 0 and maxPattern to 4 will show the idle animation. blockX 4 and maxPattern 8 will show the running animation. 

Game_CharacterBase.prototype.maxPattern = function() { if (this instanceof Game_Player) { return playerMaxPattern; } else if (this instanceof Game_Event) { console.log(this._setMaxPattern); return this._setMaxPattern; };};Game_CharacterBase.prototype.pattern = function() { if (this instanceof Game_Player) { return this._pattern < playerMaxPattern ? this._pattern : 1; } else if (this instanceof Game_Event) { return this._pattern < this._setMaxPattern ? this._pattern : 1; };};I can see 'this._setMaxPattern' change in the log but the event acts as if it is still equal to 0.
The problem with doing instance checks in the base class is that it can make it confusing later, and it violates common design principles about base classes not knowing things about classes that derive from them. It's typically viewed as a bad practice, especially since polymorphism provides a cleaner way of doing things (otherwise, why bother using derived classes?).

For your animation, look into what pattern / maxPattern is actually doing (it's more or less responsible for the left foot, stand, right foot, stand, repeat pattern that is used when animating the walking portion). Based on what I can see with your stuff, you probably aren't using it right. Without specifics on how your animations are actually being designed, I can't advise you on why they are being wrongly used. I would advise to make your animations well-defined (in terms of how you are laying them out on your spritesheet), and then make animations based on how they are laid out (such as this.animation = "walking") and then choose maxPattern and how pattern updates based on what animation state you are in.
I thought it would be faster in the base. I don't know how to get more specific. Changing the BlockX and MaxPattern is the way to go. It works but it just applies to every event.  The BlockX works, so I guess we'll say mission accomplished for today. I'll bang my head on the MaxPattern later. Thanks for the help.

Original thread: https://forums.rpgmakerweb.com/threads/change-single-events-image-block.54217/