Custom Bluprint nodes with C++ function code in-editor :-o

I’ve wished for exactly this myself! In my opinion it would be a better use of resources than to implement a separate scripting language. It would not fragment the user base but bring C++ and Blueprint even closer together.

Tim, please make it happen!

I second this, it would be awesome to inline C++ in places where it’s cleaner in code or performance is critical.

I would be carefull with expectations to performance. It could be the same effect that sometimes happens when you use inline assembler.
The compiler has to manage around your machine code and some optimizations are now impossible and you lose more performance than you gain by your asm magic.
It is really difficult to beat modern compilers :slight_smile:

2 years later, I still wish this feature existed

We really need something like this. There have been several instances where I refactored blueprints into C++ because the if-statement tree I was using or some kind of complex loop iteration proved to be far more cumbersome in blueprints. Blueprints are nice when you have a simple sequence of function calls going on, but things like mathematical arithmetic or more complex algorithms need to be done in actual code, and it would be nice if the two can easily be converted.

Hi @GhostRavenstorm
I think we are closer now to see this implemented this way if they decide to do it.
With the last update (4.15), we can officially compile blueprint as native code just setting a checkbox, so we have the performance gain.
Someone could say that the original request on this thread is no longer necessary, but you give a good reason to still make this request useful.
For people that actually know how to code in C++ will be very handy to write some code in C++ inside some blueprint nodes without having
to go to Visual Studio; in some cases could be even easier, cleaner and faster than doing “everything blueprint” or “everything c++”.

Upcoming *hackathon *I gonna have some fun :smiley:

Well ain’t that a b**** of a tease. :smiley:

That would be awesome.

I truly wish UE4 came with its own compiler in addition to code blueprint nodes. It would then become an ultimate tool.

Hmm… So, apparently it’s possible to do it.
I’ve just packaged a game and ran it printing the script’s result:

What I did:

  • Custom blueprint skeleton which stores the script while in Editor ( UBlueprint* class )
  • A shiTon of custom K2Node_ classes I’ve created to integrate code into the Blueprint Graph
  • The custom node parses the script, generates “tokens” and then converts the script to Blueprint functions.
  • Unreal do its thing as usual as if everything were made with nodes (in fact it doesnt even know the script exists).
  • So from there it runs as a normal blueprint function which can be nativized to C++ as well.

What I didn’t do:

  • VMs… No additional Virtual Machines such as Python’s running attached to the game. Script tested was “compiled” to auto generated blueprint functions.
  • A “real” version of this parser will require a database, maybe a big dummy UObject, mapping UFunctions to script keywords. I’m not even close from doing that lol.

Big challenge:

  • Next step would be auto generation of Pins from variables declared within the script. This is a hell of a complex task to do without breaking the Blueprint Graph.

I may play some more with this on my free sundays, was fun :slight_smile:

So what you did is effectively create UnrealScript node ;p If you gonna od this you could as well follow old syntax

[USER=“434”]BrUnO XaVIeR[/USER] Wow, that is really cool. Other than being able to write script instead of using pre-made nodes what benefits could it bring? Theoretically could we expose C++ fully to BPs this way without performance hit? (iirc exposing C++ fully to BPs would slow things down). Would love to see where/how far this goes. Please do have more fun! :stuck_out_tongue:

1 Script = 1 Blueprint Function.
That is what Unreal understands after I tell it to delete the script after converting it to a “Graph”…

The “script” only exist in the Editor, so whatever you can do with a Blueprint Function equates to what you could do with this node. That includes calling UFunctions not exposed to Blueprint Graphs.
(UFunctions yes, native C++ functions no)

Unreal has a ton of UFunctions “hidden”, not exposed, to Blueprint Graphs; this could be a way to access them easily.

Also, the “benefit” I dig the most is the possibility of “writing nodes” faster than dragging pins wires around which is sometimes annoying for simple “for loops”, array/set manipulations, etc… Typing words to do things like is simply faster than dragging wires around.

But anyway, I don’t have the human resources to effectively develop something “this big”. A fully working scripting node would require a team working on it full-time, it’s a lot of code to maintain.

I was just curious to see if it would run at all :stuck_out_tongue:
I will learn how to generate node pins extracted from the compiled scripts, after that I am probably done with this!

Thanks for explanation. Calling non-exposed UFunctions is what I meant. And yes, there are times I wish I could write some scripts instead of playing with nodes.

You have probably thought/tried this but you should check out math node to see how they did it. It is kind of similar on the outside at least.

Well now that we know it can be done, we’ll just ask Epic to implement it as a feature, surely they’ll do it :D… oh wait, we don’t even have Quats in BPs by default :confused:. Yes I’m and will forever be salty about that.

Anyways, thanks for your awesome contributions :).

I went full retãrd… Changed the custom K2Node_ system to generate C++ classes out of itself;
Now, making use of Editor’s “hot reload” feature, I’m writing C++ nodes from inside Blueprints and using those nodes to code their own source code… Blueception!

( Game Project’s main Build.cs now gonna need this )

Self-generated Node class:

Runtime result:

But now it only works on projects with C++ source code :stuck_out_tongue: (100% Blueprint projects cannot use hot reload I guess, not sure)

So, I figure out how to generate the pins dynamically from code…
Works well, but there’s two problems:

  • For some reason no matter what I do, default values are ignored.
  • Now because pins when hot reloaded, trying to refresh pointer to the runtime class, I get crash :stuck_out_tongue: probably because it store the new class inside a new DLL.
    https://i.imgur.com/SQVzmbf.gif
    [HR][/HR]

Edit:

Fixed the crash post hot-reload by changing from this:



TWeakObjectPtr<UMagicNodeRuntimeObject>
    RuntimeScriptObject = NewObject<UMagicNodeRuntimeObject>(**this**,Class,*Name,**RF_NoFlags**);


to doing this instead (was just a gamble that ended up well):



TWeakObjectPtr<UMagicNodeRuntimeObject>
    RuntimeScriptObject = NewObject<UMagicNodeRuntimeObject>(**GetTransientPackage()**,Class,*Name,**(RF_Transient & RF_Dynamic)**);


Packaged game now is happy:

And now, after 5 ~ 9 seconds of hot reload, when I click the “refresh” button on script panel, the new variables from script are loaded as pin while old pins have their values kept intact…

I now consider this experiment a functional success :stuck_out_tongue:

https://i.imgur.com/qwtVbsd.png

2 Likes

Improve in-node code editor a bit and have setup a “script parser” (it takes your code in node editor and generates a C++ class out of it that later can be compiled by the Engine, instead of running yet another virtual machine which slows things down):

2 Likes

How does it work with referencing Blueprint Variables?

You declare variables in the body of the node’s Execute function; (then compile)

The system transforms the declaration into “pins”…
Code away using the variables declared in function body, when the node is executed by the Blueprint the value of variable in function body is whatever value you have wired to the generated pin.

It will take the value of pins in Blueprint node and send to the C++ function written by you, automatically.

I think it’s an interesting way to learn C++, no need to setup class files, modules, project files in visual studio, etc :slight_smile: