Jump to content

[Script] OnDistance issues


Recommended Posts

I'm using a script where when a certain NPC comes too near to the player the player will die.

The script is attached externally to the NPC in question.

 

However the player does not die, only the sound and imagespace effect gets applied.

 

This is done by using two different checks for the Distance, so my question is what did I set up wrong?

 

 

 

Scriptname TWItActorScript extends Actor

Sound Property TWHearbeatFastFxCompound Auto
ImageSpaceModifier Property Poison Auto
Int Property SoundInt Auto
Int SoundInstance = 0 
Spell Property TWDamageHealthSpell Auto

Event Onload()
Somefunction()
EndEvent

Function SomeFunction()
  RegisterForDistanceLessThanEvent(Game.GetPlayer(), Self, 900.0)
  RegisterForDistanceGreaterThanEvent(Game.GetPlayer(), Self, 900.1)
RegisterForDistanceLessThanEvent(Game.GetPlayer(), Self, 400.0)

EndFunction

Function Soundy()
If SoundInt == 0
SoundInstance = TWHearbeatFastFxCompound.Play(Self)
Else   
Sound.StopInstance(SoundInstance)                
Endif
EndFunction 



Event OnDistanceLessThan(ObjectReference akObj1, ObjectReference akObj2, float afDistance)
If AfDistance <= 900 
SoundInt = 0
Poison.Apply() ; Just an Imagespacemodifier
Soundy()

Elseif AfDistance <=400
TWDamageHealthSpell.cast(Game.Getplayer()) ; This should kill the player
Game.Getplayer().Kill()
SoundInt = 1
Soundy()
Endif

EndEvent

Event OnDistanceGreaterThan(ObjectReference akObj1, ObjectReference akObj2, float afDistance)
SoundInt = 1
Soundy()
SomeFunction()
EndEvent


 

 

Link to comment
Share on other sites

RegisterForDistanceLessThanEvent registers for a single event, that means you can trigger the event once, then you need to register again after it has been triggered.

Also calling RegisterForDistanceLessThanEvent again will override the previous registration.

So, what happens is that you are registering for Distance Less twice, and the second one is the one you are actually registering for, since 400 is less than 900 it is applying only the effects in the first IF statement.

 

 

 

Scriptname TWItActorScript extends Actor

Sound Property TWHearbeatFastFxCompound Auto
ImageSpaceModifier Property Poison Auto
Int Property SoundInt Auto
Int SoundInstance = 0 
Spell Property TWDamageHealthSpell Auto

Event OnLoad()
	RegisterForDistanceLessThanEvent(Game.GetPlayer(), Self, 900.0) ; Initially register for less than 900
EndEvent

Event OnUnload()
	UnregisterForDistanceEvents(Game.GetPlayer(), Self) ; Unregister for distance events on unload.
EndEvent

Function Soundy()
	If SoundInt == 0
		SoundInstance = TWHearbeatFastFxCompound.Play(Self)
	Else   
		Sound.StopInstance(SoundInstance)                
	Endif
EndFunction 

Event OnDistanceLessThan(ObjectReference akObj1, ObjectReference akObj2, float afDistance)
	If AfDistance < 900 && AfDistance > 400
		SoundInt = 0
		Poison.Apply() ; Just an Imagespacemodifier
		Soundy()
		RegisterForDistanceLessThanEvent(Game.GetPlayer(), Self, 400.0) ; now we are less than 900 (and greater than 400), check for less than 400
		RegisterForDistanceGreaterThanEvent(Game.GetPlayer(), Self, 900.0); also check for greater than 900
	Elseif AfDistance <=400
		TWDamageHealthSpell.cast(Game.Getplayer()) ; This should kill the player
		Game.Getplayer().Kill()
		SoundInt = 1
		Soundy()
		RegisterForDistanceGreaterThanEvent(Game.GetPlayer(), Self, 400.0) ; now we are less than 400, check for greater than 400.
	Endif
EndEvent

Event OnDistanceGreaterThan(ObjectReference akObj1, ObjectReference akObj2, float afDistance)
	If AfDistance > 900
		RegisterForDistanceLessThanEvent(Game.GetPlayer(), Self, 900.0) ; now we are greater than 900, check for less than 900
		SoundInt = 1
		Soundy()
	EndIf
	
	If AfDistance > 400 && AfDistance < 900
		RegisterForDistanceLessThanEvent(Game.GetPlayer(), Self, 400.0) ; now we are greater than 400 (and less than 900), check for less than 400
		RegisterForDistanceGreaterThanEvent(Game.GetPlayer(), Self, 900.0) ; also check for greater than 900
	EndIf
EndEvent

 

 

Edited by DieFeM
Link to comment
Share on other sites

  • Recently Browsing   0 members

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