I have a task to create a chance of actor dead. The chance have to be depending from the value I set manually.
So is there a function in UE4 that can take a value in percentages, and make an event to be happened depending on the value?
One thing you could do is use FMath::FRand() to get a float between 0.0 and 1.0 (think of it as 0% and 100%), and then check if that value is less than the percent you’re checking for (which should also be between 0.0 and 1.0. Think of 0.5 as 50%).
So an example would be: You want the chance of an actor dying to be 20%.
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float chanceForDeathPercent = 0.2f;
(Making that a UPROPERTY will allow you to edit it in Blueprints, if you need to)
Then, when you’re checking to see if the actor is dead, you could do something like this:
const float deathRoll = FMath::FRand();
if (deathRoll < chanceForDeathPercent)
{
// Set the actor as dead
}
(You could also do this with integers using 0 to 100 and FMath::RandRange())
Now, I don’t believe this is statistically accurate (That is, it might not actually happen 20% of the time). I’m sure someone who took statistics class could tell you why this might be slightly off. However, as far as game mechanics go, it should be fine.