Updating array of struct

Hello,

I have array of struct and I would like to ask you what is correct way to update specific variable on specific index with new value.

You can see my blueprint script below, and you can see that I get element from array with specific index, then I break it and make from it new element (changing only 1 variable) and inserting into array on same index.

Just want to know if there is “cleaner” way to update array with new values.

1 Like

Like so:

The unexposed pins remain unchanged. BUT!

When you use arrays, loops, get they fetch copies of the struct rather than references (notice the dot vs the diamond pin symbol in the pic above). This means that you need to be extra careful and always push in the altered data back into the container it came from:

4 Likes

So diamond is something like pointer in C language right? It contains a address of variable inside memory and circle is just copy of value, right?

This is what I want (image below), as you can see, I loop throught whole array and spawn Actors, then I only want to update reference in my struct to newly created Actors and others values leave the same.

But if I run this, then my apps stop working, because I insert new element into array (endless loop)? I don’t think that there is endless loop, because I insert new element into array at current index, but ForEachLoop should jump to another index in array, no?

So diamond is something like pointer
in C language right? It contains a
address of variable inside memory and
circle is just copy of value, right?

Pretty much, yeah.

  • at the moment, you have no array to insert structs into - nothing is hooked up to the Insert node’s array pin (you either updated the image or I went blind)

  • insert will push other indexes each time it’s called - you will end up with updated structs in front and old ones in the back of the array - the array will double in size. That’s unless Elements is a brand new, empty array with no data.

  • use SetArrayElement (as seen in the pic attached in my previous post) - this will simply replace the array element with the updated one

  • the top bit of my pic above seems to be precisely what you need, Set Members In struct is key, here, just expose RefCube and you’re good to go. No need to break/make structs like that.

Thank you :slight_smile:

So I ended with something like this, so Set Member will just change Ref Cube and another values leave untouched, right? :slight_smile:

2 Likes

Yeah, that looks about right. Should work.