Very confuse on the way we access the dictionary


I don't know if I missed anything, but how did the code access the button_data:dictionary? In the last lesson, we learn that to access it we call dialogue[id][buttons], which I didn't find here. I only see the code putting it as an argument in the create_button feature and it just accept it as that, like how, what link it to that, am I missing anything? D:

  • a
    andrewjhaugen replied

    1 love
  • b
    brodyp replied

    Thank you for a very detailed answer :D Sorry for posing a confusing question, next time I will be more specific. Somewhere around 6-minute-mark, you answered my question. and yeah I had trouble following the code, I understand better now thanks to the later part of your answer ( so i guess by being vague you actually answered more than I originally asked for :D ). 

    Just to clarify, the part where I said  "In the last lesson, we learned that to access it we call dialogue[id][buttons], which I didn't find here." was this. 

    Like not as in the code but general knowledge that is in the writing. so i was confused because I didn't see this part and yet the button dictionary data was still ... there. I understand now that it is just a temporary variable and the data was under another form that is line-data.buttons 

    Again thanks for answering my question. and thanks for the tips! 

  • a
    andrewjhaugen replied

    Happy to help! I was worried my answer might make things more confusing so I am glad to hear that it was at least somewhat helpful.

  • M
    Madoba-2 replied

    Hi, two additional questions on the way of accessing the dictionary.

    I have watched your explanation video, but still don't understand, how the function "create_buttons" work at one detail.

    So I have the function:

    func create_buttons(buttons_data: Dictionary) -> void:
    for text in buttons_data: ## question 1
    var button := Button.new()
    button.text = text
    var target_line_id: int = buttons_data[text] ## question 2

    At my understanding, with the argument buttons_data in this function we add the dictionary of "buttons". As an example at Value 0 it would contain:

    "Let me sleep a little longer": 2,
    "Let's do it!": 1,

    Question 1

    What is getting counted in

    for text in buttons_data

    on the variable text? The keys, or the values of the dictionary?

    At my understanding, it refers to the keys, since we seem to pass the value of text in the line

    buttons.text = text

    But then, the line comes where I get confused (question 2):

    var target_line_id: int = buttons_data[text]

    Now the [text] value is an integer?! Shouldn't it be a string containing the keys?

    And lastly: In the video you explained that you can access the value of a dictionary by adressing it with the keys. So buttons_data[text] should give me back the value at key "text". But the key "text" doesn't exist at the sub-dictionary buttons? At my example there are only the keys "Let me sleep..." and "Let's do it"?

    I guess I'm obviously missing and/ or misunderstood something. Hope you get my point - thanks in advance!

  • Nathan Lovato replied

    Question 1: Yes, you're correct! when you use a for loop to loop over a dictionary, you're looping over the keys. So on the first iteration, in your example, the text variable will be "Let me sleep a little longer" and on the second iteration, the text variable will be "Let's do it!".

    Question 2: text still contains a String value! In dictionaries, unlike in arrays, we use the key, which can be anything (a String, an int, a Vector2 ...) to retrieve the value.

    When we write buttons_data[text], we are not storing the value of the text variable. Instead, we are asking the computer to get the value corresponding to the key contained in the text variable. 

    So in the first loop iteration, buttons_data[text] is equivalent to writing buttons_data["Let me sleep a little longer"], and this fetches the corresponding value, the number 2.

    In the second iteration, buttons_data[text] is equivalent to writing buttons_data["Let's do it!"], and this fetches the corresponding value, the number 2.

    Does this answer your questions and help you demystify how it all works?