Jump to content

Scripting: Get reference to nearest actor?


Recommended Posts

Hello everyone. In my Oblivion dungeon I'm trying to make various objects that activate when a creature with a light source comes within a certain distance of them.

 

I want this effect to happen even when it is a non-player character that approaches the object.

 

Essentially I want the following code for traps/doors/spooky haunted things:

begin GameMode
    ref nearestActor = GetNearestActor()
    if GetDistance(nearestActor) && nearestActor.GetActorLightAmount() > 30
        ;Do spooky stuff
    endif
end

However, I can find no means to get a reference to the nearest actor. GetNearestActor() does not exist.

 

Does anyone here know of a way to get a reference to the nearest actor? I'm willing to use Oblivion Script Extender functions.

Link to comment
Share on other sites

Hmm. Interesting. This is just a guess, since I've never tried it before, but maybe something like this?

ref target
ref target2
etc...

begin gamemode

   set target to GetFirstRef 69 1
   set target2 to GetNextRef
   etc...
   if GetDistance(target) && target.GetActorLightAmount() > 30
      ; spooky stuff
   endif

end

That is theory though, so no guarantees. Although I have been trying to figure out if I can a spell to jump from one actor to another, maybe something like this would work? Time to find out...

Link to comment
Share on other sites

Not quite . . . you have to make a loop to check the distance of each and every Actor in the cell

 

something along the line . . .

set target to GetFirstRef 69 1
while target
    if GetDistance(target) < 200  &&  target.GetActorLightAmount() > 30
        ; spooky stuff  to this particular actor which is less than 200 units from the object
    set target to GetNextRef
loop
Link to comment
Share on other sites

a bit different approach

how bout using activators such as pressure plates and such

 

object window

worldobjects

activator

dungeons

caves

triggers

 

when pressure plate is stepped on,

put your 'spooky stuff' to launch in the Begin OnActivate script

for the pressure plate

since you want it to react to an npc or player

just be sure it says

Begin OnActivate

and NOT
Begin OnActivate Player <<<< this will cause the pressure plate to only react to the player and ignore NPCs

Link to comment
Share on other sites

 

Not quite . . . you have to make a loop to check the distance of each and every Actor in the cell

 

something along the line . . .

set target to GetFirstRef 69 1
while target
    if GetDistance(target) < 200  &&  target.GetActorLightAmount() > 30
        ; spooky stuff  to this particular actor which is less than 200 units from the object
    set target to GetNextRef
loop
Thank you for the advice! I'm sorry I didn't respond yesterday. I went to a friend's house shortly after posting.
Your strategy works perfectly, with only one oddity. "GetFirstRef 69 1" and "GetNextRef" do not appear to find the player as an actor. I had to add a player-specific condition.
For anyone interested, here is the working code for a door that opens and closes based on the proximity of creatures with light sources:
ScriptName LohithaLightOpenDarkClose
;A door opens or closes based on the nearness of a creature with a light.

	float triggerDist
	float requiredLight
	ref curActor
	short shouldOpen

Begin GameMode
	
	set curActor to GetFirstRef 69 1
	set triggerDist to 300
	set requiredLight to 1
	set shouldOpen to 0

	if GetDistance PlayerRef < triggerDist && PlayerRef.GetActorLightAmount > requiredLight
		set shouldOpen to 1
	endif
	
	while (curActor && shouldOpen == 0)
		if GetDistance curActor < triggerDist && curActor.GetActorLightAmount > requiredLight
			set shouldOpen to 1
			break
		endif
  		set curActor to GetNextRef
	loop

	if shouldOpen == 1
		SetOpenState 1
	else 
		SetOpenState 0
	endif
End
Edited by Guest
Link to comment
Share on other sites

  • Recently Browsing   0 members

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