DayZ 1.24
Loading...
Searching...
No Matches
EffectManager.c
Go to the documentation of this file.
1
6{
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
32
42 static int PlayInWorld(notnull Effect eff, vector pos)
43 {
44 // Stop the effect first, just in case
45 eff.Stop();
46
47 int id = EffectRegister(eff);
48
49 eff.SetPosition(pos);
50 eff.Start();
51
52 return id;
53 }
54
65 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)
66 {
67 // Stop the effect first, just in case
68 eff.Stop();
69
70 int id = EffectRegister(eff);
71
72 if (!obj)
73 {
74 ErrorEx("Parent object is null.", ErrorExSeverity.WARNING);
75 eff.SetPosition(local_pos);
76 }
77 else
78 eff.SetPosition(obj.GetPosition());
79
80 eff.SetParent(obj);
81 eff.SetLocalPosition(local_pos);
82 eff.SetAttachedLocalOri(local_ori);
83
85 {
87
88 if (eff_particle)
89 eff_particle.ForceParticleRotationRelativeToWorld(force_rotation_relative_to_world);
90 }
91
92 eff.Start();
93
94 return id;
95 }
96
101 static void Stop(int effect_id)
102 {
103 Effect eff = m_EffectsMap.Get(effect_id);
104
105 if (eff)
106 eff.Stop();
107 else
108 ErrorEx(string.Format("Failed to stop Effect with ID %1. The ID is not registered in m_EffectsMap!", effect_id));
109 }
110
112
113
118
130 static EffectSound CreateSound(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false, bool enviroment = false)
131 {
133 effect_sound.SetSoundSet(sound_set);
134 effect_sound.SetPosition(position);
135 effect_sound.SetSoundFadeIn(play_fade_in);
136 effect_sound.SetSoundFadeOut(stop_fade_out);
137 effect_sound.SetSoundLoop(loop);
138 effect_sound.SetEnviromentVariables(enviroment);
139
141
142 return effect_sound;
143 }
144
155 static EffectSound PlaySound(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
156 {
158
159 effect_sound.SoundPlay();
160
161 return effect_sound;
162 }
163
175 {
177
178 effect_sound.SoundPlayEx(params);
179
180 return effect_sound;
181 }
182
193 static EffectSound PlaySoundCachedParams(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
194 {
196
198
199 effect_sound.SoundPlayEx(params);
200
201 return effect_sound;
202 }
203
214 static EffectSound PlaySoundEnviroment(string sound_set, vector position, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
215 {
217
218 effect_sound.SoundPlay();
219
220 return effect_sound;
221 }
222
233 static EffectSound PlaySoundOnObject(string sound_set, Object parent_object, float play_fade_in = 0, float stop_fade_out = 0, bool loop = false)
234 {
236
237 effect_sound.SetParent(parent_object);
238 effect_sound.SetLocalPosition(vector.Zero);
239 effect_sound.SoundPlay();
240
241 return effect_sound;
242 }
243
245
246
251
256 static void DestroyEffect(Effect effect)
257 {
258 if (effect)
259 {
260 if (effect.CanDestroy())
261 {
262 // Functionality already happens in dtor of Effect to be safe
263 delete effect;
264 }
265 else
266 {
267 // Make it clean up itself when done
268 effect.SetAutodestroy(true);
269 effect.Stop();
270 }
271 }
272 }
273
279 static bool IsEffectExist(int effect_id)
280 {
281 if (!m_IsCleanup)
282 return m_EffectsMap[effect_id] != null;
283 else
284 return false;
285 }
286
292 static Effect GetEffectByID(int effect_id)
293 {
294 if (!m_IsCleanup)
295 return m_EffectsMap[effect_id];
296 else
297 return null;
298 }
299
307 static int EffectRegister(Effect effect)
308 {
309 if (effect.IsRegistered())
310 {
311 ErrorEx(string.Format("Attempted to register Effect '%1' which was already registered.", effect.GetDebugName()), ErrorExSeverity.INFO);
312 return effect.GetID();
313 }
314
315 int id;
316
317 if (!m_IsCleanup)
318 {
319 id = GetFreeEffectID();
320 m_EffectsMap.Insert(id, effect);
321 effect.Event_OnRegistered(id);
322 }
323 else
324 ErrorEx("Attempted to register Effect while SEffectManager is cleaning up, request ignored.", ErrorExSeverity.WARNING);
325
326 return id;
327 }
328
336 static void EffectUnregister(int id)
337 {
338 if (m_IsCleanup)
339 return; // No error needed, since it will have been unregistered anyways after cleanup is finished
340
341 Effect effect;
342 if (m_EffectsMap.Find(id, effect))
343 {
344 effect.Event_OnUnregistered();
345 m_EffectsMap.Remove(id);
346 }
347
348 if (m_FreeEffectIDs.Find(id) == -1)
349 m_FreeEffectIDs.Insert(id);
350 }
351
356 static void EffectUnregisterEx(Effect effect)
357 {
358 EffectUnregister(effect.GetID());
359 }
360
365 protected static int GetFreeEffectID()
366 {
367 int return_id;
368
369 if (m_FreeEffectIDs.Count() > 0)
370 {
371 return_id = m_FreeEffectIDs.Get(0);
372 m_FreeEffectIDs.Remove(0);
373 }
374 else
375 {
378 }
379
380 return return_id;
381 }
382
384
385
390
397 {
399 return true;
400 }
401
408 {
410 if (!m_ParamsMap.Find(soundset, params))
411 {
413 m_ParamsMap.Insert(soundset, params);
414 }
415 return params;
416 }
417
419
420
425
432 {
433
434 }
435
444 {
446 }
447
449
450
455
469
474 static void Cleanup()
475 {
476 // Nothing to clean
477 if (!m_IsInitialized)
478 return;
479
480 m_IsCleanup = true;
481
482 // There should not be anything in here on server
483 if (GetGame() && GetGame().IsDedicatedServer())
484 {
485 if (m_ParamsMap.Count() > 0)
486 ErrorEx(string.Format("SEffectManager containing SoundParams on server."), ErrorExSeverity.WARNING);
487
488 if (m_EffectsMap.Count() > 0)
489 ErrorEx(string.Format("SEffectManager containing Effect on server."), ErrorExSeverity.WARNING);
490 }
491
492 // These are intentionally cached, just clear them
493 m_ParamsMap.Clear();
494
495 // These might not be intentionally still here, so log how many there are
496#ifdef DEVELOPER
497 Print("--- SEffectManager Cleanup dump - Begin ------------------------");
498 Print(string.Format("Effect count: %1", m_EffectsMap.Count()));
499#endif
500
501 // Best to call the unregister event before clearing the map
502 // In case some ref is still being held elsewhere and will still be kept alive
503 foreach (int id, Effect eff : m_EffectsMap)
504 {
505 eff.Event_OnUnregistered();
506#ifdef SFXM_DUMP
507 Print(string.Format("%1 :: %2 :: %3", eff, typename.EnumToString(EffectType, eff.GetEffectType()), eff.GetDebugName()));
508#endif
509 }
510
511#ifdef DEVELOPER
512 Print("--- SEffectManager Cleanup dump - End --------------------------");
513#endif
514
515 // Now we can clear it
516 m_EffectsMap.Clear();
517
518 // Reset the state
520 Event_OnFrameUpdate.Clear();
521 m_IsCleanup = false;
522 }
523
525}
EffectType
Enum to determine what type of effect the Effect is.
Definition Effect.c:3
Wrapper class for managing particles through SEffectManager.
Wrapper class for managing sound through SEffectManager.
Definition EffectSound.c:5
Manager class for managing Effect (EffectParticle, EffectSound)
static void Event_OnSoundWaveEnded(EffectSound effect_sound)
Event called from EffectSound.Event_OnSoundWaveEnded.
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 void EffectUnregister(int id)
Unregisters Effect in SEffectManager.
static void Cleanup()
Cleanup method to properly clean up the static data.
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 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 int GetFreeEffectID()
Helper function for EffectRegister to decide an Effect ID.
static ref array< int > m_FreeEffectIDs
Static array of IDs that were previously used, but freed up by unregistering.
static const int INVALID_ID
As the counter starts at 1, Effect ID can never be 0.
static int PlayInWorld(notnull Effect eff, vector pos)
Play an Effect.
static ref ScriptInvoker Event_OnFrameUpdate
Static invoker for the SEffectManager.Event_OnFrameUpdate called form MissionGameplay....
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 Effect GetEffectByID(int effect_id)
Gets the Effect with the given registered Effect ID.
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 ref map< int, ref Effect > m_EffectsMap
Static map of all registered effects <id, Effect>
static int m_HighestFreeEffectID
Counter for quickly getting the next ID if FreeEffectIDs array is empty.
static bool m_IsCleanup
Bool to check whether Cleanup is happening, which means that the maps should no longer be accessed.
static void Init()
Initialize the containers.
static void Stop(int effect_id)
Stops the Effect.
static SoundParams GetCachedSoundParam(string soundset)
Get or create a cached SoundParams object.
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 bool m_IsInitialized
Bool to check whether Init was called.
static bool IsEffectExist(int effect_id)
Checks whether an Effect ID is registered in SEffectManager.
static bool DestroySound(EffectSound sound_effect)
Legacy, backwards compatibility.
static void EffectUnregisterEx(Effect effect)
Unregisters Effect in SEffectManager.
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 ref map< string, ref SoundParams > m_ParamsMap
Static map of cached sound params, to prevent having to recreate them.
static int EffectRegister(Effect effect)
Registers Effect in SEffectManager.
static void DestroyEffect(Effect effect)
Unregisters, stops and frees the Effect.
static void Event_OnFrameUpdate(float time_delta)
Event called on frame.
ScriptInvoker Class provide list of callbacks usage:
Definition tools.c:116
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