How to use the random function in c++ for a blueprint?


I am trying to created an object that will explode at a random time.

I have made a blueprint off a class I have created. The issue is my random time for all the instances of the blueprint on the map are the same.

I have tried the generating the random number in the constructor as well as in the begin play method however I still cannot seem to get it to work.

float AExplosiveObject::GetRandomDelay()
	return FMath::RandRange(MinBlowUpDelay, MaxBlowUpDelay);

void AExplosiveObject::BeginPlay()
	BlowUpDelay = GetRandomDelay();

void AExplosiveObject::Tick(float DeltaTime)
	//counting time
	ExplodeTime += DeltaTime;

	//when time exceeds the delay
	if (ExplodeTime > BlowUpDelay)

What I want is for every explosive object spawned on the map to have its own randomised delay.

Hey Prit-

Your method asks for a float to be returned however using RandRange() the way you have it typed is asking for an Int value. You want to use FRandRange() to ensure the value returned is a float as well. Not sure if this will completely solve your problem, but it should help. Let us know if you have any other questions.


Thanks. That worked!

what macro did you used in the Header File?

Hey germ1389-

I’m not sure what you mean exactly by macro, the necessary include is “EngineMinimal.h” which should be included in the project header.

If you need more support for your issue, please start a new post. Doing so will help with tracking.