Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[EO] Adding Mini HP Bars for Npcs


Kimimaru
 Share

Recommended Posts

Hey, everyone! This is another feature I feel is important for games made with Eclipse Origins. I will explain the code as the tutorial progresses. Let's get started!

**Server**

There is only one thing we need to do in the Server, and that is to send the map npc's hp to everyone on the map when it loses some HP. Luckily, Eclipse Origins already has a Sub designed for doing this; all we need to do is call it.

**modPlayer -> Sub AttackNpc**

Find this:

```
' NPC not dead, just do the damage
        MapNpc(MapNum).Npc(MapNpcNum).Vital(Vitals.HP) = MapNpc(MapNum).Npc(MapNpcNum).Vital(Vitals.HP) - Damage

        ' Check for a weapon and say damage
        SendActionMsg MapNum, "-" & Damage, BrightRed, 1, (MapNpc(MapNum).Npc(MapNpcNum).x * 32), (MapNpc(MapNum).Npc(MapNpcNum).y * 32)
        SendBlood GetPlayerMap(Attacker), MapNpc(MapNum).Npc(MapNpcNum).x, MapNpc(MapNum).Npc(MapNpcNum).y
```
Add this right under that:

```
SendMapNpcVitals GetPlayerMap(Attacker), MapNpcNum
```
That's all for the Server!

**Client**

Now it's time for us to actually draw out the HP bar graphics above the npc. Like most other graphics, we need to call the Sub that draws the HP bar in the main game loop so that it is constantly being rendered. The best place to do this would be Public Sub Render_Graphics.

**modDirectDraw7 -> Public Sub Render_Graphics**

Find this:

```
' Npcs
            For I = 1 To MAX_MAP_NPCS
                If MapNpc(I).Y = Y Then
                    Call BltNpc(I)
                End If
            Next
```
Replace it with this:

```
' Npcs
            For I = 1 To MAX_MAP_NPCS
                ' Draw out npc hp bars
                Call BltNpcHP(I)
                If MapNpc(I).Y = Y Then
                    Call BltNpc(I)
                End If
            Next
```
Now that that's done, we just have to put in the Sub that draws the mini HP bar.

Add this under **Public Sub BltNpc** in **modDirectDraw7**:

```
Public Sub BltNpcHP(ByVal Index As Long)
    Dim X As Long, Y As Long, Sprite As Long, NpcNum As Long

    NpcNum = MapNpc(Index).Num

    If NpcNum = 0 Then
        Exit Sub
    End If

    ' Don't display the HP if the npc is at max hp
    If MapNpc(Index).Vital(Vitals.HP) = Npc(NpcNum).HP Then
        Exit Sub
    End If

    ' Only display HP for npcs that we can attack
    If Npc(NpcNum).Behaviour <> NPC_BEHAVIOUR_ATTACKONSIGHT And Npc(NpcNum).Behaviour <> NPC_BEHAVIOUR_ATTACKWHENATTACKED Then
        Exit Sub
    End If

    Sprite = Npc(NpcNum).Sprite

    ' Calculate the X coordinate to place the name
    X = MapNpc(Index).X * PIC_X + MapNpc(Index).XOffset

    If Sprite < 1 Or Sprite > NumCharacters Then
        Y = MapNpc(Index).Y * PIC_Y + MapNpc(Index).YOffset - 22
    Else
        Y = MapNpc(Index).Y * PIC_Y + MapNpc(Index).YOffset - (DDSD_Character(Sprite).lHeight) + 11
    End If

    ' Draw the outside box
    Call DDS_BackBuffer.SetFillColor(RGB(0, 0, 0))
    Call DDS_BackBuffer.DrawBox(ConvertMapX(X), ConvertMapY(Y), ConvertMapX(X + 32), ConvertMapY(Y + 4))

    ' Draw the HP
    Call DDS_BackBuffer.SetFillColor(RGB(0, 255, 0))
    Call DDS_BackBuffer.DrawBox(ConvertMapX(X), ConvertMapY(Y), ConvertMapX(X + Int(((MapNpc(Index).Vital(Vitals.HP) / 32) / (Npc(NpcNum).HP / 32)) * 32)), ConvertMapY(Y + 4))
End Sub
```
This is the Sub that renders the HP bar above the npc. To change the position of the HP bar, find these lines of code:

```
        Y = MapNpc(Index).Y * PIC_Y + MapNpc(Index).YOffset - 22
        Y = MapNpc(Index).Y * PIC_Y + MapNpc(Index).YOffset - (DDSD_Character(Sprite).lHeight) + 11
```
The above statement is if the npc has no sprite, and the bottom one is if the npc has a sprite. Simply change the numbers at the end (-22 and +11) to change the position of the HP bar. Decreasing the number will result in the HP bar being rendered higher above the npc, and increasing the number will render the HP bar closer to the npc.

Currently, the npc's mini HP bar will not display if its HP is full. To make the mini HP bar display all the time, remove these few lines of code from **Public Sub BltNpcHP**:

```
' Don't display the HP if the npc is at max hp
    If MapNpc(Index).Vital(Vitals.HP) = Npc(NpcNum).HP Then
        Exit Sub
    End If
```
Enjoy!
Link to comment
Share on other sites

  • Replies 81
  • Created
  • Last Reply

Top Posters In This Topic

@bandcampboy:

> I'm pretty sure I did everything you said and It's not working for me. Could you post screenshots of the effects so I can see what it's supposed to look like.

Yeah, I can, but did you make sure you put some of the code in the Server?

Npc at max HP:

![](http://www.freemmorpgmaker.com/files/imagehost/pics/51f870c0360cee47a0ec8b88b8386d9c.png)

Npc with HP taken:

![](http://www.freemmorpgmaker.com/files/imagehost/pics/ee5b9b337a9450c5487959e26300889a.png)
Link to comment
Share on other sites

Hmmm…. I added all of the code and the bars show up right. But when I attack my NPC and he loses HP the bar doesn't go down with his HP. But if I log out  and log back in when he has HP lost, then it shows up.
Link to comment
Share on other sites

For some reason I keep getting this error. I followed your tutorial step by step and it was easy to follow so I don't see how I messed this up. I'll attach a pic of the error I'm getting.

Edit: Never Mind I got it. Thanks for the tutorial.
Link to comment
Share on other sites

  • 2 weeks later...
when i press Ctrl and F5 it sas Compile error: User defined type not defined and it shols  DDS_Player() As DirectDrawSurface7
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×
×
  • Create New...