Jump to content

Number of NPCs in a cell?


Sticz

Recommended Posts

How can I get a script to run if there is only 1 npc in a cell (or even within a certain distance)? I am trying to get a theft-type of script going and want witnesses to be avoided.

 

Sticz

Link to comment
Share on other sites

What exactly do you want to do? There are some complex ways of counting the number of NPCs in a cell or a certain distance, but it can be quite CPU intensive. Perhaps the easiest way is an invisible area of effect spell and all that it does when it affects an actor is change a certain variable by 1.

Link to comment
Share on other sites

Well, I tried this script:

 

if (Player.GetNumRefs 35 > 1)
Player.ModCrimeGold 500

and it works, but it also counts dead actors as witnesses as well. How can I only count lving/conscious actors?

 

Sticz

Link to comment
Share on other sites

GetHighActors has worked well for me in the past. It returns an array of all actors currently in high level processing - which includes those within the player's cell. Once you have the array, a few If statements in a while loop is all you need to narrow down who counts as a witness.

 

I'll write you a script fragment that you can merge into an existing script...

 

 

Array_var Actors                                    ;Array_Var = Array Variable
Ref TempRef                                          ;The actor being evaluated

Short Key                                             ;The array key currently being used
Short detected                                       ;You'll see what I mean.

Begin <something>

Let Actors := GetHighActors                  ;Returns actors in the cell, and all other actors with High-level AI processing.
Let Key := ar_Size Actors                     ;Returns how many variables are in the array.

While Key > 0                             ;"While Loops" will repeat so long as the expression is true.

     Let Key -= 1
                   
     Let TempRef := Actors[Key]

     If TempRef.GetDetected Player == 0
          Continue                 ;Continue is like a return, but for a while loop.
     ElseIf
          ;;;Any other checks, like maybe the witness is in the Thieves' Guild

          ;;;If player passes all checks
               Continue
          ;;;If Player fails a check
               Set Detected to 1
               Break      ;Ends the loop immediately, no point in checking anyone else.
          endif

     endif

Loop                                                                

Let Actors := ar_Null      ;Clean up the array, to avoid keeping unneeded data


If Detected == 1
     ;;;Do stuff
Else
     ;;;Do other stuff
endif

End

 

 

 

 

I strongly recommend that you avoid using this method in a gamemode block. While loops can be fairly CPU intensive.

Link to comment
Share on other sites

You can put it in a GameMode sequence if you have to, but make sure that the while loop does not run too often. A well placed Return call should do the trick, but it depends on how your other scripts work. Essentially, just use it whenever you need to find out if there are any witnesses nearby.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...