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:

5 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:

3 Likes

Yeah, that looks about right. Should work.

Can you please help with a similar question?

  1. How do I Set all indexes of an Array, to a Certain INT value, and in the Construct Graph,
    • but only once? (Only when the Certain INT value changes in Editor - in original BP or BP Instance copy.)

Meth 1 and 2 code is not working when connected to Construct node:

I have two variables. An INT (“Override”), and “Array” of INTs.
I want an easy way to update each INT in the “Array” to all be the “Override” INT #.

Q2: But only change Array values once in Construct: meaning if in Editor, I manually change “Override #” from 1 to 2, then Array updates to all have 2s.

But if I now I manually change index 1 of “the Array” to a value of 3 (and “Override #” is still at 2), then the Construct code will not trigger to reset all the Array to #2.

(Im not sure of the logic in Construct, if I need to add a Bool, to turn on/off parts of the Construct code til I want it run. Or if I can rely on just not changing the Override Int, and thus that code will not run? But the Meth 1 code is not making the Array update at all, when I change “Override” to 2.)

Please help, ty.

1 Like

The script if fine, I am not 100% sure I understand the issue, though; but hear me out:

The Construction Script runs :

  • when actor is spawned; that exposed variable will be updated with no additional script:

image

  • when an actor is placed and / or dragged in the scene
  • when a Child Actor Component instantiates an actor
  • when actors’ data gets updated - this can be done by flagging variable as Instance Editable:

If you do the above, the actor fill fetch data from this variable, no matter what you set its value in BPs.


Since this is what I believe you’re after, in your case:

image

As soon as you hit enter, every index of this array will have 2 assigned.


  • sometimes you do not want to run the entire CS or need to run something unrelated to the CS, you can use Editor Callable Custom Events:

Here I assign a value to an array inside the actor. This has nothing to do with CS, but simply updates the instance of the actor.

  • there are also EUWs - Editor Utility Widgets. They can do the above but the script does not belong to the actor. Let’s say you want to select 14 / 20 actors in the scene and update their arrays all at once to some specific values. Or make them trace their surrounding. The script does not need to clutter the affected actor and can live in a specialised widget that can be only run outside of PIE.

  • the Construction Script does not run for actors placed in the scene when the game starts

1 Like

Thanks for your time listing all those options and the graphics :slight_smile:

I checked that my vars are Instance Editable. But in BP editor (not BP Instance), does your Array update to 3, 3, 3 - if you change “Item” to 3? Because mine does not.

My Array only updates if I edit in the Level, my “Item” Int of a BP Instance (copy).

“The Construction Script runs :” “when actors’ data gets updated - this can be done by flagging variable as Instance Editable

  • Is not editing a Var in the BP Editor (not Level), a condition to make Construct fire?
  • (It works when I change a droplist Var (in Editor), and I have CS that instantly works to change the displayed Mesh - while in BP Editor AND in Level > BP Instance. But this Array Var is only updating in BP Instance (edit in Level).)

And I have some bugs that may be complicating my testing.
Bug1: the Orange-animation Construction-line fails to display weekly, when I press Construct (and I know the line worked before). (Reporting in another thread.)

Bug2: I think my asset is corrupt. My first test Array had 10 indexes, but only 2 show in the BP Instance, after compile/save/restart Engine. So I made a new Array. Now all indexes show.

Limitation or bug 3: But editing in Level > BP Instance, I can change “Item” INT = 2, but can no longer change the individual indexes of the Array values. They get locked on 2. (Perhaps because the CS autofires when I type 5 into the Array index, thus the INT 2 pushes a CS override (upon the Array) back to 2?)

Thus thank you for teaching me about this. Perhaps I want to set the Int/Array with this button thing. So I will investigate this. Ty :slight_smile:

  • (My goal was to save time, Set 10 indexes of an Array to value = 5, instead of having to set them all. But I also want the option to manually change a few of them after the bulk Set.)

It does not and it should not. So you’re expecting:

The array to update here, right?

Limitation or bug 3: But editing in Level > BP Instance, I can change “Item” INT = 2, but can no longer change the individual indexes of the Array values. They get locked on 2. (Perhaps because the CS autofires when I type 5 into the Array index, thus the INT 2 pushes a CS override (upon the Array) back to 2?)

I’d say it is neither and working as intended. It’s just not working the way you need it, which is fair. Perhaps you could ask the CS to do more work:

Set all to Item, and then set an individual Index to Another Item. Could this work?

1 Like

Thanks for your reply on issue 3.

Yes. That is what I need.

  • I tried your pic, and I can manually change a specific index at “Complete.” (Fixes one issue, but I still can’t update ALL Array values while in BP graph Editor > after CS runs.)

  • And I setup a Call-In-Editor Event. But I can only use the button in Level/World Editor???

  • So none of these are solutions. (I’m prototyping in BP code, and need to see the changes live when CS runs, before I ever place the Actors in Level. Easier to stay in the same Graph window than switch to Level tab.)

So back to my issue, I don’t understand why cant CS update all values in an Array - to a specific INT - while in BP (Graph) Editor? How can this be working as intended [UE4.27] when I can live-update other types of Vars with CS - but not Arrays?

Hi, Im back if you can please help. Ty

  1. Can Construct (in Editor) really not update certain variables like Arrays (this thread - I gave up trying to update the Array),
  2. or MAPs (new thread)? (CS works on changing Meshes and Light colors - but not Arrays/Maps?) This [bug?] is paralyzing my progress.