Call a C++ function through blueprints? / Update Mesh Distance Fields with origin shifting

Gotcha.

So I am more or less guessing and I could be way off, but what I believe is happening is that all actors in the scene get effected by the Set World Origin Location when it runs, but the Mesh Distance Fields are ignored.

That’s what it looks like is happening - for instance where water shader colliders with nearby geometry it gets a foam effect, and if i call this Set World Origin Location repeatedly as I move the character the foam part of the water shader keeps moving along with my character. (and the water shader is also depending on mesh distance fields to draw that foam)

Ok… So it sounds like we’re going to be calling UpdatePrimitiveDistanceFieldSceneData_GameThread on every primitive component in the scene.

1 Like

Ok–here goes :astonished:
All I know is that it will compile and not crash (for me).

In the file BlueprintFunctions.h, between lines 16 and 17, put:

    public:
    UFUNCTION(BlueprintCallable)
    static void UpdateDistanceFields();

And, add this to the end of BlueprintFunctions.cpp:

void UBlueprintFunctions::UpdateDistanceFields() {
        ForEachObjectOfClass(UPrimitiveComponent::StaticClass(),
                [](UObject* Object)
                {
                        UPrimitiveComponent* Comp = Cast<UPrimitiveComponent>(Object);
                        if (IsValid(Comp) && Comp->IsRegistered()) {
                                Comp->GetScene()->UpdatePrimitiveDistanceFieldSceneData_GameThread(Comp);
                        }
                }
        ,true, RF_ClassDefaultObject, EInternalObjectFlags::None);
}

After compiling, there should be a new blueprint node called “UpdateDistanceFields”.

Hope it works

1 Like

Awesome, thank you so much. You are a knight in shining armor.

To make sure I understand correctly, now that I have modified these files, I only need to build them individually then I can test in engine. I don’t need to build the entire project, right?

1 Like

I don’t entirely understand you’re question…

To compile, you can either build in VS by pressing CTRL+B, or through the UE Editor by pressing the big “Compile” button. It will automatically compile the modified files, and the changes will be present even in the editor. When you package the project, those files will be compiled automatically again.

1 Like

image

I think this red squiggle on the BlueprintFunctions.h file might be preventing me from finding the node in blueprints. I don’t understand why an #include of the file is included inside itself?

It’s not including itself… It’s including an automatically generated header file made by the Unreal Header Tool. That file is supposed to contain the information marked by the UCLASS, GENERATED_BODY, and UFUNCTION macros.

So… did it compile? The red squiggle shouldn’t be a problem because that file hasn’t been generated yet. Just like this guy says:

1 Like

I think it did but i am unfamiliar with compile/build. I don’t understand if this is the same term and what excatly the order of operations needs to be.

I was looking up how to include this header but it sounds like you are saying it’s not necessary to do.

So what I have done so far was right click the project file and click build. No errors there. Back inside Unreal, I can’t find the new node though through search.

Yeah, sometimes the Editor doesn’t “refresh the code” properly. Try restarting the editor.

After restarting I still cant find it. Tried taking off context sensitive and also looking through list with eyes too, not only text search.

If it were there, it would be under “Class”->“Blueprint Functions”. Could you try finding it in something like a Level Blueprint, just in case? Sometimes you can’t find certain nodes in funky blueprints.

Yeah nothing in level blueprint and that class is missing:
image

I am thinking it must be related to this problem of the header file not being included. I saw some instruction for manually adding a file to the include list… is that worth trying? There is also the factor of converting this to a c++ project from blueprints… perhaps something that can go wrong there?

Which header file are you concerned about? BlueprintFunctions.generated.h is included by BlueprintFunctions.h, which is included by BlueprintFunctions.cpp. BlueprintFunctions.cpp was compiled according to your screenshot.

That’s starting to sound like the only option.

Try compiling again. Maybe post a screenshot of the output?

1 Like

Just to be clear, this highlighted file is the one I right click and then build:
image

and after doing so, here is the output:

You should try using the Compile button in the Editor, just in case. If you’re using UE4, it’ll be a big cube button on the toolbar, on UE5 it’s at the bottom of the screen.

Yeah I tried restart editor again and compile, still that class is not appearing. Perhaps I should make a fresh one, just to test and see that I can actually make a class and expose it period.

:neutral_face:

One thing might be important: when creating the class, I had not set to public or private. By default does that mean it is private? Is that important?

No, it’s not important… The public/private thing is just basically a way to organize your code. Not picking any should just work.