In Unrealscript how do I have a config value for a resource in a class set a property of a component?

1.3k views Asked by At

I have a sound cue that I want played whenever the player does a specific action. I've got it playing fine and everything but I want to make the resource that is used come from a configuration file instead of being hard coded.

So I added a property to my class called MonsterNoiseSoundCue as follows:

var config SoundCue MonsterNoiseSoundCue;

Then in the DefaultProperties section I added the following to the object I create which then then added to the components collection of my pawn.

Begin Object Class=AudioComponent Name=MonsterActivatedSound1
         bAutoPlay=false
         SoundCue=MonsterNoiseSoundCue// This variable is a configured value.   SoundCue'CastleAudio.UI.UI_StopTouchToMove_Cue'
    End Object
    Components.Add(MonsterActivatedSound1);
    MonsterActivatedSound = MonsterActivatedSound1;

For some reason it doesn't build saying "Not allowed to use 'config' with object variable." Does anyone know of another way to approach this?

3

There are 3 answers

1
pdinklag On BEST ANSWER

That "Not allowed to use 'config' with object variable." message was a change in UnrealEngine 3.

I cannot test right now and I'm a UT2004 scripter, but I would try this:

var SoundCue MonsterNoiseSoundCue;
var config string MonsterNoiseSoundCueName;

In your PreBeginPlay function (or similar), use this to get the cue:

MonsterNoiseSoundCue = SoundCue(DynamicLoadObject(MonsterNoiseSoundCueName, class'SoundCue'));

You should get a warning in the log if the sound cue doesn't exist.

0
Ryan Lancaster On

What function are you using to play the sound?

PlaySound will create an AudioComponent on the fly so you shouldn't need to have a component in the defaultproperties section.

var config SoundCue MonsterNoiseSoundCue;

Then when your action happens:

function OnMonsterAction()
{
    PlaySound(MonsterNoiseSoundCue);
}
0
Mast__G On

Potential option are dynamic components 'Dynamically created components

To dynamically create an instance of a component, use the UnrealScript new operator and call the actor's AttachComponent method to attach the new component to the actor.'

simulated event PostBeginPlay()
{
    local AudioComponent AudioComponent;
    Super.PostBeginPlay();

    AudioComponent = new(self) class'AudioComponent';
    AudioComponent.SoundCue = Cue;//var(audio) SoundCue Cue
    AudioComponent.bAutoPlay=true; // !!!
    AttachComponent(AudioComponent); // Attach copm. to actor's copm. array 
    // .....
}

To detach and free the component which you attached earlier, use the actor's DetachComponent method.

http://wiki.beyondunreal.com/UE3:AudioComponent_(UDK)