Jump to content

Trying to find nearby enemies with script. Suggested code doesn't work?


Recommended Posts

So I have constant loop running that calls the following function every second (like a radar)...

Function Scan10Meters()
   	FoundActors = Game.GetPlayer().FindAllReferencesWithKeyword(ActorTypeNPC, 800.0)
   	Debug.Notification("Actors: " + FoundActors.length + " Distance: " + FoundActors[0].GetPositionX())
EndFunction

I'm still pretty new to papyrus, so maybe I'm just doing an obvious mistake. But FoundActors is consistently empty (returning zero) when it pops up in the notification window. Going near NPCs does nothing to change this fact.

 

Anyone able to point out what I'm doing wrong? Before I do anything with the function, I wanted to test functionality/properties of FoundActors to learn a bit more.

 

This code was grabbed in the forum post here: https://forums.nexusmods.com/index.php?/topic/5440622-findclosestactorfindrandomactor-only-returns-player/?hl=%20detect

 

Would have asked there, but seemed like an oldish post.

Link to comment
Share on other sites

Sure thing. Didn't want to bloat the first post xD Here ya go:

Scriptname MapWidget extends Quest

String Property Map_Widget = "DivisionMap.swf" AutoReadOnly

HUDFramework hud

int widgetLoaded;
int[] aEnemyDirection;

Keyword Property ActorTypeNPC Auto
ObjectReference[] Property FoundActors Auto

Event onQuestInit()
	hud = HUDFramework.GetInstance()
	
	if(hud)
		VerifyEvents()
    		aEnemyDirection = new int[8];
		widgetLoaded = 1;
		While widgetLoaded
			Scan10Meters()
			Utility.Wait(1)
		EndWhile
		
	Else
		Debug.MessageBox("HUDFramework is not installled!")
	EndIf
EndEvent

Function VerifyEvents()
	RegisterForRemoteEvent(Game.GetPlayer(), "OnCombatStateChanged")
EndFunction

Function HUD_WidgetLoaded(string myWidget)
	if (myWidget == Map_Widget)
      		Debug.Trace("Map module updated survivor.")
        	;hud.SendMessage(Map_Widget, HUD_Initialize)
    	EndIf
EndFunction

Event Actor.OnCombatStateChanged(Actor akSender, Actor akTarget, int aeCombatState)
Debug.Notification("Changed combat state.")
EndEvent

Function Scan10Meters()
   	FoundActors = Game.GetPlayer().FindAllReferencesWithKeyword(ActorTypeNPC, 800.0)
   	Debug.Notification("Actors: " + FoundActors[0] + "Distance: " + FoundActors[0].GetPositionX())
endFunction

Nothing really touches the Scan10Meters() function. Everything else is HUDFramework stuff, which doesn't do anything currently (haven't added my .swf file yet).

Link to comment
Share on other sites

Hmm I didnt mean to take so long but my CK is refusing to attach scripts to editor objects right now (any). Maybe something like this will work better?

Scriptname MapWidget extends Quest

HUDFramework HUD

Actor Player
ObjectReference[] Actors

string WidgetID = "DivisionMap.swf" const
string UserLog = " Iwantacookie2010" const

string EmptyState = "" const
string ScanningState = "Scanning" const

int ScanTimer = 0 const
float ScanInterval = 3.0 const
float ScanRadius = 800.0 const


; Events
;---------------------------------------------

Event OnInit()
	Player = Game.GetPlayer()
	Debug.OpenUserLog(UserLog)
EndEvent


Event OnQuestInit()
	HUD = HUDFramework.GetInstance()

	if(HUD)
		StartScan()
	Else
		Log("HUDFramework is not installed!")
		StopScan()
	EndIf
EndEvent


Event OnQuestShutdown()
	StopScan()
EndEvent


Event OnTimer(int aiTimerID)
	{EMPTY}
EndEvent


; Methods
;---------------------------------------------

State Scanning
	Event OnBeginState(string asOldState)
		StartTimer(ScanInterval, ScanTimer)
	EndEvent

	Event OnTimer(int aiTimerID)
		Actors = Player.FindAllReferencesWithKeyword(ActorTypeNPC, ScanRadius)
		If (Actors)
			Log("Scanning found '"+Actors.Length+"' actors.")
		Else
			Log("Scanning could not find any actors.")
		EndIf

		StartTimer(ScanInterval, ScanTimer)
	EndEvent

	Event OnEndState(string asNewState)
		CancelTimer(ScanTimer)
		Actors = none
	EndEvent
EndState


; Functions
;---------------------------------------------

; @HUDFramework
Function HUD_WidgetLoaded(string myWidget)
	If (myWidget == WidgetID)
		Log("Map module updated survivor.")
		;HUD.SendMessage(WidgetID, HUD_Initialize)
	EndIf
EndFunction


Function StartScan()
	If (GetState() != ScanningState)
		GotoState(ScanningState)
	EndIf
EndFunction


Function StopScan()
	If (GetState() != EmptyState)
		GotoState(EmptyState)
	EndIf
EndFunction


Function Log(string aText)
	Debug.TraceUser(UserLog, aText)
EndFunction


; Properties
;---------------------------------------------

Group Properties
	Keyword Property ActorTypeNPC Auto Const Mandatory
EndGroup

Link to comment
Share on other sites

Ah, good ol' pain in the ass Papyrus arrays. I wish they were simple like for-loops.

Your array isn't properly initialized. It's being filled but there's 1 important step missing. The length.

If you want to know if the array is being filled at all do this:

Int iCount = 0
Int Index = FoundActors.Length ; returns the size of the array, including empty slots.
While Index
    Index -= 1 ; Starting at the last index
    if FoundActors[Index] == none
        iCount += 1
        debug.trace("::::::::::::::How many actors not found? " +iCount)
    endif
EndWhile

Check your logs for trace results. In case the array is being filled, a notification might take forever to finish if it picks up a ton of actors.

Edited by Lisselli
Link to comment
Share on other sites

Damn it, I knew I was doing something stupid.


I had a test save that was unmodded, but it apparently autosaved in the middle of my testing sometime ago. Been using a modded save file ever since >.<


What hinted it at first was scrivener07's code. Log showed the game was trying to call Scan10Meters function still.


Fun times.



Also, both the my code and scrivener07's works perfectly fine it seems. Haven't run into any errors for either. Thanks to both of ya!

Link to comment
Share on other sites

  • Recently Browsing   0 members

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