[4.6.1] Writable reference to self

Hello peeps,

I’m using blueprints and have come across a situation where I can’t feed a reference to self in as input to a function.

function takes an object of type BP_A for example, as a reference. Inside of a BP_A instance, I try to call this function sending in “self”, but error I get is:

“Cannot pass a read-only variable to a reference parameter”

I’d like to know if there is a way to pass “self” in to a function as a reference. If we can’t at moment, then I think this is a bug, because you can achieve same thing by having a public variable in blueprint function is in of type BP_A, and setting this variable to “self” before calling function. Then function uses that variable instead of an input. It’s quite a cruddy work around :frowning:

So if anyone knows how I can send “self” in as a reference to a function, please let me know! Also, forum link

Happy Holidays!

Hey MKII,

I wasn’t able to reproduce behavior you’ve described, but I’d like to make sure I’m understanding you correctly. In my setup, I have two Blueprints, ClassA and ClassB. ClassA has a function, F, with an Input that takes an Object Reference of ClassB. In ClassB, I have a variable of type ClassA, from which I call function F. ClassB Input for function F accepts a reference to Self:

24896-classb.png

Is your setup different? If so, could you provide a screenshot? Thanks!

Huh, my setup is same, except ClassBVar pin is a diamond (i.e. a reference):

It turns out that you can bypass this by casting self to whatever object self is, and passing that in:

Cheers to help from forum, and thanks for reply - my problem is solved with workaround, but if you need more help reproducing compiler error, let me know!

Hey MkII,

Ah, I didn’t know that you were using a Pass-by-Reference input. It seems that reference to Self is not being recognized as a suitable reference. I’m not certain whether this is expected behavior or not, so I have entered a bug report for developers to consider (UE-7077). I’ll let you know what they say. For now, casting to desired class is a good workaround. Thanks for report!

Hi there!

This behavior is by design. However, it needs some clarification and perhaps we need more of a visual indication in editor of compile-time incompatibility between pin types.

First to clarify, in a Blueprint, all object types are considered references (pointers in C++ terms) and are passed around as such (i.e. object pointer is passed by value, but object instance itself isn’t getting copied or anything like that). Thus, it’s not necessary to pass an object type by reference unless you have some specific need to potentially change object that it’s referencing from inside function.

‘self’ node returns an implicit “literal” value, which FWIW equates to value of ‘this’ in C++ at runtime. A pass-by-reference input means that node can potentially also set input variable’s value to something else. However, you (or a function) are not allowed to change read-only ‘self’ reference (just like you can’t reassign ‘this’ pointer in C++), so connection will result in a compile error (as is noted here).

Conversely, using a cast node will, at compile time, create a (hidden) local temporary variable on output pin to receive value of ‘self’ reference cast to given type. Since it’s just a local term, its value is allowed to change, and thus it can be passed by reference.

To connect ‘self’ directly, consider removing pass-by-reference attribute from input, as it should not be needed there (unless you have some specific reason for it).

Hope this helps!

Oh man, that makes so much sense! I didn’t realise objects were always passed as pointers - you’ve just taken a load off my mind since I’d assumed I was copying them whenever I forgot to pass by reference!

Thanks very much for clarification :smiley: