Using Set Members and Set Array Elem for struct adds new element to array

I have no idea why this is happening, but when I attempt to change a value in a struct thats contained in an array, it ends up creating a new element in the array instead of editing an existing element.

Hey Shirk-

Can you provide more detail about your struct and array? What are the different elements of your struct? Can you also explain what exactly your different variables are? Is Owned Ammo an array of structs that contains an array within the struct? What is Ammo Data? Please provide as much information as possible about your setup and what you are attempting to do to help investigate the behavior you’re seeing.

The only elements of the Struct is a float called TotalAmmo, OwnedAmmo is only an array of structs, which only hold a single float variable. AmmoData is just the struct variable that is stored in the ammo blueprint that contains the float variable.

Hey Shirk-

After stepping through the process of each node, the behavior you’re seeing is expected. Every even time (second, fourth, sixth, etc.) the branch is checked, it is changing the value of the struct that matches Ammo Data so that every odd time (first, third, fifth, etc.) its checked, the CONTAINS check fails and Ammo Data is re-added to the array.

To help me explain what is happening, lets assume the Owned Ammo array has 2 entries with the value of 10 in index 0’s struct and 15 in index 1’s struct. Let us also assume that Ammo Data is a struct with a float value of 20. At this point your default array is:

  • element at index 0 = struct with a float set to 10
  • element at index 1 = struct with a float set to 15

The first time you hit the branch node, Owned Ammo is checked for a struct with a value of 20 which it does not have, so it is added to the array at index 2. At this point your array is as follows:

  • element at index 0 = struct with a float set to 10
  • element at index 1 = struct with a float set to 15
  • element at index 2 = struct with a float set to 20

The second time you hit the branch it is able to find the array index who’s element matches Ammo Data (value of 20 at index 2). The “Set members in Struct” node is then hit which finds the index where Owned Ammo and Ammo Data match and passes in the corresponding element (in this case the struct at index 2 with a float of 20). This is then changed to a value of 25 and saved back into the same index with the Set Array Elem node. So your array now looks like this:

  • element at index 0 = struct with a float set to 10
  • element at index 1 = struct with a float set to 15
  • element at index 2 = struct with a float set to 25

This means that when the branch is hit for the third time, it again looks for a struct with a value of 20 which no longer exists so it gets added again as a new element so that your array is:

  • element at index 0 = struct with a float set to 10
  • element at index 1 = struct with a float set to 15
  • element at index 2 = struct with a float set to 25
  • element at index 3 = struct with a float set to 20

Cheers