How to properly create beam emitter in C++ using source and target method

Hi Everyone,
I have a question similar to the one present in Q/A - Beam particle source/target in C++.
But my problem persists despite following those guidelines. The problem is: I am unable to set the beam source and target points through the C++ program. Any suggestions, comments ?

First the code, it is trying to create 5 laser beams each 10 units apart along Z axis.


The above image shows that its picking up the source and target- due to the default length of 100 units from the template particle system. While through program I am trying to change the beam length to 400 units by setting the source and the target points. What is that crucial setting that I missing out here ? I checked the documentation, and searched around and around, but couldnā€™t find this.



        int32 NumberOfBeams = 5;
	int32 BeamLength = 400;
	int32 ZOffset = 10;

	FVector BoxLocation = GetOriginInVolume();
	FVector TargetPoint;


	for (int32 i = 0; i < NumberOfBeams; i++)  // Along X Axis
	{
		TargetPoint.X = BoxLocation.X;                 // Constant
		TargetPoint.Y = BoxLocation.Y;                 // Constant
		TargetPoint.Z = BoxLocation.Z + i*ZOffset;   // Moving in Straight Line

		if (BeamParticleSystem)
		{
			UParticleSystemComponent* CurrentEmitter = UGameplayStatics::SpawnEmitterAtLocation(
				GetWorld(),
				BeamParticleSystem->Template,
				TargetPoint,
				FRotator::ZeroRotator,
				true);

			CurrentEmitter->InitializeSystem();
			CurrentEmitter->SetBeamSourcePoint(0, TargetPoint, 0);

			FVector NewPoint = FVector(TargetPoint);
			NewPoint.X = NewPoint.X + BeamLength;  // Beam length in X direction 
			CurrentEmitter->SetBeamTargetPoint(0, NewPoint, 0);
			CurrentEmitter->ActivateSystem();
		}
	}
        ![SourceTargetSetting.png|1800x1067](upload://zzqq4CIhPXpjO4BnJk3Kkz1vg4w.png)

Can we bump this please ?

Small update: I was able to create the desired change in the beam length but there is a problem. Please see the attachment,

.

Here is the altered code that tries to create two beam emitters with program specified parameters:


 int32 NumberOfBeams = 2;
	int32 BeamLength = 200;
	int32 ZOffset = 20;

	FVector BoxLocation = GetOriginInVolume();
	FVector BeamSourcePoint;

	UParticleSystem* TemplateParticleSystem = BeamParticleSystem->Template;
	
	for (int32 i = 0; i < NumberOfBeams; i++)  // Along X Axis
	{
		BeamSourcePoint.X = BoxLocation.X;                 // Constant
		BeamSourcePoint.Y = BoxLocation.Y;                 // Constant
		BeamSourcePoint.Z = BoxLocation.Z + i*ZOffset;   // Moving in Straight Line

		if (BeamParticleSystem)
		{
			// Prepare
			//UParticleSystemComponent* CurrentTemp = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("TempParticleSystem"));
			//CurrentTemp->SetTemplate(TemplateParticleSystem);
			UParticleSystemComponent* CurrentTemp = BeamParticleSystem;
			
			// On Fly altering the source and target points
			CurrentTemp->SetBeamSourcePoint(0, BeamSourcePoint, 0);
			FVector BeamTargetPoint = FVector(BeamSourcePoint);
			BeamTargetPoint.X = BeamTargetPoint.X + BeamLength;  // Beam length in X direction 
			CurrentTemp->SetBeamTargetPoint(0, BeamTargetPoint, 0);

			// Spawning
			UParticleSystemComponent* CurrentEmitter = UGameplayStatics::SpawnEmitterAtLocation(
				GetWorld(),
				CurrentTemp->Template,
				BeamSourcePoint,
				FRotator::ZeroRotator,
				true);

			// Destroy
			//CurrentTemp->DestroyComponent();
			
			
		}
	}


When I try to change the template of the emitter to set the beam length, only the last update is retained. Like the first beam follows the default properties from the template. And the next beam follows the changed settings of the desired beam source and target from the program.

How do I do this setting which is different for all different emitter? Surely I am missing some settings updates.
Can someone well versed with these systems shine a light on this ?

I finally got it working on my own :slight_smile:
The issue was weird to me. I am noting down my findings for others, along with some demonstration of what I was trying to create.
In the particle system template emitter, there is a property for both source and target called ā€œDistribution.ā€ If you select it to be ā€˜Distribution Vector Constantā€™ the methods of SetBeamSourcePoint and SetBeamTargetPoint are rendered futile.
However, if you set it to ā€˜Distribution Vector Particle Parameterā€™ you can see the change if you use the Set Target/Source methods. I got it working finally after about a month when I first posted my problem hoping someone knowledgeable would know about this. Of course, I was keeping this in mind while hunting for other ideas, now I have resolved it !
I hope this helps to some other seeker in this path.

4efd3731efce3627fa6c78943e5cc3150797e6bc.gif

Here is the new code:


 
        int32 NumberOfBeams = 100;
	int32 BeamLength = 500;
	int32 ZOffset = 5;

	FVector BoxLocation = GetOriginInVolume();
	FVector BeamSourcePoint;

	UParticleSystem* TemplateParticleSystem = BeamParticleSystem->Template;
	
	for (int32 i = 0; i <= NumberOfBeams; i++)  // Along X Axis
	{
		BeamSourcePoint.X = BoxLocation.X;                 // Constant
		BeamSourcePoint.Y = BoxLocation.Y;                 // Constant
		BeamSourcePoint.Z = BoxLocation.Z + i*ZOffset;     // Moving in Straight Line

		if (BeamParticleSystem)
		{
			FVector BeamTargetPoint = FVector(BeamSourcePoint);
			BeamTargetPoint.X = BeamTargetPoint.X + BeamLength;  // Beam length in X direction 

			// Prepare On Fly altering the source and target points -- This thing is doing nothing
			//CurrentTemp->SetBeamSourcePoint(0, BeamSourcePoint, 0);
			//CurrentTemp->SetBeamTargetPoint(0, BeamTargetPoint, 0);
			//CurrentTemp->SetBeamEndPoint(i, BeamTargetPoint);

			// Spawning
			UParticleSystemComponent* CurrentEmitter = UGameplayStatics::SpawnEmitterAtLocation(
				GetWorld(),
				BeamParticleSystem->Template,
				BeamSourcePoint,
				FRotator::ZeroRotator,
				true);

			CurrentEmitter->SetBeamSourcePoint(0, BeamSourcePoint, 0);
			CurrentEmitter->SetBeamTargetPoint(0, BeamTargetPoint, 0);
		}
	}

	for (int32 i = 0; i <= NumberOfBeams; i++)  // Along X Axis
	{
		BeamSourcePoint.X = BoxLocation.X + i*ZOffset;                 // Constant
		BeamSourcePoint.Y = BoxLocation.Y;                 // Constant
		BeamSourcePoint.Z = BoxLocation.Z ;     // Moving in Straight Line

		if (BeamParticleSystem)
		{
			FVector BeamTargetPoint = FVector(BeamSourcePoint);
			BeamTargetPoint.Z = BeamTargetPoint.Z + BeamLength;  // Beam length in X direction 

			// Spawning
			UParticleSystemComponent* CurrentEmitter = UGameplayStatics::SpawnEmitterAtLocation(
				GetWorld(),
				BeamParticleSystem->Template,
				BeamSourcePoint,
				FRotator::ZeroRotator,
				true);

			CurrentEmitter->SetBeamSourcePoint(0, BeamSourcePoint, 0);
			CurrentEmitter->SetBeamTargetPoint(0, BeamTargetPoint, 0);
		}
	}


1 Like

Hi,

You donā€™t have an updated version of this project do you by any chance? Like all the source files, etc. Or even the version you showed here?

Thanks!

OP put the code in the post directly above yours.

thank you Iā€™ve been looking for something like this < 33