In UE4 blueprints, arrays are handled in an interesting way. If we have an int variable and we wish to add a value to it, we must
(Get int) —> (Add value) —> (Save sum back to variable)
But for an array, we have the “Add” node which (seemingly) takes care of the last part for us.
(Get array) —> (Add element)
So my question is - what is actually happening here? For this scenario, it is obvious. But for others it is not.
I was recently trying to help other user in another post and I came across a complex problem. How can I modify the elements in an array if the array is not a variable? For example, here is the screenshot I had posted.
Just offscreen to the left is the execution node and a reference to “MapIndex”. So for a little bit of an overview…
MapIndex is an array of TileRows
TileRow is a struct containing an array of TileStructs
TileStruct is a struct containing an int (“Type”) and an actor reference (“ActorRef”)
The idea was to create a 2D array where each element in the array has an int to define the type and a reference to the actual actor ingame. I’m not sure if there was a better way to set this up, but that’s not really the point of this post.
What I am doing is looping through each element in the 2D array. If the “Type” is 1, spawn a “Wall” actor. Then, I need to feed a reference to that actor back into that element in the “ActorRef”. The whole bottom half is me trying to accomplish that.
So in the screenshot, the logic is as follows
- Get the main MapIndex array
- Get the index into MapIndex we’re looking at (Outer “ForEach” loop)
- Break the “row” to get the elements within it
- Get the index into TileStructs we’re looking at (Inner “ForEach” loop)
- Break the “tile” to get the elements within it
- Make a new “tile” with the “type” from the old tile and a reference to the newly spawned actor
- Use the broken row from #3, setting the element at the inner loop index to be the newly created tile.
- Make a new row from the broken row.
- Replace the old row with the new row in Map Index
Overall, I HATE the steps I took here. It’s messy and convoluted. I shouldn’t need to replace an entire row of elements every time I want to change a single one. But I don’t know any other way to accomplish it. To make matters worse, this doesn’t even work.
Here’s where my confusion starts.
In step 7, I link “TileStructs” from step 3 into the “Set Array Element”. When this executes, what exactly has changed? In step 8, I make a TileRow from the TileStructs in step 3… but does doing this actually give me the modified array or am I simply getting the original array? From my testing, it looks like the ladder.
- Why does this not work?
- What would be the correct way to make this setup work?
- Is there a better way to accomplish this?
Thanks,
Chumble