Placing text on screen for showing damage?

Hey guys,

Ive been wondering if theres a way, that doesnt take TOO much effort, to place text on screen unrelated to the standard message system(with the window and all), that doesnt require me to draw pictures for each letter. The reason for this is mainly, right now, that i want to show some small damage over someones sprite when you hit them.

Thanks in advance!
Window with 0 opacity.

I would create a simple Window function in which there would be a method like this:
Code:
Window_ShowDamage.prototype.displayDmg = function(dmg, x, y) {
   this.contents.clear();
   this.drawText(dmg, 0, 0, this.width);
   this.x = x;
   this.y = y;
   this.show();
}

I would put about 5 windows like these in the scene (in case there are multiple damage windows at the same time), and each time a sprite receives damage, it would call the displayDmg function, gives it the damage and the sprite position as parameters. If you want, you can add a timer after which the window disappears.
@MushroomCake28 hmm, it appears that the Window_ShowDamage class doesnt exist in rpg maker MV, and im still not sure how to create classes. I wonder - since i forgot to mention I use rpg maker MV, did you perhaps use a class that is from another rpg maker?
@MushroomCake28 hmm, it appears that the Window_ShowDamage class doesnt exist in rpg maker MV, and im still not sure how to create classes. I wonder - since i forgot to mention I use rpg maker MV, did you perhaps use a class that is from another rpg maker?

Oh I know, it's just an example (Window ShowDamage doesn't exist lol). In order for what I proposed to work, you need to at least know the basic of programming. I can build the window for you, but you'll need to be able to implement it in your scene.

Code:
function Window_DisplayDmg() {
    this.initialize.apply(this, arguments);
}

Window_DisplayDmg.prototype = Object.create(Window_Base.prototype);
Window_DisplayDmg.prototype.constructor = Window_DisplayDmg;

Window_DisplayDmg.prototype.initialize = function(x, y) {
    this.resetVars();
    Window_Base.prototype.initialize.call(this, x, y, 300, 80);
    this.opacity = 0;
    this.hide();
    this.refresh();
};

Window_DisplayDmg.prototype.refresh = function() {
    this.contents.clear();
};

Window_DisplayDmg.prototype.resetVars = function() {
    this._dTimer   = 200;
    this._dActive = false;
};

Window_DisplayDmg.prototype.addDamage = function(dmg, x, y, frames) {
    this.x = x;
    this.y = y;
    this._dTimer  = frames;
    this._dActive = true;
    this.contents.clear();
    this.drawText(dmg, 0, 0, this.width);
    this.show();
};

Window_DisplayDmg.prototype.update = function() {
    Window_Base.prototype.update.call(this);
    if (this._dActive) {
        if (this._dTimer > 0) {
            this._dTimer -= 1;
        } else {
            this.hide();
            this.resetVars();
        }
    }
};
Check out Poryg's tutorials, he covers doing this with PIXI directly:
https://forums.rpgmakerweb.com/index.php?threads/diy-pixi-text-pt-1-visuals.83682/
(the other links are in his signature)
So basically, you add the window to your scene (by default it will be hidden). Each time someone receives some dmg, you just call the "addDamage" function of the window, pass it the damage and the position.

Or yeah, Aloe Guvner's solution is good too, probably better than mine in your case.
@Aloe Guvner
Thanks for the link - very informative video, and definitely what i was looking for.

@MushroomCake28
I put your script inside a plugin and used ''Window_DisplayDmg.prototype.displayDmg(5, 5, 5)'' to call it from console.log. But i get the error "TypeError: Cannot read property 'bitmap' of undefined". I know the issue might be resolved already, but id still like your solution to work so i can study it and learn from it. Can you tell me what im doing wrong?
@Aloe Guvner
Thanks for the link - very informative video, and definitely what i was looking for.

@MushroomCake28
I put your script inside a plugin and used ''Window_DisplayDmg.prototype.displayDmg(5, 5, 5)'' to call it from console.log. But i get the error "TypeError: Cannot read property 'bitmap' of undefined". I know the issue might be resolved already, but id still like your solution to work so i can study it and learn from it. Can you tell me what im doing wrong?

The Window_displayDmg I create is meant to be used as an object. You can't call it unless you create the object first, and the object is meant to be created in a Scene. So in order to make it work, you'll have to:

1) Create the object in a scene. This can easily be done in any scene, there should be a method named "create". Assuming your scene is the battle scene, it would look like this:

Code:
Scene_Battle.prototype.create = function {
   // Stuff that is already in here.
   this._windowDmg = new Window_DisplayDmg(0, 0); // This is how you create an Object.
   this.addChild(this._windowDmg); // You need to add it to the scene in order for it to be visible;
}

2) Now it is possible to call the "addDamage" method from the window damage we just created in the scene. You'll want to call the method each time someone is damaged. So assuming there's a method in which a battler attacks another:

Code:
Scene_Battle.prototype.hypotheticallAttackMethod = function() {
   // Other stuff, code to do the attack damage
   this._windowDmg.addDamage(damage, x, y, frames); // damage, x, and y would be previously defined. Frames is just the amount of frames you want the window to be visible.
}

That's how I did it in my custom battle system. All text that appear on screen are done this way. It makes it possible to add icons, add movement, etc. It's highly customizable.

@MushroomCake28 hmm, i just cant seem to get it to work. im trying to replicate what youre saying, and after that i can analyze how it works.

I think it would really help, if youre not getting TOO tired of my ignorance yet, if you could tell me how to exactly implement it, preferably on the regular screen(where you walk around and such), or the battlescreen - whatever is easiest.

what I did this time was:
put all your stuff ''as is'' in a plugin.
added your "create" bit to the proper function.
tried to call ''Scene_Battle.prototype.hypotheticallAttackMethod()''
got the error ''typeError: Cannot read property 'addDamage' of undefined''

I know im probably not using it right, but currently i dont even know what im doing and I was hoping to learn it from analysing your code when its working.
i made a engine to show text on screen with @Gamefall Team
he had given me some basic, and then the tool these flourished to a 100% functional tool.
It implement for scene management.
As long as you know js.
https://forums.rpgmakerweb.com/index.php?threads/pixi-text-multi-motions-styles-core-v1-0.86117/

@MushroomCake28 hmm, i just cant seem to get it to work. im trying to replicate what youre saying, and after that i can analyze how it works.

I think it would really help, if youre not getting TOO tired of my ignorance yet, if you could tell me how to exactly implement it, preferably on the regular screen(where you walk around and such), or the battlescreen - whatever is easiest.

what I did this time was:
put all your stuff ''as is'' in a plugin.
added your "create" bit to the proper function.
tried to call ''Scene_Battle.prototype.hypotheticallAttackMethod()''
got the error ''typeError: Cannot read property 'addDamage' of undefined''

I know im probably not using it right, but currently i dont even know what im doing and I was hoping to learn it from analysing your code when its working.

Lol, when I wrote "hypotheticalAttackMethod", it was not meant to be called like that. It's a "hypothetical" method name. I'll go through it each step by step in the Scene Map for you. In this example, it will make the dmg appear on screen near the heroes when pressing the Z/enter key. The damage will be fictional of course since this is just an example.

1) Install the previous code I wrote in a plugin.

2) Locate the "createAllWindows" method in the Scene_Map function.

3) Create the window there:
Code:
Scene_Map.prototype.createAllWindows = function() {
    // Other Stuff
    this._windowDmg = new Window_DisplayDmg(0, 0);
    this.addChild(this._windowDmg);
};

4) Now the window is created, but not visible because there are no damage number to display. Locate the update method in Scene_Map. We'll have to program the "if Z/Enter is pressed, display damage". Of course, once you get how everything works, you'll make the window damage appear when someone is attacked. So, add this to the update method:
Code:
Scene_Map.prototype.update = function() {
    // Other Stuff before
    if (Input.isTriggered('ok')) {
        var dmg = 500    // Fictional Number.
        var x   = ( $gamePlayer.x - $gameMap.displayX() ) * 48; // getting player X position
        var y   = ( $gamePlayer.y - $gameMap.displayY() ) * 48 - 40; // getting player Y position
        var frames = 200 // The amount of frames before the window disappear
        this._windowDmg.addDamage(dmg, x, y, frames);
    }
};

5) Now, when you hit Z or Enter on the map, there should be a 500 appearing above your player for 200 frames before disappearing. If you want to change the text size, or text color, or make the window fade out instead of disappearing, you can modify stuff in the Window DisplayDmg function.

WARNING: Don't leave the code I put here in directly in the rpg_scenes.js file in your main project. When you're done, delete it to not cause problem with other third party plugins.
@MushroomCake28 Excellent, it works! Thanks mate, I appreciate it.

Now i can dig into this stuff and do some learning.

@Jonforum Also, that looks like a very cool tool! only thing is; Im trying to learn and create everything myself as much as possible, that way I dont have to be too relient on other peoples addons. Ill check it out tho, I might learn something from it!

Original thread: https://forums.rpgmakerweb.com/threads/placing-text-on-screen-for-showing-damage.96204/