Custom K2 Node: How to Cast the Return Type?

Goal/Description

I’m implementing a custom K2 blueprint node, to help simplify workflow for designers. This is broadly what I’m trying to emulate (but with a few extra steps):

The goal is to compress the following steps into a single blueprint node:

  1. Fetch a component pointer by UClass

  2. Check if the component is valid, then either:

  • Cast the component to the correct class
  • Hide the output component pin entirely

It’s an “it’s always there, or it’s never there” sort of situation, hence why I’d like to remove the pins if possible. Here is a few examples of what I mean by hiding certain outputs based on input:

I think I might be about 80% of the way to getting it working.

Problem

This is what the broken custom nodes look like right now when I hook it up to an editor graph:

The “Bind Event to On World Interaction” node is a member of the InteractableComponent

The preceding “Get Trait Component” should be returning an InteractableComponent object reference.

The K2 node I’ve implemented is correctly showing the user that it is trying to output an InteractableComponent, which is reflected by both the tooltip and the fact that you are able to draw a spline between the “Get Trait Component” and “Bind Event” nodes.

However, the underlying C++ function that implements this node returns a Base Component object reference instead. Which understandably explains my compile error in the blueprint graph:

My best guess is that I need to modify my implementation of UK2Node::ExpandNode to add another intermediary node to cast the return type from my C++ function to the correct type I wish to output to the user.

I’ve had a look a the implementation for the UK2Node_SpawnActorFromClass type, which contains the exact return value behaviour I’m trying to emulate – I just haven’t figured out which parts from it I’m missing at the moment.

If anyone could point me in the right direction, that would be a big help!

Fixed it!

All I had to do was get the return pin from the UK2_CallFunction intermediate node I created, then change the pin type to match the pin type of the output pin of my custom K2 node

2 Likes