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

Hello,

Is there a way I can call a C++ function through blueprints? I am an artist/developer making a blueprint-only game.

I use mesh distance fields for a couple things, namely DFAO and some water shaders. I occasionally shift the world origin through blueprints, and when I do the distance fields don’t receive the offset. In the image below it looks like there is a simple function that could be called at the same time I shift the world origin, but I don’t know if this is possible to do in blueprints.

Any advice is appreciated, thanks.

You can call any function that has been marked as BlueprintCallable in the code via Blueprints.

The “UpdatePrimitveDistanceFieldSceneData_GameThread” function has not been exposed to blueprints in the code. You can, however write a custom C++ function that calls the function, and expose that function to Blueprints. (You will need C++, though)

This may help you write C++ in a Blueprint project (never tried it, but sounds cool):

But I still do recommend that you make your project a C++ project instead of Blueprint-only, because of little things like this.

1 Like

Hi, I was just looking at that as well. It does also require a C++ project. In that case I think it makes sense just to get a programmer to help me write the function and expose it for use in blueprints.

I’ll have to see if it’s possible to convert a project, or if I would need to make a fresh one and then migrate things over. That’s probably something easy to google though.

It’s certainly possible to turn a Blueprint project into a C++ project in-place (I don’t remember the process, but you could figure it out on these forums).

The function should be so easy to write that you could just ask me :slight_smile:
(once you’re project’s converted).

Thanks NachoMonkey!

Yeah I just skimmed instructions for conversion and it seems simple. I am gonna take a break for rest of day but give this a go tomorrow. Once ready to write that function I will give you a shout, I really appreciate that!

Thinking a little further in advance, I am assuming it is just as simple as when I shift the origin, also call this function and that is it. Hopefully that’s all there is to it.

1 Like

Yep, it should be.


When you do the conversion, please make a backup of your project just in case anything goes wrong (don’t know how something would, you just never know). Also, you’ll end up needing to install Visual Studio and a specific version of the DotNET framework. Once the project is converted, I also recommend you try to package it, just to make sure that it works.

Good call. Yeah I will make a commit to source control before hand. I do have visual studio and the necessary plugins for it already. I did some c++ tutorials a little here and there, but I don’t know enough to do much of anything on my own.

Making a package is good idea to, thanks for the heads up.

1 Like

Okay so I’ve successfully converted the blueprint project into being a c++ project by following the steps listed here: How to Convert a Blueprint Only Project to a C++ Project (using Windows) (allarsblog.com).

I have actually seen how a function can be made blueprint callable before… something to do with adding some UMACRO properties, but there may be more to it than that. I’ll see what I can figure out but if you are still able to help that would be awesome @NachoMonkey2

Yeah, I can help :smiley:

The first step is you’ll want to find the “Create C++ Class” menu. Scroll down and select “Blueprint Function Library”, and in the next page name the class “BPFunctions”. Then tell it to create the class.

1 Like

On creation it is has given me warning here:

“Successfully added class ‘BlueprintFunctions’, however you must recompile the ‘LandNav’ module before it will appear in the Content Browser. Failed to automatically compile the ‘LandNav’ module.”

Not sure if that is important right now or not but I was a little unsure if I should leave this new class in its default directory which was not in the content directory but in the source.

Is “LandNav” the name of your game, or is it a plugin? Whatever it is, it failed to compile and there should be errors in the Content Browser.

All of your game’s C++ code belongs in the Source folder. The Content folder is only for uassets.

1 Like

Landnav is the title of the project.

Errors in the Content browser would show in the output log? I combed through it but dont see any errors related to those classes.

Ok, try building (compiling) through Visual Studio. The errors should be obvious there.

oh you know what, i think maybe it couldnt recompile because visual studio wasn’t open.

That shouldn’t be a factor. It still runs VS to compile in the background.

Okay so I right click the main project called Landnav and hit build. It took awhile but succeeded with no errors or warnings:

Right now I’m looking into how to use the UpdatePrimitiveDistanceFieldSceneData_GameThread function… it looks like it only works on one component at a time. Do you have a plan for it? Or are you going to want to run it on all primitive components in the entire scene?

The problem is that when I change the world origin, that also moves the mesh distance fields. Just going off of the post I screenshotted above, it sounded like that guy was suggesting that by running that function when doing the origin shift, that would also apply the new offset to the mesh distance fields.

I had to look up what primitive component means. So it sounds like the mesh distance fields would be considered one primitive component? And thus we could point to that, and basically say, “you also should receive the origin shift offset like all the other actors in the scene do.”

There is probably a bit I don’t understand about what the origin shifting is actually doing… I’ll paste the blueprint I use to do that in a sec…

A PrimitiveComponent is basically any component that can be rendered/have collision/physics.

1 Like


Just for reference