我有一个声音提示,每当玩家执行特定操作时我都会播放该声音提示。我已经运行得很好,但我想让使用的资源来自配置文件而不是硬编码。
所以我向我的类添加了一个名为 MonsterNoiseSoundCue 的属性,如下所示:
var config SoundCue MonsterNoiseSoundCue;
然后在 DefaultProperties 部分中,我将以下内容添加到我创建的对象中,然后将其添加到我的 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;
由于某种原因,它不会构建说“不允许将‘config’与对象变量一起使用。”有谁知道另一种方法来解决这个问题?
“不允许将‘config’与对象变量一起使用。”消息是 UnrealEngine 3 中的更改。
我现在无法测试,我是 UT2004 脚本编写者,但我会尝试这个:
var SoundCue MonsterNoiseSoundCue;
var config string MonsterNoiseSoundCueName;
在 PreBeginPlay 函数(或类似函数)中,使用它来获取提示:
MonsterNoiseSoundCue = SoundCue(DynamicLoadObject(MonsterNoiseSoundCueName, class'SoundCue'));
如果声音提示不存在,您应该在日志中收到警告。
您使用什么功能来播放声音?
PlaySound 将动态创建一个 AudioComponent,因此您不需要在默认属性部分中有一个组件。
var config SoundCue MonsterNoiseSoundCue;
然后当你的行动发生时:
function OnMonsterAction()
{
PlaySound(MonsterNoiseSoundCue);
}
要动态创建组件的实例,请使用 UnrealScript new 运算符并调用 actor 的 AttachComponent 方法将新组件附加到 actor。'
simulated event PostBeginPlay()
{
local AudioComponent AudioComponent;
Super.PostBeginPlay();
AudioComponent = new(self)
AudioComponent.SoundCue = Cue;//var(audio) SoundCue Cue
AudioComponent.bAutoPlay=true;
AttachComponent(AudioComponent); // Attach copm. to actor's copm. array
// .....
}
要分离并释放之前附加的组件,请使用 actor 的 DetachComponent 方法。