|
Post by insipid1 on Oct 1, 2022 19:21:42 GMT
Hi there,
I'm looking for some help regarding two items.
1) The first is a warning that appears related to an event trying to fire twice in the same frame:
What is the best practice to make this stop happening? Currently there are multiple wolves walking around the zone in our game, and each one has the Event Sounds script on it, which allows it to play some footstep sounds while walking around. The Animation Events are on the wolves Walk Animation, and they have been spaced out more than one frame apart from each other. Is it happening because there are multiple wolves in the scene?
2) The next item I'm not sure about is related to Custom Events (which are configured for the wolves above).
These Custom Events have the ability to Send to Receivers, and I'm trying to make it so only wolves near the player will receive these events. The best option for that appears to be the When Distance Less Than Per-event Control, however when I set a distance here it doesn't seem to matter. I can move the wolves across the entire map, far away from the player but they will still receive the event and the audio clips will still play. The actual Event Sounds script is on a child game object of the wolf (the parent is created during runtime from an NPC Spawner).
I'm sure I'm doing something wrong here, but I just can't figure out what it is. Any ideas?
Thanks in advance!
|
|
|
Post by DarkTonic Dev on Oct 2, 2022 6:52:26 GMT
Yes, it's because you have more than one Wolf. There must be more than one of them trying to fire the same event on the same frame. I don't know what "trigger event" you're using to fire the event. We could add a checkbox to disable the logging of the duplicate if you need it. Otherwise, I'd suggest you code it normally and don't use Event Sounds script for this one. That way you can call MasterAudio.FireCustomEvent passing false for the last parameter "logDupe". In either case, the Custom Event can only be fired once per frame. Even if the warning isn't logged, any additional calls to fire the event that frame will be ignored. So it's possible you may want to use different Custom Events for each wolf if you need more than 1 to fire at the same time.
The "When Distance Less Than" option will be working on the position of the Event Sounds script (source and destination), not the wolf, so keep that in mind. If it's not a zero offset, you may be looking at the wrong position. Also, I'm not sure the scale/size of your Scene, so it's possible that clear across the map would still be too close. You'll need to check the actual distance. You can edit the code to log it if you like. It's in the FireCustomEvent method of MasterAudio.
Let me know what you find out! -Brian
|
|
|
Post by insipid1 on Oct 11, 2022 20:36:21 GMT
Thanks for the reply! I was on vacation so I'm back and ready to work on this.
Got it, I would say it's not terribly important to have too many of these sounds firing off all at the exact same time. I think just disabling the logging would be great, I'm cool with either solution, it could be something that pops up for someone else down the line and the checkbox would be nice for them (especially if they're not comfortable with coding).
I also wanted to add that I get this showing up even when there is only one wolf in the scene: Already fired Custom Event 'WalkForward' this frame or later. Cannot be fired twice in the same frame.
For the next topic, Understood. I had a look at this script and put some logs in there, and here's what I was able to figure out with one wolf in the scene:
if (!validReceiversSet) { // only used for "OnXGameObject" Send To Receiver modes foreach (var receiver in dict.Keys) { switch (customEvent.eventReceiveMode) { case CustomEventReceiveMode.WhenDistanceLessThan: var dist1 = (dict[receiver].position - originPoint).sqrMagnitude; Debug.Log(receiver); Debug.Log("dict[receiver].position: " + dict[receiver].position); Debug.Log("originPoint: " + originPoint); Debug.Log("dist1: " + dist1); if (dist1 > sqrDist) { continue; } break;
The output was: >> dict[receiver].position: (-49.16, 0.00, 157.34)
>> originPoint: (-49.16, 0.00, 157.34)
>> dist1: 0
The transform coordinates there are exactly on the wolf, so like you said, that's where the Events Sound Script is located. So the fact that they are the same, giving a 0 distance, is why I'm having my problem. When I Fire! an event from MasterAudio the originPoint is (0, 0, 0), which is where the MasterAudio game object is located in the scene.
I feel like I've got something misconfigured or misunderstood. What I obviously need to happen is for the distance to be calculated from the Audio Listener on the Camera to the Wolf. How do I go about making that happen?
Thanks again for your help 
|
|
|
Post by DarkTonic Dev on Oct 11, 2022 20:43:05 GMT
The distance will be the distance between the game object that fires the custom event and the receiver of the custom event. So if the wolf is the receiver, the camera game object would need to be the one doing the firing. Is it?
Also, in the code above where you logged out some things, what is the sqrDistance? I assume it's exiting the method early or skipping your receiver.
I'll look into adding a checkbox for log spamping.
-B
|
|
|
Post by insipid1 on Oct 11, 2022 22:39:49 GMT
Got it, so these are Animation Events, being sent from an Animator on the wolf. They are received by and played from a script that lives on the wolf itself. Since my script and the Master Audio Events Script are on the same game object, we have this situation.
I guess there are two options for me. Either move the script onto the camera or player and have it handle the sending from there, or have the game object transform of my script mirror the position of the player. Which option do you think is better? (I'm so glad I understand this better now - thanks a lot!)
The value of sqrt is: Infinity
Great, thanks  -B
|
|
|
Post by DarkTonic Dev on Oct 12, 2022 2:02:05 GMT
Infinity eh? Well since you're sending to the same game object, let's try this. In the setup for the Custom Event itself on the Master Audio game object, do not use one of the distance options. Set "Send To Receivers" field to "Same Game Object". There are also options for "Same or parent Game Object" and Same or child, depending on how your prefab is structured.
|
|
|
Post by insipid1 on Oct 12, 2022 17:08:22 GMT
Infinity eh? Well since you're sending to the same game object, let's try this. In the setup for the Custom Event itself on the Master Audio game object, do not use one of the distance options. Set "Send To Receivers" field to "Same Game Object". There are also options for "Same or parent Game Object" and Same or child, depending on how your prefab is structured. Yep, I looked closer and here's why it was infinity:
It looks like the number is 3.something, but it's actually a really huge number. When debugging I can see it's this:
3.402823e+38
So, when I made this number smaller, something similar to the scale of my game scene, it worked perfectly!
The other mistake in my code was the line below:
All I had to do to get it to use the listener was cache the camera and use its transform 
As for the other options, Parent Game Object did work, but I really want to go the distance route so that far away things don't have to bother playing sounds.
Thanks so much for all your help!
|
|
|
Post by DarkTonic Dev on Oct 12, 2022 19:45:14 GMT
Yeah, if you need more than 1 to play the sound you need distance. Cool!
|
|
|
Post by DarkTonic Dev on Oct 12, 2022 20:09:57 GMT
I've added the checkbox to Event Sounds script to disable the duplication event firing log, for the next update.
|
|
|
Post by insipid1 on Oct 14, 2022 10:21:15 GMT
Awesome! Thanks so much 
|
|
|
Post by DarkTonic Dev on Oct 14, 2022 14:59:10 GMT
No problem.
|
|