Problems adapting array within a dictionary

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By stubbsy345
:warning: Old Version Published before Godot 3 was released.

Hi, so I am having problems adapting a value in an array that is contained within a few dictionaries. Basically, I am trying to change the first string in my array depending on the content the user inputs. My code looks like this.

func update_content(currentInteractable): 
      var flag1 = a
      var flag2 = b
      var input = userInput
      
Dict[flag1][flag2][currentInteractable][0] = userInput

For some reason when I run this code it changes all the values on the first item in the array for all of the interactables (i.e. all options in the flag2 dictionaries), rather than just changing the value for the array contained within the currentInteractable dictionary? If anyone knows why this is happening I would really appreciate a solution.

Thanks in advance!

Your code is ok, however it looks like more is going on. Is that all of update_content? Where are you using it? Maybe you used it in a loop for all interactables and as a result, it’s going to update all of them instead of the one you want?

Zylann | 2017-04-09 14:39

Hi, so no the problem only occurs when I am trying to change the first item of an array. For example, if I take out [0], and just replace the current interactable dictionary with a string, the code works fine.

stubbsy345 | 2017-04-09 15:51

func update_special(b): 
var special = get_node("/root/B").Values
var SpecialChange = story["data"]["Change"]["SpecialChange"]
if SpecialChange != null:
	for i in range (0, SpecialChange.size()):
		var flag1 = SpecialChange[i]["Flag1"]
		var name = SpecialChange[i]["Name"]
		var textValue = SpecialChange[i]["Value"]
		
		special["OTHER"][flag1][name][b][0] = textValue

So above is the code I am using. When I remove the [0] and just replace the [b] dict with the text value it is working fine for only changing the value of that array. But if I want to adapt the value of an array contained in that dictionary it adapts the arrays of all the dictionary items contained in the name dictionary.

stubbsy345 | 2017-04-09 20:23

You have lots of nested arrays and dictionaries, I’m confused about what is an array and what is a dictionary. Did you try to make a simple project that reproducts only this in one single script with “fake” data?

Zylann | 2017-04-09 20:37

What is the type of story and special there?

Anutrix | 2017-04-10 14:28

I am not sure but the problem I think is special["OTHER"][comment5-flag1][name][comment5-b][0] = textValue should be something special["OTHER"][comment5-flag1][name][comment5-b][i] = textValue. Also dictionary has method values() not Values().

Anutrix | 2017-04-10 14:37

@Anutrix .Values is from a scripted node, not a dictionary.

Zylann | 2017-04-10 19:15

So the only way I could get it to work was if I assigned the other variables in the array to a variable and then recreated the array with the players input in the first item and the rest as the other variables. Then I just had the [b] dictionary assigned to the array I created. That worked. But trying to change the [0] item in that array would not.

Thanks guys, I don’t know why it’s not working and the workaround isn’t the most elegant but it works.

stubbsy345 | 2017-04-10 19:40