Updating an array inside a structure inside an array

Unreal 4.27.2
Hi, I am trying to update an array that is inside a structure that is also inside an array, however it doesn’t seem to be updating as I’d expect, is there something wrong with the way I am doing this?

The process is completing, as I have a print at the end that is firing correctly, I also have a print that loops all elements inside the “ObjectivesCompleted” array to check it has been added, but it is always empty.

hi @WesLee1991

when you manipulate objects in an array it gets a copy of the item. This means that once you edit that item, you need to put it back into the same index from which it came.

Set Array Elem | Unreal Engine Documentation

Use set array element to the new updated structure copy

1 Like

I tried that originally, does the same thing;

buit not in the original array also!, you are setting the array in the structure thats fine, but you not putting the structure back into the original Quest Data

1 Like

Ah I see what you mean now, though this also doesn’t work - also I thought using a ref to the array element would mean I don’t need to set the array element afterwards?

But check the get node, after you place it, it generally changes to get(acopy) when you choose ref

1 Like

Doesn’t the diamond icon mean it’s a reference, while circle means copy?

yes it does so strange, mine ALWAYS returns a copy even if i try ref so i cannot replicate what you are doing.

Just to be sure have you toggled a break point on the add or set array item? just to make sure its getting to that point and not failing on your branch before hand.

Also im gathering the ITEM is meant to be the same as the index value? so to me there does not seem to be a point to the array. Should the item be something different?

so what you are doing at index 1 item = 1, index 2 item = 2

1 Like

I put in a print when it didn’t work for testing purposes, but it will be connected to the Foreach break, if that’s what you mean? The print at the end is firing though, so the branch does pass fine I think.

The index of the int array doesn’t really matter, it is supposed to store the index of the quest objective that is stored in a QuestsList data table
So you could have completed the objective in any order and the array would look something like this
0 = 1
1 = 4
2 = 0
etc

This is the function before the one in question -


You can see the QuestsStruct has its’ own objectives array that is used to store the actual data, so my “Completed Objectives” just needs to store the index for the objective in question

ok sorry if it feels we going in circles, i understand it better a little, personally i would have added the objectives count at the start of the quest creation in to that array as booleans. So objective 1 = true or false. But if it works for you its all good.

So how are you testing for fails? Have you got some sort of output to look at and a test case input to compare against?

As your questindex could be higher than the actual current item count, it maybe failing becuase of that, say you have 2 quests logged, but you now to quest 4, you are trying to add an item at position 4 when you only have 0 and 1 ( 1st and 2nd) in there, so maybe it adds it in the next available item index which would be 2 no? (position 3 in array as its 0 indexed)

maybe just use array length into your add at index (which will always be one number higher than the last item index)

1 Like

Thanks for replying, I’ve tried it with length also, I think I had that originally also but when it didn’t work I changed small bits here and there, it now looks like this

So the print shows “You’ve completed objective 1” (as I am testing the 2nd objective)

I also have this on tick;

It seems to work as it doesn’t print anything until I accept the quest, then it prints “Quest quest0” every tick, and I was expecting it to also print “Objective 1 complete” after the first print “You’ve completed objective 1”, but it never shows this print.

ok i just did a test and the original method you used would add in the new value and not set value , but i can store the correct amount.

so get rid of set array item, just use an Array Add with the ObjectivesComplete as the taregt array, and the ObjectiveID as the item input

Now as for the structure, you need to do exactly as you have and use a get then a set members, as it is a ref, you shouldnt need to set it back into the array

1 Like

Okay, thanks for this, sorry it’s not working still :frowning:

for reference this is what the prints are looking like -
image

It should be showing the completed objective in the tick print, but it should also not be allowing me to complete the same objective twice since I already have a check on that also

I must be missing something really obvious here, if it is working for you but not me

edit;
I just tried adding this foreach to the end of set members and it is not showing here either, so I feel like either it’s not setting correctly or the array it is setting isn’t updated when I do set it

Are you sure its doing the objective more than once? as the tick will always show what you have done as you a cycling through them.

So to help me a little here what is the breakdown of your struct, item types specifically “Quest”

i want to try and recreate the whole thing and i guess quests are just names?

1 Like

The quest structure “QuestsStruct” (where the quest data itself is stored is) is like this


This only has 1 quest inside right now for testing, and that table looks like this

The objectives structure “QuestObjectiveStruct” looks like this

The first value in this structure uses enums “QuestsObjectiveTypes” to know which value in here is to be used, this looks like this

Then I have a 2nd quest structure “QuestPlayerListStruct” for storing the current quests and objectives the user is on;


This is used as an array variable inside a player component
image

All of this is working as expected, though I am only testing the location objectives right now, just seems like the missing link is actually setting the objective as complete by adding its’ index to the Completed Objectives array

ok bare with me im gonna make a minimal version of what i need just to test what you want to achieve so im gonna ignore variables that wont be needed for updateing the quest

1 Like

Could you try storing the Objectives Completed array from the struct break node in a Local Variable before adding Objective Index to it? then set that local variable as the “Objectives Completed” in the set members node?

I suspect the culprit here is the break node, since it’s pure. it always outputs a copy of the array, so you’re adding the index to a copy, then setting a different copy as the Objectives Completed input in set members, if that makes sense.

I’m not 100% sure though!

2 Likes

This has solved it! Thank you both for your help with this!

|

1 Like

id just sat here testing this and my suspicion of this came up, i didnt even see it in front of me, your eyes are great!

but yeah i found the same conclusion needed a local variable and i did suspect copied arrays rather than referenced at the start but i was over thinking it.

good spot!

2 Likes