for BP functions, is there a way to make a function auto ref self in the context of the bp it is located in? As my current work around I made an input pin for self and when I call the function then I attach the self ref.

So to be more clear I made a custom print string function in a function library, and I to place this function in a bp of any actor and will print what I want in additional to the actor’s name in the world.
But I kept getting compile error and thus my current work around is add an input pin and when I call the custom print string function I then call the self ref and attach it to the pin.

I’m asking is there a world context based self ref or something as make my workflow easier?

Hey @Hero_Tales!

Have you tried “GetOwner” and things of that nature? Can you not add a “Get ref to self” on the Blueprint Function Library, and that’s the issue?

Get back to us! Interested in helping with this solution!:slight_smile:

If you set up a function library you could make a global function that returns a reference to self on any given actor you like

if I use ‘ref self’ in a function within a function library and compile I get a warning stating: ‘Self node Self-Reference cannot be used in a static function.’

But how do you do that exactly in blueprints?

I think I understand what you’re trying to achieve, but let me know if I’m wrong. You essentially want a print function where you just input a custom message and it automatically prepends the source Actor’s name to it, and you’d like a workflow where you only have to input the message and not worry about the Actor reference.

If that’s correct, then I can help you. I don’t know if you’re writing your Blueprint library in C++ or Blueprint, so here are two example functions in either:

(Let’s ignore the specific implementation details and focus on the usage.)

Now, since these are static functions, they will always need an input for the SourceActor pointer. There’s no way around this, but we can still achieve the easier workflow you want via Blueprint Macros. Make a Blueprint Macro library, have it inherit from Actor, and then you can use the following two macros:
image
image

The key thing to note is that the macros have just one input pin for the message string and internally it automatically populates the SourceActor input with a reference to self. Since macros expand at the site where they are used, this will always be the reference to the actor whose event graph is using the macro.

Using it in an actor looks like this:

2 Likes

In your content browser, id recommended in a “blueprints” folder, right click and search “function library” this will allow you to create global functions you can call anywhere

yes you understand my issue. Interesting idea to use a Macro Library and have the self ref there. However, personally this macro library method still falls apart as I mainly use this code in widgets which do no inherit macro libraries sadly. But if use it only blueprints will make sure.

1 Like

If you’re talking about UserWidgets, you can make this work for them as well. If you’re talking about Slate, then this won’t work. We can make this strategy work for all UObjects as long as you’re just needing GetDisplayName or GetObjectName since both of those are from the UObject interface.

(If you needed specific functionality from Widgets you could always make a specialized version that deals with UUserWidgets / inherits from UserWidget).


image

Just change the input type for Source Object to Object reference.

When making your Macro library, click on the drop down for All Classes and select Object:
image

image
There’s an extra pin for World Context object, but self should work fine for that as well in most cases.

image
Now we can use this for anything that inherits from UObject, including UUserWidgets.

Hope that helps.