I want some sounds to have a small chance of playing randomly during the level, based on a periodic chance generator. I already have a sound queue set up with the sounds that I want, but I am not sure how to set up code that runs a timer throughout the level, and periodically runs a designated chance number to determine if the sound plays at that particular moment.
Are you wanting this to be action based (i.e. the player moves into a trigger) or just randomly throughout the level (i.e. a timer that runs every 30 seconds or so, which then plays the sound by chance)?
What specifically are you having trouble with? Timers are pretty straight forward and have a lot of control to allow when functions will be run, if you are wanting the trigger to be time based.
Should be an easy thing to implement if you provide a bit more detail on exactly what you are wanting.
Using Timers in Unreal Engine | Unreal Engine 5.5 Documentation | Epic Developer Community
I want to have the sound play randomly throughout the level, and then play the sound based on chance every so often, yes. I know how to use timers but not how build a chance generator. And a little while ago, I reading somewhere that this could not be done in unreal but I think I was mistaken.
As a specific example, I want a randomly-selected sound from my sound queue to have a % chance of playing every 60 seconds. I just don’t know how to build the chance generator.
Edit: my current thought process on this is that I should make a random number generator, and then set up an if statement to play one of the sounds from the queue if it is lower than a certain number.
There are many ways to do this in both C++ and blueprints (not sure which you are using, but the logic is essentially the same). “Random Bool With Weight” is a good function to use in blueprints to gatekeep with chance. You could essentially recreate that logic by generating a random float/int between 0-100, and returning true if it is above a certain number (i.e. above 50 for 50% chance). As per your example, you can simply create a function that starts with a branch and uses the Random Bool with Weight function to determine if it will continue with the logic or just return.
As for selecting a random sound base reference, you can create an array that contains all the sound base references and get a random index (either by generating a random index and getting at that index from the array, or in blueprints I believe there is an actual function you can call on the array to get a random item.
Like I said, there are many different ways to achieve this and there are plenty of C++ and Blueprint functions that can help you here. You could create a custom actor class to handle this logic, or if it is specific to a certain level, the level blueprint would be a good place as well. If the logic is meant to be utilized over all levels, the game state would be a good, accessible class to place the logic.
Random Bool with Weight | Unreal Engine 5.6 Documentation | Epic Developer Community