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

NPC Number (Solved)


Zamin
 Share

Recommended Posts

Hello,

Is there a way to get the NPC slot number? The slot number of where each NPC is saved? The one shoen in menu 'F1 > Edit NPCs'.

I asked a few people in shout box and they were very confused.

I tried 'NPCnum', and 'GetMapNPCNumber(MapNum ,Index)' but it doesn't give me the right number. 'NPCnum' always gives me '0', and 'GetMapNPCNumber(MapNum ,Index)' always gives my '1'.

I basically need a mechanism or a sub/function which can distinguish each NPC from the other. It shouldn't matter whether the NPC is attack-able or is friendly/scripted. Because the 'OnAttack' sub doesn't run when the NPC is friendly/scripted.

I hope I'm not too confusing. Please help me in this little problem.

-Thanks in advance.
Link to comment
Share on other sites

OK. The NPC I am testing is in slot number 2 in the menu 'F1 > Edit NPCs'. It is a scripted NPC in case 2\. It is running my script. So at the top of Case 2 I added this:
```
If NPCnum = 2 Then
    Call PlayerMsg(Index, "asdasda", GREEN)
End If

```
When I hit 'ctrl' it doesn't display the message, even though it should. But when I change the number from '2' to '0', it display's the message. I dunno if I'm still doing something wrong. :(

-Thanks
Link to comment
Share on other sites

@[THE-KRIS:

> link=topic=51395.msg539206#msg539206 date=1252101498]
> thats because npcnum isnt defined…

if you mean
```
Dim NPCnum
```Then it's already on top of case. Still the same problem.

Should'nt NPCnum be connected to the source somehow. I mean, normall when a sub/function is used. The info gets passed to the source wich then returns you a value.

-Thanks
Link to comment
Share on other sites

@Zamin:

> OK. The NPC I am testing is in slot number 2 in the menu 'F1 > Edit NPCs'. It is a scripted NPC in case 2\. It is running my script. So at the top of Case 2 I added this:
> ```
> If NPCnum = 2 Then
>     Call PlayerMsg(Index, "asdasda", GREEN)
> End If
>
> ```
> When I hit 'ctrl' it doesn't display the message, even though it should. But when I change the number from '2' to '0', it display's the message. I dunno if I'm still doing something wrong. :(
>
> -Thanks

Try GetPlayerTargetNPC, if you're attacking. I think OnAttack, ScriptedNPC and such set the target when you attack.

```
If GetPlayerTargetNPC(Index) < 1 Then
Exit Sub
End If

If GetPlayerTargetNpc(Index) = 2 Then
Call PlayerMsg(Index, "You are attacking NPC 2.", BRIGHTGREEN)
Else
Call PlayerMsg(Index, "You aren't attacking NPC 2.", BRIGHTRED)
End If
```

Regards,
  Godlord.
Link to comment
Share on other sites

OK, I put that code in OnAttack sub. I also placed this under your code to check what I get:
```
Call PlayerMsg(Index, "NPC Number: " & GetPlayerTargetNpc(Index), BRIGHTRED)
```
My NPc which is in slot 1 is atackable on Map 1, 2nd NPC is scripted on Map 1 and the 3rd NPC is also attackable but on Map 2.

I placed your code and the line mentioned in Sub OnAttack. When I attack the attack-able NPCs, the one in slots 1 and 3, I always get the following message:
```
Your aren't attacking NPC 2.
NPC Number: 1

```
When I placed the above code in the sub ScriptedNPC in case 2, the case that my scripted NPC is using. This NPC is in slot #2\. The script doesn't run, in fact, because of the 'GetPlayerTargetNpc(Index)' script, my scripted NPC doesn't run its usual script.

I hope I wasn't too confusing. Is there no way to get the NPc number right from where it is stored in source?

> you cant just dim it, you have to give it a variable.

What do you mean give it a variable? Like
```
Dim x
x = NPCnum

```Sorry, I'm just not understanding. :(

-Thanks
Link to comment
Share on other sites

@[THE-KRIS:

> link=topic=51395.msg539274#msg539274 date=1252105437]
> well npcnum doesnt exist unless you give it a value propperly
>
> dim npcnum
>
> npcnum = GetPlayerTargetNpc(index)

Ok, so I did that and placed the following in the ScriptedNPC case 2, to check for my npc.
```
If NPCnum = 2 Then
    Call PlayerMsg(Index, "asdasda", GREEN)
End If
Call PlayerMsg(index, "NPC Number 1: " & NPCnum, RED)

```
I get 'NPC Number 1: -1'
So I NPCnum is '-1' when I hit 'ctrl' to the scripted npc. At least this isn't '0'. lol
Any other ideas, lol.

-Thanks.
Link to comment
Share on other sites

@Robin:

> MapNpcNum is used in a few subroutines, but it's **called**.
>
> You can't "find" the slot number, you need to know it.

Ok so correct me if i'm wrong
```
GetMapNPCnumber(1,Index)
```In this case, 1 is the map number. And this will simply return the npc number, which is unique in this entire map?

This is so confusing. :(

-Thanks
Link to comment
Share on other sites

The only way to find what NPC you're attacking is via Godlord's method: GetPlayerTargetNPC. You can't do this with scripted NPCs. You'll have to add that to the source. It's just a matter of adding another parameter to the MyScript execution of ScriptedNPCs.

@Zamin:

> Ok so correct me if i'm wrong
> ```
> GetMapNPCnumber(1,Index)
> ```In this case, 1 is the map number. And this will simply return the npc number, which is unique in this entire map?
>
> This is so confusing. :(
>
> -Thanks

Index is likely your player index. You can't use that. You have to know the slot that the NPC is in to use that command, like Robin said. The "index" that command references is the NPC index.
Link to comment
Share on other sites

@Balliztik1:

> The only way to find what NPC you're attacking is via Godlord's method: GetPlayerTargetNPC. You can't do this via scripting. You'll have to add that to the source. It's just a matter of adding another parameter to the MyScript execution of ScriptedNPCs.
>
> Index is likely your player index. You can't use that. You have to know the slot that the NPC is in to use that command, like Robin said. The "index" that command references is the NPC index.

Sorry, this is the slot number in the menu 'F1 > Edit NPCs' or the meny 'F1 > Edit Map >  Properties > NPC'. Just wanna be sure.

-Thanks
Link to comment
Share on other sites

Omg people.. [http://www.touchofdeathforums.com/smf/index.php/topic,28153.0.html](http://www.touchofdeathforums.com/smf/index.php/topic,28153.0.html)

```
NPCNUM = GetMapNpcNumber(GetPlayerMap(Index), GetPlayerTargetNPC(Index))
Call PlayerMsg(Index, "Npcnum is " & NPCNUM, 15)

```
Link to comment
Share on other sites

@Bobosk:

> Omg people.. [http://www.touchofdeathforums.com/smf/index.php/topic,28153.0.html](http://www.touchofdeathforums.com/smf/index.php/topic,28153.0.html)
>
> ```
> NPCNUM = GetMapNpcNumber(GetPlayerMap(Index), GetPlayerTargetNPC(Index))
> Call PlayerMsg(Index, "Npcnum is " & NPCNUM, 15)
>
> ```

OK, I tried this. It works perfectly on NPC that you can attack. I did a few trial runs. It gives me the targetNPC as 1 for all attack-able NPCs. But I need it to work for a scripted NPC. And the targetNPC for a scripted NPC is 0.

So when I use this command for the scripted NPC, it becomes this:
GetMapNpcNumber(GetPlayerMap(Index), 0) because the targetNPC is 0, right?

This is the problem, it doesn't read in 0\. As soon as 0 is entered in the GetMapNPCNumber Function, the script doesn't run. Is there a way for it to work for a scripted NPC? Thanks.

@Balliztik1:

> Yes, that index. But if you already know it, then you aren't trying to find it, which gets you no where.

I am checking it using a .ini file. User put the NPC number by looking at the Edit NPCs menu, then I check whether the NPC the user is targeting is the same or not. If so then….

-Thanks.
Link to comment
Share on other sites

@Kimimaru:

> Are you sure you're doing this part correctly?

This is what I will do after I do know how to get the NPC number of a scripted NPC. And Ya, i think i know how to do it. But first things first, I need to read the targeted NPC's number.

-Thanks
Link to comment
Share on other sites

There's a tidbit in the Server-side source that doesn't make scriptedNPC's targets.. which can easily be fixed a few ways… but i think the easiest would be.
```
' Try to attack an NPC.
    For I = 1 To MAX_MAP_NPCS
        If CanAttackNpc(Index, I) Then
            ' Get the damage we can do
            Player(Index).TargetNPC = I
            Player(Index).TargetType = TARGET_TYPE_NPC
            If Not CanPlayerCriticalHit(Index) Then
                Damage = GetPlayerDamage(Index) - Int(NPC(MapNPC(GetPlayerMap(Index), I).num).DEF / 2)
                Call SendDataToMap(GetPlayerMap(Index), "sound" & SEP_CHAR & "attack" & END_CHAR)
            Else
                Damage = GetPlayerDamage(Index) + Int(Rnd * Int(GetPlayerDamage(Index) / 2)) + 1 - Int(NPC(MapNPC(GetPlayerMap(Index), I).num).DEF / 2)
                Call BattleMsg(Index, "You feel a surge of energy upon swinging!", BRIGHTCYAN, 0)
                Call SendDataToMap(GetPlayerMap(Index), "sound" & SEP_CHAR & "critical" & END_CHAR)
            End If
```Find that Server-Side in the ModHandleData and simply put```
            Player(Index).TargetNPC = I
            Player(Index).TargetType = TARGET_TYPE_NPC
```above the "If CanAttackNpc(Index, I)"
Link to comment
Share on other sites

I'd suggest against that. The way the targetting system works, if you did that, when you don't hit an NPC, you are still setting the player's target to the last NPC, instead of leaving it as is. If a player misses, then tries to cast a spell, for instance, unless they were trying to attack NPC 15, their target will be messed up. Furthermore, because of the way the server works, if it's a damaging spell, you'll get messages that you killed a non-existant NPC and that you received EXP for it. Sure, that's just an annoying message, but then the OnNPCDeath script triggers, which, without a substantial NPC to actually use, crashes the server.

Setting the target to something that does not exist is not the way to go. The way to do this without interfering with the targeting system is to simply alter the Scripted NPC execution from the source code, like so:

Find all instances of this line of code:
```
MyScript.ExecuteStatement "Scripts\Main.txt", "ScriptedNPC " & Attacker & "," & NPC(NPCnum).SpawnSecs
```
Replace with this:
```
MyScript.ExecuteStatement "Scripts\Main.txt", "ScriptedNPC " & Attacker & "," & NPC(NPCnum).SpawnSecs & "," & MapNpcNum
```
Currently, you'll find 8 of those in the source. Change each of them.

Then, change your ScriptedNPC sub to this:
```
Sub ScriptedNPC(index, script, npcSlot)
```
And add this line to it:
```
NpcNum = GetMapNPCNumber(GetPlayerMap(index), npcSlot)
```

This way, you'll still get more specific NPC execution without altering the targeting system.
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...