Properly Setting Struct within an array

Hey there. So I’m new here, porting a small project from Unity and having a blast but I had a question on how Arrays of Structs behave.

A contrived example:
Let’s say I have a Struct with a single int member and I make an array with two structs initializing them to 100 and 200 no matter how many times called.

I then Loop through the array breaking out the struct reference, increment and set the ref to the new incremented value.

Then I print this value:

This fails and prints out 100 and 200 no matter how many times E is pressed.

So I figured I needed to write the struct ref back into the array, like so:

As expected, this prints out 100, 200 on the first press and 101 and 201 on the second press.

So I guess My question is why do I need to Set the Array Element if I am working on a reference to the struct? I honestly would have thought that just I needed to edit the reference. Obviously, I got it working but I’m wondering if the Green Comment is the correct way to do this or if there is some simpler way?

Thanks.

I think, techincally, it should update. You will come across quite a lot of things like this in the engine. It’s evolving very quickly. Usually best to not get too hooked up on it :slight_smile:

Because this is a copy:

@hossimo Note the shape of the pin:

Top one is ref. Structs are nasty like that.

3 Likes

Ah, the light… :slight_smile:

The output from Foreach node is a copy so you’re not working on a reference.

SetMembers uses a reference, but in this scenario it’s already a reference to the copy.

Foreach is a BP macro, you can double click on the node to view its code.
You could copypaste it into a new macro, and change the array GET node into a GET(by ref). That should make it work like you expect.

3 Likes

I’ll just point out another set of traps:

  • this is a custom event:

You think this would work. It does not.

  • this is a function:

You think this would work. It does.

2 Likes

This is the kind of thing I mean… :melting_face:

Thank you so much @Everynone I totally did not notice the shape of the pins, that is a huge help! I’m not sure why I thought I had a reference, I guess when I was testing I used a Get and noticed a Copy and Ref version, but I did not notice that the Foreach was copy only.

Also Thanks @Chatouille I took a look at the foreach and created a version using Get (ref) but I’m not sure that the output pin can be a reference, perhaps that’s the reason a ForEach (ref) does not exist?

If I choose a Wildcard or my Struct type the return element always seems to be a circle (copy I assume).

However oddly if I choose a type that says Reference in the (Like Base Component Reference) it also seems to be a copy.

Either way, I see what I need to do, Either don’t use ForEach and loop through myself without a macro or copy the copy back into the array as I did.

Thanks again So much for the help. I’m sure I’ll run into more but this was enlightening.

1 Like

I’ll just point out another set of traps:

:face_with_diagonal_mouth:

Also thanks for that. So much to learn!

You cannot customize Macro pins but they don’t matter, Macro input and output nodes are skipped as the macros are inlined into other graphs, so the GET(ref) should work as expected.

Give it a try with a simple test

1 Like

@Chatouille Yeah, I quickly tried this morning but had no luck but just tried again and I got it working.

Basically, I need to set both the input and output pins to wildcards then it seems to work. If I set either to my type (not really what I want to do) then it failed.

I guess I didn’t start with a wildcard then when I switched the type the caller got unhooked and I didn’t notice.

Anyway, round-about way of saying Thanks a ton!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.