I want to use Niagara in C++, but I encountered a problem and did not succeed in adding it. Does any boss know how to do it?

Here are my changes:
in Build.cs:

using UnrealBuildTool;

public class FPS_2 : ModuleRules
{
	public FPS_2(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay","Niagara" });
		PrivateDependencyModuleNames.AddRange(new string[] { "Niagara" });
	}
}

in uproject:
{
	"FileVersion": 3,
	"EngineAssociation": "4.27",
	"Category": "",
	"Description": "",
	"Modules": [
		{
			"Name": "FPS_2",
			"Type": "Runtime",
			"LoadingPhase": "Default"
		},
		{
			"Name": "Niagara",
			"Type": "Runtime",
			"Enabled": true
		}
	]
}

in .h:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FPS_2Projectile.generated.h"

class USphereComponent;
class UProjectileMovementComponent;
UCLASS(config=Game)
class AFPS_2Projectile : public AActor
{
	GENERATED_BODY()

	/** Sphere collision component */
	UPROPERTY(VisibleDefaultsOnly, Category=Projectile)
	USphereComponent* CollisionComp;

	/** Projectile movement component */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true"))
	UProjectileMovementComponent* ProjectileMovement;

public:
	AFPS_2Projectile();

	/** called when projectile hits something */
	UFUNCTION()
	void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

	/** Returns CollisionComp subobject **/
	USphereComponent* GetCollisionComp() const { return CollisionComp; }
	/** Returns ProjectileMovement subobject **/
	UProjectileMovementComponent* GetProjectileMovement() const { return ProjectileMovement; }
	class UNiagaraSystem* NiagaraSystemAsset;

private:
	void BeginPlay();
	class UNiagaraComponent* NiagaraSystemInstance;
};

in .cpp:

#include "FPS_2Projectile.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Components/SphereComponent.h"
#include "NiagaraComponent.h"
#include "NiagaraFunctionLibrary.h"
#include "NiagaraSystem.h"

...
void AFPS_2Projectile::BeginPlay() {
	ParticleSystemInstance = UNiagaraFunctionLibrary::SpawnSystemAttached(
		ParticleSystemAsset, RootComponent, NAME_None,
		FVector::ZeroVector, FRotator::ZeroRotator, FVector::OneVector,
		EAttachLocation::Type::KeepWorldPosition, false,
		ENCPoolMethod::None);
}

but,i see error:

1>  [11/15] FPS_2Character.cpp
1>  [12/15] FPS_2Projectile.cpp
1>D:\Program Files\Epic Games\UE_4.27\Engine\Plugins\FX\Niagara\Source\Niagara\Public\NiagaraTypes.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

how to fix ?

1 Like

Perhaps you’ll need to add it as a public depenency as well?

Try adding

class UNiagaraSystem;
class UNiagaraComponent;

forward declarations in your header.

i did forward declarations in myheader. It does not work.

PS:I compiled successfully with these settings on the ue4.25 version, but not on the 4.27 version.
Who knows what to do on 4.27?

Try add this header:

#include "NiagaraTypes.h"

Also don’t forget UPROPERTY() on any pointer types otherwise you going to get other random issues when running your project.

UPROPERTY() UNiagaraSystem* NiagaraSystemAsset;
UPROPERTY() UNiagaraComponent* NiagaraSystemInstance;