DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
Loading...
Searching...
No Matches
EffectManager.c
Go to the documentation of this file.
1
6{
8 protected static ref map<int, ref Effect> m_EffectsMap;
10 protected static ref array<int> m_FreeEffectIDs;
12 protected static int m_HighestFreeEffectID = 1;
14 static const int INVALID_ID = 0;
16 protected static bool m_IsCleanup;
18 protected static bool m_IsInitialized;
19
22
25
26
27
33
43 static int PlayInWorld(notnull Effect eff, vector pos)
44 {
45 // Stop the effect first, just in case
46 eff.Stop();
47
48 int id = EffectRegister(eff);
49
50 eff.SetPosition( pos );
51 eff.Start();
52
53 return id;
54 }
55
66 static int PlayOnObject(notnull Effect eff, Object obj, vector local_pos = "0 0 0", vector local_ori = "0 0 0", bool force_rotation_relative_to_world = false)
67 {
68 // Stop the effect first, just in case
69 eff.Stop();
70
71 int id = EffectRegister(eff);
72
73 if (!obj)
74 {
75 ErrorEx("Parent object is null.", ErrorExSeverity.WARNING);
76 eff.SetPosition(local_pos);
77 }
78 else
79 {
80 eff.SetPosition(obj.GetPosition());
81 }
82
83 eff.SetParent(obj);
84 eff.SetLocalPosition(local_pos);
85 eff.SetAttachedLocalOri(local_ori);
86
87 if (force_rotation_relative_to_world)
88 {
89 EffectParticle eff_particle = EffectParticle.Cast(eff);
90
91 if (eff_particle)
92 {
93 eff_particle.ForceParticleRotationRelativeToWorld(force_rotation_relative_to_world);
94 }
95 }
96
97 eff.Start();
98
99 return id;
100 }
101
106 static void Stop(int effect_id)
107 {
108 Effect eff = m_EffectsMap.Get(effect_id);
109
110 if (eff)
111 {
112 eff.Stop();
113 }
114 else
115 {
116 ErrorEx(string.Format("Failed to stop Effect with ID %1. The ID is not registered in m_EffectsMap!", effect_id));
117 }
118 }
119
121
122
123
128
140 static EffectSound CreateSound(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false, bool enviroment = false)
141 {
142 EffectSound effect_sound = new EffectSound();
143 effect_sound.SetSoundSet(sound_set);
144 effect_sound.SetPosition(position);
145 effect_sound.SetSoundFadeIn(play_fade_in);
146 effect_sound.SetSoundFadeOut(stop_fade_out);
147 effect_sound.SetSoundLoop(loop);
148 effect_sound.SetEnviromentVariables(enviroment);
149
150 EffectRegister( effect_sound );
151
152 return effect_sound;
153 }
154
165 static EffectSound PlaySound(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
166 {
167 EffectSound effect_sound = CreateSound(sound_set, position, play_fade_in, stop_fade_out, loop, false);
168
169 effect_sound.SoundPlay();
170
171 return effect_sound;
172 }
173
184 static EffectSound PlaySoundParams(notnull SoundParams params, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
185 {
186 EffectSound effect_sound = CreateSound(params.GetName(), position, play_fade_in, stop_fade_out, loop, false);
187
188 effect_sound.SoundPlayEx(params);
189
190 return effect_sound;
191 }
192
203 static EffectSound PlaySoundCachedParams(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
204 {
205 SoundParams params = GetCachedSoundParam(sound_set);
206
207 EffectSound effect_sound = CreateSound(params.GetName(), position, play_fade_in, stop_fade_out, loop, false);
208
209 effect_sound.SoundPlayEx(params);
210
211 return effect_sound;
212 }
213
224 static EffectSound PlaySoundEnviroment(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
225 {
226 EffectSound effect_sound = CreateSound(sound_set, position, play_fade_in, stop_fade_out, loop, true);
227
228 effect_sound.SoundPlay();
229
230 return effect_sound;
231 }
232
243 static EffectSound PlaySoundOnObject(string sound_set, Object parent_object, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
244 {
245 EffectSound effect_sound = CreateSound(sound_set, parent_object.GetPosition(), play_fade_in, stop_fade_out, loop);
246
247 effect_sound.SetParent( parent_object );
248 effect_sound.SetLocalPosition( vector.Zero );
249 effect_sound.SoundPlay();
250
251 return effect_sound;
252 }
253
255
256
257
262
267 static void DestroyEffect(Effect effect)
268 {
269 if (effect)
270 {
271 // Functionality already happens in dtor of Effect to be safe
272 delete effect;
273 }
274 }
275
281 static bool IsEffectExist( int effect_id )
282 {
283 if (!m_IsCleanup)
284 return m_EffectsMap[effect_id] != null;
285 else
286 return false;
287 }
288
294 static Effect GetEffectByID(int effect_id)
295 {
296 if (!m_IsCleanup)
297 return m_EffectsMap[effect_id];
298 else
299 return null;
300 }
301
309 static int EffectRegister(Effect effect)
310 {
311 if (effect.IsRegistered())
312 {
313 ErrorEx(string.Format("Attempted to register Effect '%1' which was already registered.", effect.GetDebugName()), ErrorExSeverity.INFO);
314 return effect.GetID();
315 }
316
317 int id;
318
319 if (!m_IsCleanup)
320 {
321 id = GetFreeEffectID();
322 m_EffectsMap.Insert(id, effect);
323 effect.Event_OnRegistered(id);
324 }
325 else
326 ErrorEx("Attempted to register Effect while SEffectManager is cleaning up, request ignored.", ErrorExSeverity.WARNING);
327
328 return id;
329 }
330
338 static void EffectUnregister(int id)
339 {
340 if (m_IsCleanup)
341 return; // No error needed, since it will have been unregistered anyways after cleanup is finished
342
343 Effect effect;
344 if ( m_EffectsMap.Find(id, effect) )
345 {
346 effect.Event_OnUnregistered();
347 m_EffectsMap.Remove(id);
348 }
349
350 if ( m_FreeEffectIDs.Find(id) == -1 )
351 {
352 m_FreeEffectIDs.Insert(id);
353 }
354 }
355
360 static void EffectUnregisterEx(Effect effect)
361 {
362 EffectUnregister(effect.GetID());
363 }
364
369 protected static int GetFreeEffectID()
370 {
371 int return_id;
372
373 if (m_FreeEffectIDs.Count() > 0)
374 {
375 return_id = m_FreeEffectIDs.Get(0);
376 m_FreeEffectIDs.Remove(0);
377 }
378 else
379 {
380 return_id = m_HighestFreeEffectID;
382 }
383
384 return return_id;
385 }
386
388
389
390
395
401 static bool DestroySound(EffectSound sound_effect)
402 {
403 DestroyEffect(sound_effect);
404 return true;
405 }
406
412 static SoundParams GetCachedSoundParam(string soundset)
413 {
414 SoundParams params;
415 if (!m_ParamsMap.Find(soundset, params))
416 {
417 params = new SoundParams(soundset);
418 m_ParamsMap.Insert(soundset, params);
419 }
420 return params;
421 }
422
424
425
426
431
437 static void Event_OnSoundWaveEnded(EffectSound effect_sound)
438 {
439
440 }
441
449 static void Event_OnFrameUpdate(float time_delta)
450 {
451 Event_OnFrameUpdate.Invoke(time_delta);
452 }
453
455
456
457
462
467 static void Init()
468 {
473
474 m_IsInitialized = true;
475 }
476
481 static void Cleanup()
482 {
483 // Nothing to clean
484 if (!m_IsInitialized)
485 return;
486
487 m_IsCleanup = true;
488
489 // There should not be anything in here on server
490 if (GetGame() && GetGame().IsDedicatedServer())
491 {
492 if (m_ParamsMap.Count() > 0)
493 ErrorEx(string.Format("SEffectManager containing SoundParams on server."), ErrorExSeverity.WARNING);
494
495 if (m_EffectsMap.Count() > 0)
496 ErrorEx(string.Format("SEffectManager containing Effect on server."), ErrorExSeverity.WARNING);
497 }
498
499 // These are intentionally cached, just clear them
500 m_ParamsMap.Clear();
501
502 // These might not be intentionally still here, so log how many there are
503 #ifdef DEVELOPER
504 Print("--- SEffectManager Cleanup dump - Begin ------------------------");
505 Print(string.Format("Effect count: %1", m_EffectsMap.Count()));
506 #endif
507
508 // Best to call the unregister event before clearing the map
509 // In case some ref is still being held elsewhere and will still be kept alive
510 foreach (int id, Effect eff : m_EffectsMap)
511 {
512 eff.Event_OnUnregistered();
513 #ifdef SFXM_DUMP
514 Print(string.Format( "%1 :: %2 :: %3", eff, typename.EnumToString(EffectType, eff.GetEffectType()), eff.GetDebugName() ));
515 #endif
516 }
517
518 #ifdef DEVELOPER
519 Print("--- SEffectManager Cleanup dump - End --------------------------");
520 #endif
521
522 // Now we can clear it
523 m_EffectsMap.Clear();
524
525 // Reset the state
528 m_IsCleanup = false;
529 }
530
532}
EffectType
Enum to determine what type of effect the Effect is.
Definition Effect.c:3
void Effect()
ctor
Definition Effect.c:70
Wrapper class for managing particles through SEffectManager.
void ForceParticleRotationRelativeToWorld(bool state)
Set orientation setting to be used by the effect when the Effect starts.
Wrapper class for managing sound through SEffectManager.
Definition EffectSound.c:5
void SetEnviromentVariables(bool setEnvVariables)
Sets whether UpdateEnvSoundControllers needs to be called during Loading.
bool SoundPlay()
Plays sound.
void SetSoundSet(string snd)
Set soundset for the sound.
void SetSoundFadeOut(float fade_out)
Set the sound fade out duration.
override void SetParent(Object parent_obj)
Set parent for the sound to follow.
void SetSoundFadeIn(float fade_in)
Set the sound fade in duration.
bool SoundPlayEx(out SoundParams params)
Plays sound.
void SetSoundLoop(bool loop)
Set if the sound loops.
Manager class for managing Effect (EffectParticle, EffectSound)
static void Stop(int effect_id)
Stops the Effect.
static void Event_OnFrameUpdate(float time_delta)
Event called on frame.
static void Init()
Initialize the containers.
static int PlayInWorld(notnull Effect eff, vector pos)
Play an Effect.
static const int INVALID_ID
As the counter starts at 1, Effect ID can never be 0.
static void Cleanup()
Cleanup method to properly clean up the static data.
static Effect GetEffectByID(int effect_id)
Gets the Effect with the given registered Effect ID.
static ref ScriptInvoker Event_OnFrameUpdate
Static invoker for the SEffectManager.Event_OnFrameUpdate called form MissionGameplay....
static void EffectUnregister(int id)
Unregisters Effect in SEffectManager.
static EffectSound PlaySoundEnviroment(string sound_set, vector position, float play_fade_in=0, float stop_fade_out=0, bool loop=false)
Create and play an EffectSound, updating environment variables.
static int PlayOnObject(notnull Effect eff, Object obj, vector local_pos="0 0 0", vector local_ori="0 0 0", bool force_rotation_relative_to_world=false)
Play an Effect.
static EffectSound CreateSound(string sound_set, vector position, float play_fade_in=0, float stop_fade_out=0, bool loop=false, bool enviroment=false)
Create an EffectSound.
static EffectSound PlaySoundOnObject(string sound_set, Object parent_object, float play_fade_in=0, float stop_fade_out=0, bool loop=false)
Create and play an EffectSound.
static protected ref map< int, ref Effect > m_EffectsMap
Static map of all registered effects <id, Effect>
static protected int GetFreeEffectID()
Helper function for EffectRegister to decide an Effect ID.
static EffectSound PlaySoundCachedParams(string sound_set, vector position, float play_fade_in=0, float stop_fade_out=0, bool loop=false)
Create and play an EffectSound, using or creating cached SoundParams.
static protected bool m_IsCleanup
Bool to check whether Cleanup is happening, which means that the maps should no longer be accessed.
static void DestroyEffect(Effect effect)
Unregisters, stops and frees the Effect.
static void Event_OnSoundWaveEnded(EffectSound effect_sound)
Event called from EffectSound.Event_OnSoundWaveEnded.
static bool IsEffectExist(int effect_id)
Checks whether an Effect ID is registered in SEffectManager.
static void EffectUnregisterEx(Effect effect)
Unregisters Effect in SEffectManager.
static bool DestroySound(EffectSound sound_effect)
Legacy, backwards compatibility.
static int EffectRegister(Effect effect)
Registers Effect in SEffectManager.
static SoundParams GetCachedSoundParam(string soundset)
Get or create a cached SoundParams object.
static EffectSound PlaySoundParams(notnull SoundParams params, vector position, float play_fade_in=0, float stop_fade_out=0, bool loop=false)
Create and play an EffectSound.
static protected ref map< string, ref SoundParams > m_ParamsMap
Static map of cached sound params, to prevent having to recreate them.
static EffectSound PlaySound(string sound_set, vector position, float play_fade_in=0, float stop_fade_out=0, bool loop=false)
Create and play an EffectSound.
static protected ref array< int > m_FreeEffectIDs
Static array of IDs that were previously used, but freed up by unregistering.
static protected int m_HighestFreeEffectID
Counter for quickly getting the next ID if FreeEffectIDs array is empty.
static protected bool m_IsInitialized
Bool to check whether Init was called.
ScriptInvoker Class provide list of callbacks usage:
Definition tools.c:116
proto void Invoke(void param1=NULL, void param2=NULL, void param3=NULL, void param4=NULL, void param5=NULL, void param6=NULL, void param7=NULL, void param8=NULL, void param9=NULL)
invoke call on all inserted methods with given arguments
proto native void Clear()
remove all calls from list
proto string GetName()
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
static const vector Zero
Definition EnConvert.c:110
proto native CGame GetGame()
ErrorExSeverity
Definition EnDebug.c:62
proto void Print(void var)
Prints content of variable to console/log.
enum ShapeType ErrorEx