Wednesday, May 21, 2014

UDK : Creating a sound you can find and turn off via unrealscript

The player in my game collects QR cubes. One way I help the player find them is by playing an audio loop when they get near one. Once the cube is collected, the sound goes away.

There was no way I wanted to do that in kismet. That would take a long long time.

After a bit of digging through unrealscript actors I found just what I needed:

AmbientSoundSimpleToggleable

I drag the actor onto the map and place it where the cube is. I then tag it so I can find it:
QRSOUND000 : the 000 = number of the cube.

Then in my code for updating the map once a cube is collected I do this:
    // enable/disable sound for qr cubes : ex : QRSOUND016
    foreach AllActors(class'AmbientSoundSimpleToggleable', sound) {
        if (Len(sound.Tag) == 10 && Left(sound.Tag,7) == "QRSOUND") {
            qrCode = int(Mid(sound.Tag,7,3));
            if (gameState.hasQRCode(qrCode)) {
                sound.StopPlaying();
            }
            else {
                sound.StartPlaying();
            }
        }
    }

Voila! cube sound modified on the fly with no kismet.

The best thing is you can adjust the min/max radius and volume of the sound in the editor. Best of both worlds.

No comments:

Post a Comment