Passing Array by reference to a function

I’m an experienced programmer, but this one has me stuck. If I pass an array, or a struct that holds an array by reference in to a function then manipulate the array (adding data to it) that data sticks and I can access it throughout the rest of the function.

When the function returns my array is now back to being empty. I can understand how this would happen if I was passing by value (copy) as the changes are dumped when the local functions stack is dumped… but… this is now working how I expect it would have for a reference.

I’ve read dozen of pages off google in the past hour and discovered nothing that helps me understand how to have functions that manipulate data.

( I even confused myself there, gimme a moment… )

It passes back a copy, that’s what’s going on :slight_smile:

If you do this

You basically have 2 copies of the array.

So you have to say

image

Or, use a custom event.

1 Like

I’m ashamed that i didn’t follow your suggestion. I’ll admit this is the first time I’ve used Pass-By-Reference with functions, and that I just noticed something:

So, in this function I’m loading a bunch of actors out of a save file (It’s basically a Rust looking building I saved as a prefab). All those BuildParts are individual actors. I need to capture those somewhere I can later access.

So, you see that before I clicked array I enabled ‘Pass-by-reference’. When I selected the array, the box greyed out. About 5-6 years ago forum chatter suggested that this was not supported.

I have no outputs BTW because I assumed that I was writing to the array I passed in by ref. What I get is say 20 array elements, then as soon at the function returns, it’s back to zero.

I’m not passing it back out, mostly because I know that a reference to anything in a function is invalidated the second the functions returns.

Interestingly I can pass in a struct, by ref, get the diamond, edit the array in the struct, and exit the function , array is empty.

Or… perhaps I completely missed your well presented point.

1 Like

I can’t get anything except greyed out ‘by ref’.

I don’t get how you’re setting things, as this works

image

What engine version is this?

It’s 5.3. I’m trying to solve this in place, inside my operating code, but I think I need to spin up a clean project with a simple example to remove any other possible factors. Then I can test more things without trying to restrict the mess I’m making.

I also need to try this with primitive data types. int’s, etc as they are not subject to garbage collection or anything else stepping in. 24 hours however. Thankyou so far…

(I really thought I would be told. this is the most basic thing, and clearly you didn’t read the documentation… lol.)

1 Like

It’s a little strange as, of course, you can return a pointer from a function in C.

It would just be a pointer to the original structure.

Yeah… I’ve got about 20 years of c++ behind me. My understanding is that Blueprint ‘Arrays’ are actually more similar to STL lists than vectors and an Object Reference is actually a Smart Pointer. At some point I’ll have to stop sticking to 100% to blueprints, but… until I want to do something hard, like 3 dimensional navigation and map everything in to an octtree and fit the centre points to a bspline I’ll keep resisting. Year ago I sank 60,000 lines of code in to my own engine… and it was wonderful code… just did nothing useful. I had no ‘game’ just an over polished half engine with approaches to things I still miss :slight_smile: … so, this time… discipline, I’ll do this the unreal way rather than my way. :wink:

1 Like

Yes, I also have many years of language experience. But I want game dev to be playing, and BP is playing. CPP is not :slight_smile:

Just write the other half of your engine and try and draw some Unity users over :rofl:

Looks like we’re quite similar. I had a bit of a problem with my old codebase. It was a source of pride like you have for your own kids. I cared over every row it had so many cool things that even unreal doesn’t even have. It had a fully integrated voxel engine even back in 2003, streamed and chunked, saved in binary as a raw minimum depth octree format, My data was streamed as XML and parsed by of course my own customer parser, plugged in to a templatised abstract factory so new ‘actors’ were created directly from the loader, all in a hierarchy I could serialize back to save file by a single recursive call. Model loaders read in lightwave save files directly, OGL\D3D\cross platform, etc, etc. An awesome boat anchor that prevented me from ever getting to actually making a game, so, when 2 years ago I found the old NAS with dead drives… I was released with the decades old codebase. So yeah… blueprints from here on out!

1 Like

I know what you mean. Writing code ( any kind ), feels like you’re getting stuff done, but it can also be procrastination :rofl:

I’ve had time to sit down and recreate an empty project bare bones test case and unreal DID do what I think it should. That means my game codebase has something else going on. Apologies for the time wasted.

1 Like

Do you have the same problem I do?I’ve encountered an issue where, after passing my array through a function (the function simply passes the array), the output array is no longer a reference to the original array but a copy.

Sorry for not getting back to you. I did however find a solution. I cloned the for each macro and made a new one called ForEachByRef and then changed all the outputs to be references. I then put that in a macro library.
The solution beginning to end including research took only an hour and was well worth it. That was also my first blueprint macro so it should be achievable.
The only hard part was I needed tow of them. One was in a library as an ‘Actor’ the other as a ‘widget’. Same code copy\paste.