Random sounds

Help bring our custom Ages to life! Share tips and tricks, as well as code samples with other developers.

Random sounds

Postby Whilyam » Sun Aug 24, 2008 7:08 am

Does anyone have/know the script for having an emitter object randomly activate? I've got some birds in one of my ages I'd like to squawk randomly as they go along their path. Also to that end, how would I get an emitter object to stay with the animal's object?
User avatar
Whilyam
 
Posts: 1023
Joined: Sat Sep 29, 2007 5:55 pm

Re: Random sounds

Postby D'Lanor » Sun Aug 24, 2008 7:46 am

This is what I use for the creepy creaking sounds in Prad.

AlcScript
Code: Select all
<emitter object>:
    type: soundemit
    sound:
        flags:
          - is3dsound
          - localonly
        file: <sound file>
        volume: 0.4
        type: ambience
        minfdist: 6
        maxfdist: 60
    logic:
        modifiers:
          - tag: RandResp
            actions:
              - type: pythonfile
                ref: $Pyth_Random
        actions:
          - type: pythonfile
            tag: Pyth_Random
            pythonfile:
                file: <python file>
                parameters:
                  - type: responder
                    ref: $Resp_Random
                  - type: string
                    value: <emitter object>
                  - type: int
                    value: <timer ID>
                  - type: float
                    value: <timer interval>
                  - type: int
                    value: <chance value>
                  - type: sceneobject
                    ref: scnobj:<emitter object>
          - type: responder
            tag: Resp_Random
            responder:
                states:
                  - cmds:
                      - type: soundmsg
                        params:
                            receivers:
                              - 0011:<emitter object>
                            cmds:
                              - play
                              - setvolume
                            volume: 0.4
                        waiton: -1
                    nextstate: 0
                    waittocmd: 0
                curstate: 0
                flags:
                  - detecttrigger


Python script
Code: Select all
from Plasma import *
from PlasmaTypes import *
import xRandom
respSound = ptAttribResponder(1, 'Sound responder')
strObj = ptAttribString(2, 'Object String')
intID = ptAttribInt(3, 'Timer ID')
floatTime = ptAttribFloat(4, 'Timer interval')
intChance = ptAttribInt(5, 'Chance value')
testObj = ptAttribSceneobject(6, 'Owner test object')
class <python file>(ptModifier,):

    def __init__(self):
        ptModifier.__init__(self)
        self.version = '1'
        print ('__init__%s v.%s' % (self.__class__.__name__,
         self.version))



    def OnFirstUpdate(self):
        print ('%s: OnFirstUpdate: Getting local avatar' % self.__class__.__name__)
        self.LocalAvatar = PtGetLocalAvatar()



    def OnServerInitComplete(self):
        print ('%s: OnServerInitComplete: Starting timer %d with interval %d for %s' % (self.__class__.__name__,
         intID.value, floatTime.value, strObj.value))
        PtAtTimeCallback(self.key, floatTime.value, intID.value)



    def BeginAgeUnLoad(self, avatar):
        if (avatar == self.LocalAvatar):
            print ('%s: BeginAgeUnLoad: Leave your timer at the door' % self.__class__.__name__)
            PtClearTimerCallbacks(self.key)



    def OnTimer(self, id):
        print ('%s: OnTimer: id = %d' % (self.__class__.__name__, id))
        if (id == intID.value):
            cur_chance = xRandom.randint(0, 100)
            print (' - Chance value = %d, Local chance roll = %d' % (intChance.value, cur_chance))
            if (cur_chance <= intChance.value):
                # Responders are netpropagated by default which means that every player will hear the sound, even if it is localonly!
                # We want the same sounds to be heard by all but letting all players run them will mess up the chance rolls.
                # So only the game owner must run the responder!
                if (type(testObj) != type(None)):
                    if testObj.sceneobject.isLocallyOwned():
                        print (' - I am game owner. Running sound from %s' % (strObj.value))
                        respSound.run(self.key)
                else:
                    print (' - Unknown game owner, running sound locally from %s' % (strObj.value))
                    respSound.run(self.key, netPropagate=0)
            PtAtTimeCallback(self.key, floatTime.value, intID.value)



#insert glue section here


This is fully multiplayer proof. All age players will hear the same sounds.

<timer ID>, <timer interval> and <chance value> are set by the AlcScript so that you can give each sound emitter a different randomizer and still use the same Python file for all.

And to move the emitter with the object you simply parent it to the object (must be a 3D sound).
"It is in self-limitation that a master first shows himself." - Goethe
User avatar
D'Lanor
 
Posts: 1980
Joined: Sat Sep 29, 2007 4:24 am

Re: Random sounds

Postby Paradox » Sun Aug 24, 2008 12:12 pm

Is it really necessary to use a Python file to play a sound?

An easier way would be to use a responder with netforcing, and send a plSoundMsg to play/stop/etc. the sound.
Paradox
 
Posts: 1290
Joined: Fri Sep 28, 2007 6:48 pm
Location: Canada

Re: Random sounds

Postby D'Lanor » Sun Aug 24, 2008 2:21 pm

I would not know how to randomly play a sound without Python. This is what Cyan uses for the random bahro screams. Or rather... an improved version of it. :D
"It is in self-limitation that a master first shows himself." - Goethe
User avatar
D'Lanor
 
Posts: 1980
Joined: Sat Sep 29, 2007 4:24 am

Re: Random sounds

Postby Whilyam » Sun Aug 24, 2008 4:31 pm

Paradox wrote:Is it really necessary to use a Python file to play a sound?

An easier way would be to use a responder with netforcing, and send a plSoundMsg to play/stop/etc. the sound.

I have no real issue with the python (as I'll already be using python for journals and such) but I'm interested in what you mean. What is netforcing? I know I could always tie the sound to a region, but I felt that opened it up to people running in and out to make the poor birds have a fit :lol:
User avatar
Whilyam
 
Posts: 1023
Joined: Sat Sep 29, 2007 5:55 pm

Re: Random sounds

Postby D'Lanor » Mon Aug 25, 2008 12:22 am

Netforcing means that you send the actions of the local player/client to other players. However, there is also netpropagating which does about the same. And since responders are already netpropagated by default you normally do not have to netforce them.

The Python code I showed has several multiplayer fine tunings which AFAIK cannot be achieved any other way.
"It is in self-limitation that a master first shows himself." - Goethe
User avatar
D'Lanor
 
Posts: 1980
Joined: Sat Sep 29, 2007 4:24 am

Re: Random sounds

Postby Lontahv » Thu Aug 28, 2008 3:56 am

Actually, for some of the Bahro screams they use (most of the ones I've seen) also use RandomSoundMods (I think I have that name right).

This has a list of sounds that a random sound gets picked a played every time it's triggered. This is what kickable sounds use... if you can find a way to randomly call this sound (like in D'Lanor's Python) then you'll get a random sound every time your Python (or whatever you're using) chooses to call it.
Currently getting some ink on my hands over at the Guild Of Ink-Makers (PyPRP2).
User avatar
Lontahv
Councilor of Artistic Direction
 
Posts: 1331
Joined: Wed Oct 03, 2007 2:09 pm


Return to Scripting

Who is online

Users browsing this forum: No registered users and 1 guest

cron