Get (a ref) in Array of UObjects altered to Get (a copy)

Yeah, it works fine for primitive types and structures. But I talk about another thing: you literraly can’t use Get (a ref) node, Editor doesn’t allow it (or replace it to Get (a copy) if you try).
And I’m hardly believe that any “Pointer types” can return only a pointer.

So I also find this semantic to be problematic. I am making an array of player controllers so that I may call methods from any or all of them from the server. This does not necessarily work if I obtain a copy of the player controller. I am not necessarily making changes to the pointer but I can call events and functions which I need to make sure happen to the player controller within the array. I guess for now as the question states " I have to create even more stupid duct tapes to work it out (some of them cause undefined behavior)."

Beacuse I don’t want to use OOP when none one needs it, i.e., using mutators all over. For example, I have to do the exact operation over the array of objects (say, some advanced HUD parameters), to be more flexible I’d defined some public math operations so anyone (any object in the game) could change the specific HUD-parameter. But I cannot get a reference to my parameters since (in BP), and that leads to creating multiple separeted copies. Why do I need it?..

After update 4.16 I’ve faced with stupid problem that I can’t get a reference of an array of Objects. So I have to create even more stupid duct tapes to work it out (some of them cause undefined behaviour).

To reproduce the issue:

  1. Open UE4 Editor.
  2. Create any BP.
  3. Create a variable inside of it.
  4. Make the variable array of Objects (Reference).
  5. Try to use node Get (a ref) on this variable.
  6. Get an altered node (a copy) with the notification about it.

Little investigation.

I am not sure, but it seems it somehow connected with C++ implementation of TArray. Because when I use direct getter inside (in BP) of a custom ActorComponent ( ArrayVarType* GetArrayVarElem(int32 Index) { return ArrayVar[Index]; } ), with PrintString node see two values instead of one if I put it right after EventTick node. What completely creeps me out is that GetObjectName returns different values (a reference OR a copy) on different sessions (Alt + P in Editor), i.e., my getter returns not only the reference, but somehow a copy as well. One of my C++ logs also got mad, showing sometimes copy’s or reference’s value for the element alter each other.

Has anyone faced with such problems? Any advice to debug such behaviour?

Upd. 1: I’ve checked 4.16 release notes, and it is said «can toggle between returning a reference or copy,» which is false, i.e., it looks like a bug.

If you pull a Object out of a array you get a Pointer. Otherwise you would return a Ref to the Pointer and not the actual Value(Object). The Get by Ref works on Value types like you would expect.

Granted the current Get Node description on Pointer types is missleading. Since its a Pointer it will Propagade the changes back to the Array.

Staff-> Fix the tooltip :stuck_out_tongue_winking_eye:

Why would you need a Ref to pointer? Would only make sense if you want to change the Pointer itself.

And remeber BP are also made for none Programmers and thats a good source for Bugs. What other Potential Bugs that might bring with it like screwing with the Garbage Reflection or Property Reflection System I don´t really want to think about =)

Its by Design thats why you also get a nice Notification.

I distinctly remember using Get actors of class, then getting the very first from that array we got from said function. Hmm.
As in not a copy but an actual reference to that actor. Which seems logical. Or more like if I want to do it, the editor should just do it and stop being a pain with all it’s “I know better than you” ideas.

Oh well, i just wrote my own GetArrayItem function with a ForEachLoopWithBreak.Definitely worse than having a proper GetREF function but I guess that’s all I have right now.

Better than to leave the question unanswered if we have a solution. I for one am glad since this is useful for me too.
It does annoy me that it’s in such a location that I could have noticed it with a bit more luck…

I know this is an old question but for anyone still looking for the answer to this now like I was, if you pull out the Get (a copy) node from the array and right click on it, there is an option in the dropdown menu for “Change to return a reference”.

The silly thing is, the only way to know this has been converted to a reference (from my use in 4.21) is to right click again and see that option greyed out, as when selected, it doesn’t seem to change the node itself, and the tooltip for the get (a copy) still appears when hovered. But testing it in practice, it did seem to alter the item itself, rather than a copy, so I hope this is the answer you were looking for.

1 Like

I will check it once I’m in the Editor. Thank you.
Though, it’s not a big issue for me anymore since I’ve switched to C++ completely.

To change an item in an array remotely, don’t use ‘Get’ and try to operate on a generated copy of an item (I mean it feels dirty anyway working on a supposed reference to an item from a floating reference to an array), instead create a function in the object where the array lives which takes in an index and the value you’ll need to change, and call the function from the place you want to change the array from. Use the SetArrayElem node to directly replace specific items in an array - in the object where the array lives so you are not even working on a reference in the first place.

Here is an example of a correct SET method for an item in an array. I call the function from a farm plot somewhere and input a plot number (which is one of 192 plots in the array) and a new value. The game instance function handles the changes to the array because that is where the array lives.

Switching the logic to C++ seems way more pretty.

@Stephanie The Viking: or, you know, set by ref without the blueprint spaghetti:

332113-screenshot-5.jpg

Im having a weird issue - im also very new to UE5 and blueprints so plz excuse if this is obvious

I am trying to get a static mesh components relitive transform from the actor that parents the component I am calling from, the actor has 2 static meshes on it - one is a fruit and one is the bush.

When I connect the transform up to the spawner transform, the static mesh component changes to the wrong one…

What am I doing wrong here?

thanks…