Jump to content

Script that fires when an object is shot


ALEXPOW

Recommended Posts

Hi all. I'm once again attempting to use a script to play a sound, but I'm not sure how to go about it.

I want the sound descriptor to play when my object is shot at using a weapon from the player. This object is placed by me in the Creation Kit, in an area the player cannot reach, and only once in a cell where no combat takes place, so realistically the restrictions should be lax. 

The object is an Activator with the model of a radio I made, which is essentially just a rectangle. If this is more properly done with a Static object (or something else), I can exchange it as needed.

My current attempt to use the OnHit event for this have been fruitless. Either I simply don't understand something, or this event is not what I should be using.

This is my current (pitiful) attempt at the script:

Spoiler

Scriptname RCA_SoundWhenShot:RCA_ObjectSoundWhenShot extends ObjectReference Const

Sound Property RCA_SoundWhenShot Auto Const

Function RegisterForHitEvent(ScriptObject akTarget, ScriptObject akAggressorFilter = None, Form akSourceFilter = None,  Form akProjectileFilter = None, int aiPowerFilter = -1, int aiSneakFilter = -1, int aiBashFilter = -1, int aiBlockFilter = -1, bool abMatch = true) native

Event OnHit(ObjectReference akTarget, ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack,  bool abSneakAttack, bool abBashAttack, bool abHitBlocked, string apMaterial)
    RCA_SoundWhenShot.play(self)
    Debug.Trace(akTarget + " was hit by " + akAggressor) ; For Testing Purposes
EndEvent

 

Link to comment
Share on other sites

Activator should be fine.

You need to pass a variable to a function parameter (e.g. RegisterForHitEvent(akTarget = Self, akAggressorFilter = Game.GetPlayer()) instead of RegisterForHitEvent(ScriptObject akTarget, ScriptObject akAggressorFilter = None)).

; Function RegisterForHitEvent(ScriptObject akTarget, ScriptObject akAggressorFilter = None, Form akSourceFilter = None, Form akProjectileFilter = None,
;	int aiPowerFilter = -1, int aiSneakFilter = -1, int aiBashFilter = -1, int aiBlockFilter = -1, bool abMatch = true) native

; Event OnHit(ObjectReference akTarget, ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack,
; 	bool abBashAttack, bool abHitBlocked, string asMaterialName)
; EndEvent


Scriptname RCA_SoundWhenShot:RCA_ObjectSoundWhenShot extends ObjectReference Const

Sound Property RCA_SoundWhenShot Auto Const


Event OnLoad()		; event is sent when the object reference this script is attached to 3D loads (no need to register for this event)
	RegisterForHitEvent(akTarget = Self, akAggressorFilter = Game.GetPlayer())		; set the function parameters (doing so is "optional" if the parameter already has a default value like "ScriptObject akAggressorFilter = None" instead of "ScriptObject akAggressorFilter")
EndEvent

Event OnHit(ObjectReference akTarget, ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, \
	bool abBashAttack, bool abHitBlocked, string asMaterialName)

	; "Self" (this object reference) was hit by akAggressorFilter which was set to the player

	RCA_SoundWhenShot.play(self)
    	Debug.Trace(akTarget + " was hit by " + akAggressor)

EndEvent

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

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