🔊 PlaySound(filename, volume)

The PlaySound() function allows you to play sound effects at any moment during the game. Sounds are loaded directly from the project’s assets/sounds folder.

⚙️ Syntax

PlaySound(filename, volume = 1.0)

🎧 Basic Example

// Play a sound at full volume
PlaySound("explosion.mp3");

// Play a softer sound
PlaySound("laser.wav", 0.3);

💥 Using it inside events

You can call PlaySound() from any lifecycle function, such as OnCollisionEnter() or OnTriggerEnter().

function OnTriggerEnter(obj, other) {
  if (other.name === "enemy") {
    PlaySound("explosion.mp3", 0.7);
    Destroy(other);
  }
}

🔁 Playing the same sound multiple times

Each call to PlaySound() creates a new audio instance, allowing multiple overlapping sounds without interruptions.

// Fire multiple sounds without cutting the previous ones
PlaySound("shot.wav");
PlaySound("shot.wav");
PlaySound("shot.wav");

⚠️ Important Notes

Advanced Example

// Store the reference and manually stop the sound
const audio = PlaySound("engine_loop.mp3", 0.5);
audio.loop = true;

// Stop after 5 seconds
setTimeout(() => audio.pause(), 5000);
🔸 Summary:
- PlaySound() is ideal for fast effects such as shots, explosions, and UI clicks.
- Sounds are played directly from your project’s assets.
- Supports per-call volume control.
- Returns the native Audio object for advanced control.