+1 vote

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!

in Engine by (296 points)

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?

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.

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.

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?

What is the type of story and special there?

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

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

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.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.