Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

pass user parameters to niagara from c++

  1. User

    pass user parameters to niagara from c++

  2. Assistant

    You can pass user parameters to Niagara from C++ by creating a custom user parameter struct and binding it with a custom Niagara Function Library. Here's a step-by-step guide on how to do this:

    1. First, create a custom user parameter struct that inherits from FNiagaraUserParameterBinding. This struct should contain the properties you want to pass to Niagara:
    // MyNiagaraCustomParameters.h
    
    #pragma once
    #include "CoreMinimal.h"
    #include "NiagaraCommon.h"
    #include "NiagaraUserRedirectionParameterStore.h"
    #include "MyNiagaraCustomParameters.generated.h"
    
    USTRUCT()
    struct FMyNiagaraCustomParameters : public FNiagaraUserParameterBinding
    {
        GENERATED_BODY()
    
        UPROPERTY(EditAnywhere, Category = "My Parameters")
        float MyCustomFloat = 0.0f;
    
        // Add more properties here if you want to pass additional parameters to Niagara
    };
    
    1. Next, create a custom Niagara Function Library to define the functions that will use these custom parameters. In the header file, you should inherit from UNiagaraFunctionLibrary and declare your custom functions:
    // MyNiagaraFunctionLibrary.h
    
    #pragma once
    #include "CoreMinimal.h"
    #include "MyNiagaraCustomParameters.h" 
    #include "Kismet/BlueprintFunctionLibrary.h"
    #include "NiagaraFunctionLibrary.h"
    #include "MyNiagaraFunctionLibrary.generated.h"
    
    UCLASS()
    class UMyNiagaraFunctionLibrary : public UNiagaraFunctionLibrary
    {
        GENERATED_BODY()
    
    public:
        // Declare your custom functions here
        UFUNCTION(BlueprintCallable, Category = "My Niagara Functions")
        static void SetMyCustomFloatParameter(UNiagaraComponent* NiagaraComponent, FMyNiagaraCustomParameters CustomParameters);
    };
    
    1. Implement your custom Niagara Function Library functions in the implementation file. You can apply your custom parameters by modifying the component's UserRedirectionParameterStore:
    // MyNiagaraFunctionLibrary.cpp
    
    #include "MyNiagaraFunctionLibrary.h"
    #include "NiagaraComponent.h"
    #include "NiagaraDataInterface.h"
    #include "NiagaraTypes.h"
    
    void UMyNiagaraFunctionLibrary::SetMyCustomFloatParameter(UNiagaraComponent* NiagaraComponent, FMyNiagaraCustomParameters CustomParameters)
    {
        if (NiagaraComponent)
        {
            // Get the UserRedirectionParameterStore from the Niagara component
            FNiagaraUserRedirectionParameterStore& UserParameterStore = NiagaraComponent->GetUserRedirectionParameterStore();
    
            // Assign your custom parameters to the corresponding Niagara variables
            UserParameterStore.SetParameterValue(CustomParameters.MyCustomFloat, CustomParameters.Parameter, false);
            
            // Ensure the component is updated with the new parameter values
            NiagaraComponent->ReinitializeSystem();
        }
    }
    
    1. Now you can call the custom function from your other C++ classes and Blueprints to pass user parameters to a Niagara component:
    // Example usage from a C++ class
    UNiagaraComponent* NiagaraComponent; // Assume you have a reference to a Niagara component
    FMyNiagaraCustomParameters CustomParameters;
    CustomParameters.MyCustomFloat = 123.0f;
    
    UMyNiagaraFunctionLibrary::SetMyCustomFloatParameter(NiagaraComponent, CustomParameters);
    

    Now you can pass your custom user parameters to the Niagara component from C++.

  3. User

    How would you do it with having a UNiagaraSystem?

  4. Assistant

    If you have a reference to a UNiagaraSystem, you can create a UNiagaraComponent and assign that system to the component. After that, you can pass user parameters to the Niagara component, as we did earlier. Here's an example:

    1. Assuming you already have your custom parameter struct FMyNiagaraCustomParameters and custom Niagara Function Library UMyNiagaraFunctionLibrary, as shown in the previous answer.

    2. In the C++ class where you want to use the UNiagaraSystem, create and initialize a UNiagaraComponent and assign the system to this component:

    // Example usage from a C++ class with a UNiagaraSystem reference
    
    #include "MyNiagaraFunctionLibrary.h" // include your custom Niagara Function Library header
    #include "NiagaraSystem.h"
    #include "NiagaraComponent.h"
    #include "Engine/World.h"
    #include "Components/SceneComponent.h"
    
    // ...
    
    UFUNCTION()
    void MyExampleFunction()
    {
        UNiagaraSystem* NiagaraSystem; // Assume you have a reference to a Niagara system
        USceneComponent* AttachComponent; // Assume you have a scene component to attach the Niagara component
    
        // Create and initialize Niagara component
        UNiagaraComponent* NiagaraComponent = NewObject<UNiagaraComponent>(this);
        NiagaraComponent->SetAsset(NiagaraSystem);
        NiagaraComponent->SetAutoDestroy(true); // Set AutoDestroy, if required
        NiagaraComponent->RegisterComponentWithWorld(GetWorld());
        NiagaraComponent->AttachToComponent(AttachComponent, FAttachmentTransformRules::KeepRelativeTransform);
        
        // Activate the Niagara system
        NiagaraComponent->ActivateSystem();
    
        // Set the custom parameter values
        FMyNiagaraCustomParameters CustomParameters;
        CustomParameters.MyCustomFloat = 123.0f;
    
        // Call the custom Niagara function to set the user parameters
        UMyNiagaraFunctionLibrary::SetMyCustomFloatParameter(NiagaraComponent, CustomParameters);
    }
    

    This creates a UNiagaraComponent with the given UNiagaraSystem, attaches it to a scene component (if required), and sets the custom parameter values using the custom Niagara Function Library.

  5. User

    What if I set my NiagaraSystem from the blueprint, how do I get my component from it?

  6. Assistant

    If you set your UNiagaraSystem from a Blueprint, you can define a UPROPERTY in your C++ class to hold a reference to the Niagara System. That way, when the Blueprint assigns the Niagara System, you will have access to it in your C++ code. Here's an example:

    1. Create a UPROPERTY in your C++ class to hold a reference to the UNiagaraSystem:
    // MyActor.h
    
    #pragma once
    #include "CoreMinimal.h"
    #include "GameFramework/Actor.h"
    #include "NiagaraSystem.h"
    #include "MyActor.generated.h"
    
    UCLASS()
    class AMyActor : public AActor
    {
        GENERATED_BODY()
    
    public:
        // Add a UPROPERTY to hold a reference to the Niagara System
        UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Niagara System")
        UNiagaraSystem* NiagaraSystemReference;
    
        // ...
    };
    
    1. In your Blueprint based on your created C++ class, assign the desired Niagara System to the NiagaraSystemReference property.

    2. To create a Niagara component from the Niagara System and utilize user parameters, follow the same steps as in the previous example:

    // MyActor.cpp
    
    #include "MyActor.h"
    #include "MyNiagaraFunctionLibrary.h" // include your custom Niagara Function Library header
    #include "NiagaraComponent.h"
    #include "Engine/World.h"
    #include "Components/SceneComponent.h"
    
    // ...
    
    UFUNCTION()
    void MyExampleFunction()
    {
        if (NiagaraSystemReference == nullptr)
        {
            // Ensure the Niagara System reference is set
            UE_LOG(LogTemp, Warning, TEXT("NiagaraSystemReference is not set."));
            return;
        }
    
        // Create and initialize Niagara component using the NiagaraSystemReference
        UNiagaraComponent* NiagaraComponent = NewObject<UNiagaraComponent>(this);
        NiagaraComponent->SetAsset(NiagaraSystemReference);
        NiagaraComponent->SetAutoDestroy(true); // Set AutoDestroy, if required
        NiagaraComponent->RegisterComponentWithWorld(GetWorld());
        NiagaraComponent->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
    
        // Activate the Niagara system
        NiagaraComponent->ActivateSystem();
    
        // Set the custom parameter values
        FMyNiagaraCustomParameters CustomParameters;
        CustomParameters.MyCustomFloat = 123.0f;
    
        // Call the custom Niagara function to set the user parameters
        UMyNiagaraFunctionLibrary::SetMyCustomFloatParameter(NiagaraComponent, CustomParameters);
    }
    

    Now, when you call MyExampleFunction(), it will create a UNiagaraComponent with the Niagara System assigned from the Blueprint, attach it to the root component of the actor, and set the custom user parameters.

  7. User

    SetMyCustomFloatParameter doesn't exist in the latest version of UE. Where is it?

  8. Assistant

    I apologize for the confusion. The SetMyCustomFloatParameter function is a custom function that was defined in the previous explanations. It is not part of Unreal Engine's API.

    To clarify, SetMyCustomFloatParameter is a custom function within a custom function library named UMyNiagaraFunctionLibrary. You need to create this library yourself using the provided code samples in the previous explanations. Once the custom function library is created, you can use the custom SetMyCustomFloatParameter function to set your user parameters in your Niagara component.

    Here are the header and implementation files for creating the custom Niagara Function Library:

    MyNiagaraFunctionLibrary.h:

    // MyNiagaraFunctionLibrary.h
    
    #pragma once
    #include "CoreMinimal.h"
    #include "MyNiagaraCustomParameters.h" 
    #include "Kismet/BlueprintFunctionLibrary.h"
    #include "NiagaraFunctionLibrary.h"
    #include "MyNiagaraFunctionLibrary.generated.h"
    
    UCLASS()
    class UMyNiagaraFunctionLibrary : public UNiagaraFunctionLibrary
    {
        GENERATED_BODY()
    
    public:
        // Declare your custom functions here
        UFUNCTION(BlueprintCallable, Category = "My Niagara Functions")
        static void SetMyCustomFloatParameter(UNiagaraComponent* NiagaraComponent, FMyNiagaraCustomParameters CustomParameters);
    };
    

    MyNiagaraFunctionLibrary.cpp:

    // MyNiagaraFunctionLibrary.cpp
    
    #include "MyNiagaraFunctionLibrary.h"
    #include "NiagaraComponent.h"
    #include "NiagaraDataInterface.h"
    #include "NiagaraTypes.h"
    
    void UMyNiagaraFunctionLibrary::SetMyCustomFloatParameter(UNiagaraComponent* NiagaraComponent, FMyNiagaraCustomParameters CustomParameters)
    {
        if (NiagaraComponent)
        {
            // Get the UserRedirectionParameterStore from the Niagara component
            FNiagaraUserRedirectionParameterStore& UserParameterStore = NiagaraComponent->GetUserRedirectionParameterStore();
    
            // Assign your custom parameters to the corresponding Niagara variables
            UserParameterStore.SetParameterValue(CustomParameters.MyCustomFloat, CustomParameters.Parameter, false);
            
            // Ensure the component is updated with the new parameter values
            NiagaraComponent->ReinitializeSystem();
        }
    }
    

    Once you have created the custom function library with the provided code samples, you will have a method named SetMyCustomFloatParameter as part of your own custom library for setting user parameters in Niagara components.

  9. User

    How Can I get the uniagaracomponent to terminate after using it once?

  10. Assistant

    To terminate the UNiagaraComponent after using it once or after a single loop of the particle system, you can set the bAutoDestroy property of the component to true and ensure the Niagara System has System Lifecycle Mode set to Complete.

    Here's a step-by-step process:

    1. In the C++ code where you create and initialize the UNiagaraComponent, set bAutoDestroy to true:
    UNiagaraComponent* NiagaraComponent = NewObject<UNiagaraComponent>(this);
    NiagaraComponent->SetAsset(NiagaraSystemReference);
    NiagaraComponent->SetAutoDestroy(true); // Set AutoDestroy
    NiagaraComponent->RegisterComponentWithWorld(GetWorld());
    NiagaraComponent->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
    NiagaraComponent->ActivateSystem();
    
    1. Open your UNiagaraSystem in the Niagara Editor. In the Emitter tab, click on the emitter and go to the Emitter Update script. In the script, you should see a "Complete" mode node, which indicates that the emitter ends after a single loop.

    2. Go to the System tab and click on your UNiagaraSystem. In the Details panel, find the System Lifecycle Mode property and set it to Complete. This ensures that the system terminates after playing the emitter(s) once.

    Now, when the particle system plays once, the UNiagaraComponent will automatically be destroyed because of the bAutoDestroy property. The Niagara System and emitter(s) are configured to terminate after a single loop, which prevents it from looping indefinitely.

    Remember to save your Niagara System and re-compile it before testing this in your game.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.