Hology
Hology
Hology
  • 👋Welcome to Hology Docs
  • Getting started
    • Introduction to Hology
    • The first steps
    • Editor basics
      • Flying in scenes
      • Placing objects
      • Select objects
      • Transform
      • Grouping objects
      • Duplicate
    • Starter project - Third person shooter
  • Tutorials
    • Rolling ball - Gameplay programming
    • Character movement programming
    • Character AI behavior
  • Release
    • Distribution
      • Discord Activities
      • Facebook Instant Games
      • Upload to Itch.io
      • Host with GitHub Pages
      • Publishing to Steam
      • iOS and Android
  • Assets
    • 3D Models
      • Custom collision shapes
      • Material slots
    • Materials
    • Textures
    • Prefabs
  • Gameplay
    • Actors
      • Creating actor classes
      • Actor parameters
      • Actor components
      • Actor lifecycle
      • Spawning actors
      • Moving actors
    • Services
      • Load assets
    • Player input
    • Collision detection
    • Physics
      • Physics body types
      • Applying forces
      • Ray casting
    • Trigger volumes
    • Character movement
    • Pointer events
    • Animation
      • Animation system
      • Character Animation
      • Animation State Machine
    • Sound
      • Audio parameter
    • World
    • Navigation
  • Shaders
    • Introduction to shaders
    • Creating shaders
    • Shader parameters
    • Typescript shaders
      • Types
      • Math functions
      • Attributes
      • Varying
      • Uniforms
      • Textures
      • Arrays
      • Select
      • Lighting
    • Painted materials
    • Water shader tutorial
  • Level design
    • Landscape sculpting
    • Landscape painting
    • Grass
  • User Interfaces
    • Creating user interfaces
    • Using React
    • VR
  • Visual Effects
    • Introduction to VFX
    • VFX Assets
  • Integrations
    • Arcweave
Powered by GitBook
On this page
  1. Gameplay
  2. Sound

Audio parameter

It is possible to pass in an audio asset to an actor as a parameter in the editor. This makes it possible to easily change the sound that an actor plays from the editor.

The code below illustrates how to define an audio parameter.

  1. First define the parameter. Read more about parameters in Actor parameters

  2. Inject the View controller to access the global audio listener.

  3. Create the instance of a new sound instance to be able to play the audio clip.

  4. Set the sound buffer using the value of the audio parameter in the onInit function. At this point the parameters are set. Read more about actors lifecycle functions in Actor lifecycle

  5. Finally, we define a function that can be called on the actor to play the sound which could be called whenever the sound should play.

import { Actor, BaseActor, ViewController, inject } from "@hology/core/gameplay";
import { Parameter } from "@hology/core/shader/parameter";
import * as THREE from 'three'

@Actor()
class ExampleActor extends BaseActor {
  @Parameter() audio: AudioBuffer;
  private view = inject(ViewController)
  private sound = new THREE.Audio(this.view.audioListener)

  onInit() {
    this.sound.setBuffer(this.audio)
  }
  
  playSound() {
    this.sound.play()
  }

}

export default ExampleActor
PreviousSoundNextWorld

Last updated 12 months ago