+1 vote

Hello! I am a beginner. I have a dictionary (all values are arrays) I want to put the values from the A B C D array [0] into a new array. I did get the result I wanted. But I don't think this is a good way to do it. Because the number of arrays in other dictionaries may be different.
I think I should use a for loop or something to get the result, my attempt failed. Please give me some good advice. Thanks!

extends Node

var dict = {
        "A": [1,    2,    3,    4],
        "B": [12,   4,    3,    8],
        "C": [true, true, false,true],
        "D": ["L1","L2", "L3", "L4"],
        }

func _ready():
    var arr= dict.values() #Puts all the dictionary into an array
    var num= max(dict.size(),dict.size()) #There are 4 arrays in dict

    #(funny method)
    var new_arr = [arr[0][0],arr[1][0],arr[2][0],arr[3][0]]
    prints(new_arr)
    return new_arr #print:[1, 12, True, L1]-- But.
Godot version 3.44
in Engine by (16 points)
edited by

I am confused, why are you trying to copy your dictionary? Why not simply get the values directly from the dictionary?

what is the game about and is it 2d or 3d ? I didn't understand you well i am also beginner

This is COMMENT not an ANSWER (and, it's irrelevant to the question). Please use the appropriate function when responding...

Why are you so angry? relax man i just ask you about the game

First, I'm not angry (and, I'm not the OP).

I'm just trying to point out the proper use of the forum. When you use the "Answer" feature (like you did), the question shows up in the main forum as having "been answered". Due to the amount of traffic on the forum, those of us who attempt to answer questions don't tend to spend much time looking at questions that have already been answered.

So, your input (posted using the "Answer" feature) could have easily caused this question to never receive an actual answer - thus doing a disservice to the original poster.

So, in the future, just use the "Comment" feature when seeking additional information about the question.

thanks dude for teaching me this because i am new user and i didn't knew this before i am not mocking i am serious and i didn't know that will hurt you I am sorry ^_^
i am trying to delete the answer

This is the comment section not the answer section. You are telling him to post a comment and not an answer on his comment.

This is the comment section not the answer section.

Not much to be gained here now, but...

Originally, the below statement (now shown above as a comment) was submitted as an Answer:

what is the game about and is it 2d or 3d ? I didn't understand you well i am also beginner

Once I explained the Comment / Answer system, the OP apparently used the "Hide" feature to remove the answer. I'm not sure what happened from there. I can only guess the forum software took the conversation belonging to the now Hidden answer and moved it to the comment section of the OP - hence it looks like a Comment now and the entire conversation makes no sense... :(

Yes, but the comment was in answer section. :)

never mind i can understand you , just tell me is it 2d or 3d
in 3d it will be like this
enter image description here

but in 2d it will be like this
enter image description here

2 Answers

0 votes
Best answer

Here's one way...

func _ready() -> void:
    var new_array = []
    var dict = {
        "A": [1,    2,    3,    4],
        "B": [12,   4,    3,    8],
        "C": [true, true, false,true],
        "D": ["L1","L2", "L3", "L4"],
    }
    var arrays = dict.values()
    for array in arrays:
        new_array.append(array[0])
    print(new_array)
by (19,272 points)
selected by

This is very good way. Thanks!!

+2 votes

Set pos to the point you want to query

var pos = 0
var new_arr = []
for value in dict:
    if dict[value].size() > pos:
       new_arr.append(dict[value][pos])
by (6,876 points)
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.