MV Show Sprite Icon from Weapon ID Database

I'm trying to code here, looking at rpg_objects stateIcons, rpg_windows drawIcon, and rpg_sprites Sprite_StateIcon..

here is the body code
Spoiler
JavaScript:
//-----------------------------------------------------------------------------
// Sprite_AttackIcon
//

function Sprite_AttackIconSprite() {
    this.initialize.apply(this, arguments);
}

Sprite_AttackIconSprite.prototype = Object.create(Sprite.prototype);
Sprite_AttackIconSprite.prototype.constructor = Sprite_AttackIconSprite;

Sprite_AttackIconSprite.prototype.initialize = function(character) {
    Sprite.prototype.initialize.call(this);
    //this._iconIndex = 0;
    this.loadBitmap();
    this.x = -34;
    this.y = -40;
    this.z = 9;
    this.anchor.x = 0.5;
    this.anchor.y = 0.5;
    this.scale.x = 0.5;
    this.scale.y = 0.5;
};

Sprite_AttackIconSprite._iconWidth  = 32;
Sprite_AttackIconSprite._iconHeight = 32;

Sprite_AttackIconSprite.prototype.loadBitmap = function() {
    this.bitmap = ImageManager.loadSystem('IconSet');
    //this.setFrame(0, 0, 0, 0);
};

Sprite_AttackIconSprite.prototype.setBattler = function(battler) {
    if (this._battler === battler) return;
    this._battler = battler;
};

// assign icon index here
Sprite_AttackIconSprite.prototype.update = function() {
    Sprite.prototype.update.call(this);
    if (this._iconIndex) {
        this.updateIcon();
        this.updateFrame();
        this.refresh();
    }
};

Sprite_AttackIconSprite.prototype.updateIcon = function() {
    if (!this._battler || this._battler.isActor()) {
        this._iconIndex = this._battler.weapons()[0].iconIndex;  // actor
    } else {
        this._iconIndex = $dataWeapons[Number(this._battler.enemy().meta.srpgWeapon)].iconIndex; // enemy
    }
};

Sprite_AttackIconSprite.prototype.updateFrame = function() {
    var pw = 32;
    var ph = 32;
    var sx = 101 % 16 * pw;
    var sy = Math.floor(101 / 16) * ph;
    this.setFrame(sx, sy, pw, ph);
};

// draw the icon text here
Sprite_AttackIconSprite.prototype.refresh = function() {
    this.bitmap.clear();
    if (this._iconIndex === 0) return;
    //var pw = Sprite_AttackIconSprite._iconWidth;
    //var ph = Sprite_AttackIconSprite._iconHeight;
    //var sx = this._iconIndex % 16 * pw;
    //var sy = Math.floor(this._iconIndex / 16) * ph;
    this.bitmap.blt(this.bitmap, sx, sy, pw, ph, x, y);
};

here is the create function:
JavaScript:
    Sprite_Character.prototype.createAttackIconSprite = function() {
        if (!this._AttackIconSprite) {
            this._AttackIconSprite = new Sprite_AttackIconSprite();
            var battler = $gameSystem.EventToUnit(this._character.eventId())[1];
            this._AttackIconSprite.setBattler(battler);
            this.addChild(this._AttackIconSprite);
        }
    };

Right now.. this is what it looks like.. its the whole "iconset" loaded and not trimmed!
1637321429509.png


this is how it should look like..
1637321681168.png
You have the if statement

if (this._iconIndex !== this._iconIndex), inside which you're calling the functions that set the frame.

The problem is, your if statement can never return true. You're checking whether a property is not equal to itself; it will always be equal to itself, so this will not run under any circumstances, hence the bitmap appearing as the entire image.
You have the if statement

if (this._iconIndex !== this._iconIndex), inside which you're calling the functions that set the frame.

The problem is, your if statement can never return true. You're checking whether a property is not equal to itself; it will always be equal to itself, so this will not run under any circumstances, hence the bitmap appearing as the entire image.
i updated the code and still null, same results.. its the whole sheet

JavaScript:
    // assign icon index here
    Sprite_AttackIconSprite.prototype.update = function() {
        Sprite.prototype.update.call(this);
        if (this._iconIndex !== 0) { // <-- this
            this._iconIndex = this._iconIndex;
            this.updateIcon();
            this.updateFrame();
            this.refresh();
        }
    };

    Sprite_AttackIconSprite.prototype.updateIcon = function() {
        if (!this._battler || this._battler.isActor()) {
            this._iconIndex = 97; // this._battler.weapons()[0]._iconIndex;  // actor  <-- this id to iconIndex
        } else {
            this._iconIndex = 101;//$dataWeapons[Number(battler.enemy().meta.srpgWeapon)]._iconIndex; // enemy <-- this id to iconIndex
        }
    };
You're only calling updateIcon if this._iconIndex is not 0, but updateIcon is what's setting it to a value greater than 0 in the first place.

Also this._iconIndex = this._iconIndex; is doing literally nothing.
all my changes did nothing, i dont what is the correct, i've commented out each line with //, to see what changes, it's like everything is null, even when i place this._iconIndex to have a value, its still the same results.
i searched a bit and found this:
JavaScript:
this.drawIcon(iconId,rect.x,rect.y);

it looks like this is used to draw an single icon,but i didnt test it..
Note: its a method of Window Base.
its from here#10


EDIT nr2
test idea:
Spoiler
JavaScript:
//make a window based function to use the draw icon script
Window_Base.prototype.testDrawIcon = function(iconId,rect.x,rect.y) {
    //get the data which is stored in the function => (iconId,rect.x,rect.y)
    var id = iconId;
    var x = rect.x;
    var y = rect.y;
    //use the draw icon script
    this.drawIcon(id,x,y);
};
//----------------------------------------
//get the data to store it the function above & to call the function above

var iconId = // put the id you wanna use here;
var rect.x = // put the id_related rect.x you wanna use here;
var rect.y = // put the id_related rect.x you wanna use here;

//now this should be possible to call with
Window_Base.prototype.testDrawIcon.call(this,iconId,rect.x,rect.y);
//this should be used instead of "this.drawIcon(iconId,rect.x,rect.y);"

//"this" always adress the function family in which it is used..
// thats why you cant use "this.drawIcon(iconId,rect.x,rect.y);" outside of a window based function
// not sure but i think other window functions should works aswell because they are conneted to "Window_Base"
// i have to admit i have not much exp on windows..
i made that test function to not conflict with other stuff if using any already existing functions that does the same

If anybody sees a mistake in my test code pls let us know!.thx
i've searched the related functions to bitmap sprite, but mine is lacking, the cropping/trimming of the bitmap "iconset" is not working, and seems like the index returned is zero. i dont know.

Spoiler: TMSrpg
JavaScript:
  Sprite_Character.prototype.updateStateIcon = function() {
    if (this._character.isSrpgUnit()) {
      if (!this._srpgStateIconSprite) {
        this._srpgStateIconSprite = new Sprite_StateIcon();
        this._srpgStateIconSprite.setup(this._character.srpgBattler());
        this._srpgStateIconSprite.y = -40;
        this._srpgStateIconSprite.z = 9;
        this._srpgStateIconSprite.scale.x = 0.5;
        this._srpgStateIconSprite.scale.y = 0.5;
        this.addChild(this._srpgStateIconSprite);
      }
    }
  };

Spoiler: rpg_sprites
JavaScript:
//-----------------------------------------------------------------------------
// Sprite_StateIcon
//
// The sprite for displaying state icons.

function Sprite_StateIcon() {
    this.initialize.apply(this, arguments);
}

Sprite_StateIcon.prototype = Object.create(Sprite.prototype);
Sprite_StateIcon.prototype.constructor = Sprite_StateIcon;

Sprite_StateIcon.prototype.initialize = function() {
    Sprite.prototype.initialize.call(this);
    this.initMembers();
    this.loadBitmap();
};

Sprite_StateIcon._iconWidth  = 32;
Sprite_StateIcon._iconHeight = 32;

Sprite_StateIcon.prototype.initMembers = function() {
    this._battler = null;
    this._iconIndex = 0;
    this._animationCount = 0;
    this._animationIndex = 0;
    this.anchor.x = 0.5;
    this.anchor.y = 0.5;
};

Sprite_StateIcon.prototype.loadBitmap = function() {
    this.bitmap = ImageManager.loadSystem('IconSet');
    this.setFrame(0, 0, 0, 0);
};

Sprite_StateIcon.prototype.setup = function(battler) {
    this._battler = battler;
};

Sprite_StateIcon.prototype.update = function() {
    Sprite.prototype.update.call(this);
    this._animationCount++;
    if (this._animationCount >= this.animationWait()) {
        this.updateIcon();
        this.updateFrame();
        this._animationCount = 0;
    }
};

Sprite_StateIcon.prototype.animationWait = function() {
    return 40;
};

Sprite_StateIcon.prototype.updateIcon = function() {
    var icons = [];
    if (this._battler && this._battler.isAlive()) {
        icons = this._battler.allIcons();
    }
    if (icons.length > 0) {
        this._animationIndex++;
        if (this._animationIndex >= icons.length) {
            this._animationIndex = 0;
        }
        this._iconIndex = icons[this._animationIndex];
    } else {
        this._animationIndex = 0;
        this._iconIndex = 0;
    }
};

Sprite_StateIcon.prototype.updateFrame = function() {
    var pw = Sprite_StateIcon._iconWidth;
    var ph = Sprite_StateIcon._iconHeight;
    var sx = this._iconIndex % 16 * pw;
    var sy = Math.floor(this._iconIndex / 16) * ph;
    this.setFrame(sx, sy, pw, ph);
};

Spoiler: rpg_windows
JavaScript:
Window_Base.prototype.drawIcon = function(iconIndex, x, y) {
    var bitmap = ImageManager.loadSystem('IconSet');
    var pw = Window_Base._iconWidth;
    var ph = Window_Base._iconHeight;
    var sx = iconIndex % 16 * pw;
    var sy = Math.floor(iconIndex / 16) * ph;
    this.contents.blt(bitmap, sx, sy, pw, ph, x, y);
};
i've searched the related functions to bitmap sprite, but mine is lacking, the cropping/trimming of the bitmap "iconset" is not working, and seems like the index returned is zero. i dont know.

Spoiler: TMSrpg
JavaScript:
  Sprite_Character.prototype.updateStateIcon = function() {
    if (this._character.isSrpgUnit()) {
      if (!this._srpgStateIconSprite) {
        this._srpgStateIconSprite = new Sprite_StateIcon();
        this._srpgStateIconSprite.setup(this._character.srpgBattler());
        this._srpgStateIconSprite.y = -40;
        this._srpgStateIconSprite.z = 9;
        this._srpgStateIconSprite.scale.x = 0.5;
        this._srpgStateIconSprite.scale.y = 0.5;
        this.addChild(this._srpgStateIconSprite);
      }
    }
  };

Spoiler: rpg_sprites
JavaScript:
//-----------------------------------------------------------------------------
// Sprite_StateIcon
//
// The sprite for displaying state icons.

function Sprite_StateIcon() {
    this.initialize.apply(this, arguments);
}

Sprite_StateIcon.prototype = Object.create(Sprite.prototype);
Sprite_StateIcon.prototype.constructor = Sprite_StateIcon;

Sprite_StateIcon.prototype.initialize = function() {
    Sprite.prototype.initialize.call(this);
    this.initMembers();
    this.loadBitmap();
};

Sprite_StateIcon._iconWidth  = 32;
Sprite_StateIcon._iconHeight = 32;

Sprite_StateIcon.prototype.initMembers = function() {
    this._battler = null;
    this._iconIndex = 0;
    this._animationCount = 0;
    this._animationIndex = 0;
    this.anchor.x = 0.5;
    this.anchor.y = 0.5;
};

Sprite_StateIcon.prototype.loadBitmap = function() {
    this.bitmap = ImageManager.loadSystem('IconSet');
    this.setFrame(0, 0, 0, 0);
};

Sprite_StateIcon.prototype.setup = function(battler) {
    this._battler = battler;
};

Sprite_StateIcon.prototype.update = function() {
    Sprite.prototype.update.call(this);
    this._animationCount++;
    if (this._animationCount >= this.animationWait()) {
        this.updateIcon();
        this.updateFrame();
        this._animationCount = 0;
    }
};

Sprite_StateIcon.prototype.animationWait = function() {
    return 40;
};

Sprite_StateIcon.prototype.updateIcon = function() {
    var icons = [];
    if (this._battler && this._battler.isAlive()) {
        icons = this._battler.allIcons();
    }
    if (icons.length > 0) {
        this._animationIndex++;
        if (this._animationIndex >= icons.length) {
            this._animationIndex = 0;
        }
        this._iconIndex = icons[this._animationIndex];
    } else {
        this._animationIndex = 0;
        this._iconIndex = 0;
    }
};

Sprite_StateIcon.prototype.updateFrame = function() {
    var pw = Sprite_StateIcon._iconWidth;
    var ph = Sprite_StateIcon._iconHeight;
    var sx = this._iconIndex % 16 * pw;
    var sy = Math.floor(this._iconIndex / 16) * ph;
    this.setFrame(sx, sy, pw, ph);
};

Spoiler: rpg_windows
JavaScript:
Window_Base.prototype.drawIcon = function(iconIndex, x, y) {
    var bitmap = ImageManager.loadSystem('IconSet');
    var pw = Window_Base._iconWidth;
    var ph = Window_Base._iconHeight;
    var sx = iconIndex % 16 * pw;
    var sy = Math.floor(iconIndex / 16) * ph;
    this.contents.blt(bitmap, sx, sy, pw, ph, x, y);
};
the last Spoiler_function is the one which you wanna call to draw the icon
(instead of my test funtion for example)

get the data:
var iconIndex = //put stuff here
var x = //put stuff here
var y = //put stuff here

call the function:
Window_Base.prototype.drawIcon.call(this, iconIndex, x, y);

The Middle spoiler_function, is handling anchors,adding battler,other data & updates ect
(for the Sprite_StateIcon)

The First Function is probably from the your plugin , and its used to update iconSprites..
but where did you add the index data?? i can only see x y z data in that function..
also i do not know what "z" stands for?? or is it a typoMistake and should be "x"??


edit 2

I don not know this if condtion?? where does it come from?
if (this._character.isSrpgUnit()) {
-> its NOT part of the SRPG_Core
if thats only part of Tomoaky Plugins, than you cant use it..
you need a similar if condtion from the srpg Core

edit3
similar if condtion fron the SRPG_core:
Screenshot from my plugin "mapStateOverlay"
Screenshot_1.png
z is the layer priority of the image. you cant use window_base functions on sprite functions. i already rewrote / fused / extracted those 3.
you just need to rewrite the functions name like what i did Sprite_StateIcon -> Sprite_AttackIconSprite

even if i use this code from draw icon, the whole icon set is still displayed
JavaScript:
        this.bitmap.blt(this.bitmap, 101 % 16 * 32, Math.floor(101 / 16) * 32, 32, 32, 0, 0);
z is the layer priority of the image. you cant use window_base functions on sprite functions. i already rewrote / fused / extracted those 3.
you just need to rewrite the functions name like what i did Sprite_StateIcon -> Sprite_AttackIconSprite
so if thats not the problem , than its probably the "if condition" that cant be "true",
with using the SRPG Core..
Or do you use any srpg plugin that stores the related function?
if (this._character.isSrpgUnit()) {

i searched in the srpg core for "isSrpgUnit", but it doesnt exist..
so if thats not the problem , than its probably the "if condition" that cant be "true",
with using the SRPG Core..
Or do you use any srpg plugin that stores the related function?
if (this._character.isSrpgUnit()) {

i searched in the srpg core for "isSrpgUnit", but it doesnt exist..
that is from TMSrpg.. for SRPG_Core, that condition is just
if (!this._battler || this._battler.isActor())
var battler = $gameSystem.EventToUnit(this._character.eventId())[1];
if this event character is an event unit..
that is from TMSrpg.. for SRPG_Core, that condition is just
if (!this._battler || this._battler.isActor())
var battler = $gameSystem.EventToUnit(this._character.eventId())[1];
if this event character is an event unit..
ok but you cant use a TMSrpg related "If Condition", it will never be "true" without the TMSrpg_plugin
(because the srpg core aswell as your Plugin ,cant check the related function,when it doesnt exist)

or perhaps i misssunderstood something
how to properly use setframe? the way i use it in the code, i dont get the effect, i already checked the plugin crop picture

i made my own version of sprite_stateicon to sprite_attackicon, and i get incredible lag.
how to properly use setframe? the way i use it in the code, i dont get the effect, i already checked the plugin crop picture
there is 1 singel match ,in "rpg_windows":
Screenshot_1.png

But i am not sure if thats helpfull,..also i do not know what the data (0, 0, w, h) means.
------------------------------------------------------------------------------------------
And there are several matches in "rpg_sprites"
(1 example img.. its the first one that appears from the top)
Screenshot_2.png

-----------------------------------------------------------------

and google just showed me this topic, atleast there is a: "Sprite_Character.prototype.updateCharacterFrame"function
where "setframe" is used.. perhaps thats helpfull as example idk


the srpg core doesnt use it ,so it will just use the default setup "setframe"-usage from the "rpg default plugins" ..showed in the imgs above
-> i only could find "setFrame" in those 2 default plugins..

Edit
the data that is used on setFrame seems to be about xy positions & width/height of the sprite or window..
@dopan the plugin is finished.. i settled with tomoaky's method of using the default stateicon rather than making my own attackicon.. i just had to use yep_autopassivestates..
@dopan the plugin is finished.. i settled with tomoaky's method of using the default stateicon rather than making my own attackicon.. i just had to use yep_autopassivestates..
Congratz!
well then your plugin requires the yep plugin?

in such case i will probably cut down the code on the ealier version to only get the other functions (without the iconUsage).. for my private project.. to avoid having code inthere which isnt used..


spoiler off topic
Spoiler
However the enemyEquip/Steal plugin is also close to be done.. i just need to build a demo anyway before releasing it because i want to avoid solving to much asked questions^^
i am still not sure about steal gold or exp, because enemys might have the gold amount that they will drop anyway if killed ,but they have no exp..
However i can add skillnotes that make skills which let the actor units get gold or exp,,..
aslong nobody asks where these exp or gold are comming from^^

Original thread: https://forums.rpgmakerweb.com/threads/show-sprite-icon-from-weapon-id-database.142284/