Setting variable inside a struct array - Not working

I am having problems setting variables inside a struct(inside of a array).

I’m working on a hint system. I have an array of hints and I have a variable bool for if the player has bought the hint or not. So I have made a function inside the objective tracker that is used to buy the hint. It is suppose to set bool to true on that hint-item. But I can’t make it change the value. If I don’t hard code it from the start it will always be false.

What I do is a foreachloop that loops thru and finds the hint I need to change, then I use “setMember” to set the variable inside that array element(struct). I have tried it a couple of different ways but it still does not work.

Does anyone know what I am doing wrong?

How do you know it’s not working.

I think doing the print next to the ‘set members’ would work better.

Thanks for the reply!

If I do that the print says it is true. But when I check the array element from my hintwidget the variable is still set to false.

I added a debug print loop so I could see what the values where over time, it never shows the element variable change.

This loop looks fine, that’s all I can say I’m afraid… :frowning:

1 Like

No worries!

I made it work now but the solution seems really ugly and still not sure why my original graph didn’t work.

Yeah, that’s not right, eh?.. ( shouldn’t need to happen like that )

1 Like

It’s because you passed a copy of the struct instead of a reference, which is what the set member node requires. So you were setting the variable of a “floating” copy. In your fix, when you set the array element, you replaced the original with the copy, which is why it works.

You should be able to convert the element pin from a pass-by-copy to a pass-by-reference, but an alternative is this: break the struct from the array element pin (or you can right click the element pin and select “split struct”), then on the set array element, put a “make struct” node. Then plug the two nodes together with the variables you don’t want to change, and plug new values into the ones you do want to change. This is more hand-work, but I think it’s more performant since you’re not making a copy.

3 Likes

Either do what @midgunner66 suggests, or:

1 Like

doh!

1 Like

Got it: it’s the get node. There are two versions: one gets a copy, and the other gets a reference. Use the reference version instead of the loop’s element pin and that should be it.

1 Like