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

[EO] Displaying NPC Names


Kimimaru
 Share

Recommended Posts

Hello, everyone! We all know that Eclipse Origins is a great engine, but we also know that it has some missing features that we'd all like in our games. Displaying npc names on the maps is a feature that I feel is necessary in any game, but unfortunately Eclipse Origins doesn't have it. By the end of this tutorial, you will have this necessity in your game. Let's get started!

Everything takes place in the Client source:

**modDirectDraw7 -> Sub Render_Graphics**

Like player names, we need the npc names to be rendered constantly, so putting the command to draw the names in Sub Render_Graphics, which is in the main game loop, is the ideal place to put the code.

Near the bottom of Sub Render_Graphics, find this:

```
' draw player names
    For I = 1 To MAX_PLAYERS
        If IsPlaying(I) And GetPlayerMap(I) = GetPlayerMap(MyIndex) Then
            Call DrawPlayerName(I)
        End If
    Next
```
Directly under that, add this:

```
' draw npc names
    For I = 1 To MAX_MAP_NPCS
        Call DrawNPCName(I)
    Next I
```
Now head over to **modText**, and find **Public Sub DrawPlayerName**. Right under that, add this:

```
Public Sub DrawNPCName(ByVal Index As Long)
    Dim NpcName As String
    Dim TextX As Long, TextY As Long, Sprite As Long, NpcNum As Long

    NpcNum = MapNpc(Index).Num

    If NpcNum = 0 Then
        Exit Sub
    End If

    Sprite = Npc(NpcNum).Sprite
    NpcName = Trim$(Npc(NpcNum).Name)

    ' Calculate the X coordinate to place the name
    TextX = ConvertMapX(MapNpc(Index).X * PIC_X) + MapNpc(Index).XOffset + (PIC_X \ 2) - getWidth(TexthDC, NpcName)

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

    ' Draw name
    Call DrawText(TexthDC, TextX, TextY, NpcName, QBColor(White))
End Sub
```
This is just the Sub that actually does the calculations for drawing out the NPC name.

That's all! To change the color of the npc name, look at this line in **Public Sub DrawNpcName**:

```
Call DrawText(TexthDC, TextX, TextY, NpcName, QBColor(White))
```
Change the **White** variable with the variable of another color. This works for npcs with dynamic sprite sizes as well as npcs with no sprites.

Enjoy!
Link to comment
Share on other sites

@Kimimaru:

> although I'm sure it's a planned feature.

It's not. I added it in before, but I felt it didn't suit the retro feel I go for.

It's an okay tutorial. I take it you just took the code from DrawPlayerName?
Link to comment
Share on other sites

@Robin:

> @Kimimaru:
>
> > although I'm sure it's a planned feature.
>
> It's not. I added it in before, but I felt it didn't suit the retro feel I go for.
>
> It's an okay tutorial. I take it you just took the code from DrawPlayerName?

Oh, okay. Well, if people want it, at least they can have it now. I took and edited some code from both DrawPlayerName and BltNpc.
Link to comment
Share on other sites

@Grendalin:

> Is there a way to make it so that friendly NPCs have visible names and Enemies don't? If so good tutorial.

Yes, and it's pretty simple to do. Find this line in Public Sub DrawNpcName:

```
Sprite = Npc(NpcNum).Sprite
```
Above that, add this:

```
If Npc(NpcNum).Behaviour <> NPC_BEHAVIOUR_FRIENDLY Then
    Exit Sub
End If
```
Link to comment
Share on other sites

Wohoo thanks, great job.
I really needed this. :)

Though I want to have Atack on sight to be Red.
And Shopkeeper, Banker, Friendly, to be White.
And Atack when attacked to be Orange. (Or maybe a other color.)

I tried this:

```
Public Sub DrawNPCName(ByVal Index As Long)
    Dim NpcName As String
    Dim TextX As Long, TextY As Long, Sprite As Long, NpcNum As Long

    NpcNum = MapNpc(Index).Num

    If NpcNum = 0 Then
        Exit Sub
    End If

    If Npc(NpcNum).Behaviour <> NPC_BEHAVIOUR_FRIENDLY Then
    Call DrawText(TexthDC, TextX, TextY, NpcName, QBColor(White))
        Exit Sub
    Else
    End If
    If Npc(NpcNum).Behaviour <> NPC_BEHAVIOUR_SHOPKEEPER Then
    Call DrawText(TexthDC, TextX, TextY, NpcName, QBColor(White))
        Exit Sub
    End If

    Sprite = Npc(NpcNum).Sprite
    NpcName = Trim$(Npc(NpcNum).Name)

    ' Calculate the X coordinate to place the name
    TextX = ConvertMapX(MapNpc(Index).X * PIC_X) + MapNpc(Index).XOffset + (PIC_X \ 2) - getWidth(TexthDC, NpcName)

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

    ' Draw name
    Call DrawText(TexthDC, TextX, TextY, NpcName, QBColor(Red))
End Sub
```
As I don't know the command for NPC's like "NPC_BEHAVIOUR_FRIENDLY" I tried guessing it.
No compile error. But it doesn't work at all anymore.

When I just Kimimaru's script without Shopkeeper, only Friendly have Red. And other behavior NPC's nothing.

I would really apprentice all help.
Thanks in advance!
Link to comment
Share on other sites

Well you have the code to display text, before all of the displaying text code is even handled, then u have it exiting the script before running it all…

Try this
```
Public Sub DrawNPCName(ByVal Index As Long)
    Dim NpcName As String
    Dim TextX As Long, TextY As Long, Sprite As Long, NpcNum As Long

    NpcNum = MapNpc(Index).Num

    If NpcNum = 0 Then
        Exit Sub
    End If

    Sprite = Npc(NpcNum).Sprite
    NpcName = Trim$(Npc(NpcNum).Name)

    ' Calculate the X coordinate to place the name
    TextX = ConvertMapX(MapNpc(Index).X * PIC_X) + MapNpc(Index).XOffset + (PIC_X \ 2) - getWidth(TexthDC, NpcName)

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

    ' Draw name   
        If Npc(NpcNum).Behaviour = NPC_BEHAVIOUR_ATTACKONSIGHT Then
    Call DrawText(TexthDC, TextX, TextY, NpcName, QBColor(Red))
        End If
    If Npc(NpcNum).Behaviour = NPC_BEHAVIOUR_ATTACKWHENATTACKED Then
        Call DrawText(TexthDC, TextX, TextY, NpcName, QBColor(Orange))
    End If
        If Npc(NpcNum).Behaviour <> NPC_BEHAVIOUR_ATTACKONSIGHT And Npc(NpcNum).Behaviour <> NPC_BEHAVIOUR_ATTACKWHENATTACKED Then
    Call DrawText(TexthDC, TextX, TextY, NpcName, QBColor(White))
        End If
End Sub
```
Link to comment
Share on other sites

wouldn't it be better to do like how drawplayernames are in a select case.

something like…

```
    ' Check npc behaviour
    If NpcNum > 0 Then

        Select Case Npc(NpcNum).Behaviour
            Case 0 ' NPC_BEHAVIOUR_ATTACKONSIGHT
                color = RGB(255, 96, 0)
            Case 1 ' NPC_BEHAVIOUR_ATTACKWHENATTACKED
                color = QBColor(DarkGrey)
            Case 2 ' NPC_BEHAVIOUR_FRIENDLY
                color = QBColor(Cyan)
            Case 3 ' NPC_BEHAVIOUR_SHOPKEEPER
                color = QBColor(BrightGreen)
            Case 4 ' NPC_BEHAVIOUR_GUARD
                color = QBColor(Yellow)
        End Select
    Else
        color = QBColor(BrightRed)
    End If

```
Link to comment
Share on other sites

@Ertzel:

> Well you have the code to display text, before all of the displaying text code is even handled, then u have it exiting the script before running it all…
>
> Try this
> -snip-

Hmm.. I see.
Thanks a ton Ertzel link! It works flawless now. :)
(Though, EO doesn't accept 'Orange' so I changed it to Blue, for the time being)

@boneyou
Your script doesn't work here. It gives compile errors.
But indeed it is a nice idea.
Link to comment
Share on other sites

here's my whole sub. the error you were getting was probably the color wasn't dimmed

```
Public Sub DrawNPCName(ByVal Index As Long)
    Dim NpcName As String
    Dim TextX As Long, TextY As Long, Sprite As Long, NpcNum As Long
    Dim color As Long

    NpcNum = MapNpc(Index).Num

    If NpcNum = 0 Then
        Exit Sub
    End If

    Sprite = Npc(NpcNum).Sprite
    NpcName = Trim$(Npc(NpcNum).Name)

    ' Calculate the X coordinate to place the name
    TextX = ConvertMapX(MapNpc(Index).X * PIC_X) + MapNpc(Index).XOffset + (PIC_X \ 2) - getWidth(TexthDC, NpcName)

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

    ' Check npc behaviour
    If NpcNum > 0 Then

        Select Case Npc(NpcNum).Behaviour
            Case 0 ' NPC_BEHAVIOUR_ATTACKONSIGHT
                color = RGB(255, 96, 0)
            Case 1 ' NPC_BEHAVIOUR_ATTACKWHENATTACKED
                color = QBColor(DarkGrey)
            Case 2 ' NPC_BEHAVIOUR_FRIENDLY
                color = QBColor(Cyan)
            Case 3 ' NPC_BEHAVIOUR_SHOPKEEPER
                color = QBColor(BrightGreen)
            Case 4 ' NPC_BEHAVIOUR_GUARD
                color = QBColor(Yellow)
        End Select
    Else
        color = QBColor(BrightRed)
    End If

    ' Draw name
    Call DrawText(TexthDC, TextX, TextY, NpcName, color)
End Sub
```
Link to comment
Share on other sites

@boneyou:

> here's my whole sub. the error you were getting was probably the color wasn't dimmed
>
> ```
> Public Sub DrawNPCName(ByVal Index As Long)
>     Dim NpcName As String
>     Dim TextX As Long, TextY As Long, Sprite As Long, NpcNum As Long
>     Dim color As Long
>  
>     NpcNum = MapNpc(Index).Num
>  
>     If NpcNum = 0 Then
>         Exit Sub
>     End If
>  
>     Sprite = Npc(NpcNum).Sprite
>     NpcName = Trim$(Npc(NpcNum).Name)
>  
>     ' Calculate the X coordinate to place the name
>     TextX = ConvertMapX(MapNpc(Index).X * PIC_X) + MapNpc(Index).XOffset + (PIC_X \ 2) - getWidth(TexthDC, NpcName)
>  
>     If Sprite < 1 Or Sprite > NumCharacters Then
>         TextY = ConvertMapY(MapNpc(Index).Y * PIC_Y) + MapNpc(Index).YOffset - 17
>     Else
>         TextY = ConvertMapY(MapNpc(Index).Y * PIC_Y) + MapNpc(Index).YOffset - (DDSD_Character(Sprite).lHeight) + 16
>     End If
>    
>     ' Check npc behaviour
>     If NpcNum > 0 Then
>
>         Select Case Npc(NpcNum).Behaviour
>             Case 0 ' NPC_BEHAVIOUR_ATTACKONSIGHT
>                 color = RGB(255, 96, 0)
>             Case 1 ' NPC_BEHAVIOUR_ATTACKWHENATTACKED
>                 color = QBColor(DarkGrey)
>             Case 2 ' NPC_BEHAVIOUR_FRIENDLY
>                 color = QBColor(Cyan)
>             Case 3 ' NPC_BEHAVIOUR_SHOPKEEPER
>                 color = QBColor(BrightGreen)
>             Case 4 ' NPC_BEHAVIOUR_GUARD
>                 color = QBColor(Yellow)
>         End Select
>     Else
>         color = QBColor(BrightRed)
>     End If
>  
>     ' Draw name
>     Call DrawText(TexthDC, TextX, TextY, NpcName, color)
> End Sub
> ```

Neat. :)
Thanks for this too, I'm using this at the moment. Keep it up!

RGB(255,128,0 =/= Orange (yay)
Link to comment
Share on other sites

  • 5 months later...

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...