-- Ambient Sound -- Author: Jeff the Intern -- Created On: 10/10/2008 -- Reivisions -- [J.McNab] 10/10/2008 - New Script -- CONSTANTS -- Properties Define Properties() ambient_sound_range_trigger = 0 ambient_sound_radius = 1 ExposeProperty('ambient_sound_radius', 'Radius of ambient sound. Users inside the radius will hear the sound being played') PersistProperty('ambient_sound_radius') ambient_sound_active = 1 PersistProperty('ambient_sound_active') ambient_sound_sound_id = '' ExposeProperty('ambient_sound_sound_id', 'What sound do you want to play?', 'soundId') PersistProperty('ambient_sound_sound_id') ambient_sound_volume = 100 ExposeProperty('ambient_sound_volume', 'What is the volume of the ambient sound?', 'int') SetPropRange('ambient_sound_volume', 0, 255) ambient_sound_ids = {} PersistProperty('ambient_sound_ids') -- Play to place support removed b/c of compatibility issues --[[ ambient_sound_place = 0 ExposeProperty('ambient_sound_place', 'Should the sound play to all users in the place?', 'checkbox') PersistProperty('ambient_sound_place') --]] script_description = 'Ambient Sound' script_long_description = 'Play a sound to users near this object' script_icon = '12071:447' end -- Triggers Trigger attach() local parent_template_name = self.type local parent_template_id = '' for index, template_id in pairs(stylesheet.templates._all_) do local template = stylesheet.templates[template_id] if(template.name == parent_template_name) then parent_template_id = template_id break end end local physical = stylesheet.templates[parent_template_id].physical if(physical == 1) then add_range(self) else Debug('Object %s cannot have ambient sound. Please make the template %s be physical', self.name, self.type) self.ambient_sound_active = 0 end end Trigger detach() clear_range(self) end Trigger ambient_sound_enter(object_id) -- Debug('Ambient Enter') ambient_play(self, object_id) end Trigger ambient_sound_exit(object_id) -- Debug('Ambient Exit') ambient_stop(self, object_id) end Trigger param_changed(caller, user_id, script, param) if(script == scriptId) then if(param == 'ambient_sound_radius') then if(self.ambient_sound_range_trigger ~= 0) then clear_range(self) ambient_stop_all(self) add_range(self) end end if(param == 'ambient_sound_place') then if(self.ambient_sound_place == 1) then clear_range(self) ambient_play_all(self) end if(self.ambient_sound_place == 0) then add_range(self) ambient_stop_all(self) end end end end -- Functions function ambient_stop(self, object_id) local object = GetObjectById(object_id) if(object == nil) then return end for find_id, sound_id in pairs(self.ambient_sound_ids) do if(find_id == object_id) then StopSound(object, sound_id) self.ambient_sound_ids[find_id] = 0 break end end end function ambient_play(self, object_id) local object = GetObjectById(object_id) if(object == nil) then return end self.ambient_sound_ids[object_id] = PlaySoundTo(object, self.ambient_sound_sound_id, self.ambient_sound_volume, 1) end function ambient_stop_all(self) for object_id, sound_id in pairs(self.ambient_sound_ids) do if(sound_id ~= 0) then local object = GetObjectById(object_id) if(object ~= nil) then StopSound(object, sound_id) end self.ambient_sound_ids[object_id] = 0 end end end function ambient_play_all(self) local place = GetPlace() for index, user in pairs(place.users) do ambient_play(self, user.id) end end function clear_range(self) if(self.ambient_sound_range_trigger ~= 0) then RemoveRangeTrigger(self, self.ambient_sound_range_trigger) self.ambient_sound_range_trigger = 0 end end function add_range(self) self.ambient_sound_range_trigger = AddRangeTrigger(self, self.ambient_sound_radius, 'ambient_sound_enter', 'ambient_sound_exit') end