Is it possible to make a Dictionary-type list?

Asked 1 years ago, Updated 1 years ago, 462 views

Let me ask you a question about the dictionary and list of C#.

(In the first place, I don't know if it's possible or impossible due to the specifications of C#.) I'm trying to create a list with Dictionary.The code is as follows.

inputRecord 2D = new List <Dictionary <string, float >>();

The purpose of use is to enable Unity to store input from InputManager for a certain number of frames in the past and to identify "command input" in fighting games.

Then, to retrieve the value from the code above...

Debug.Log (inputRecord2D[0, "Horizontal"));

I tried writing the code …, but I got an error.

"For my part, I wrote ""I want you to output the value (value) of the key called Horizontal stored in inputRecord 2D number 0"", but an error occurred.The error is rerror CS1501: No overload for method' this' takes 2 arguments. /

So, I would like to ask you if it is possible or not to nest Dictionary inside the list in the first place, or if there is a way to get the correct value? Please let me know.

Here are the codes you can paste and verify:

using System;
using System.Collections;
using System.Collections.General;
using UnityEngine;
using UnityEngine.UI;

public class CommandController: MonoBehavior
{
    public Rigidbody rigidbody;

    public List <Dictionary <string, float > > inputRecord 2D;// Stores all input and input per frame.
    public Dictionary <string, float > inputRecord 1D;// Contains all inputs and inputs.
    public int maxFrame = 10; // Maximum number of frames stored; maximum number of elements of inputRecord 2D.A value of 60 will store the input of the last 60 frames.

    void Start()
    {
        rigidbody=GetComponent<Rigidbody>();

        inputRecord2D = new List<Dictionary<string, float>>()
        {
            inputRecord 1D, inputRecord 1D, inputRecord 1D, inputRecord 1D, inputRecord 1D, inputRecord 1D, inputRecord 1D, inputRecord 1D, inputRecord 1D, inputRecord 1D, inputRecord 1D, inputRecord 1D
        };
        inputRecord1D = new Dictionary <string, float > ( )
        {
            // It originally stores inputs other than the horizontal axis.
            {"Horizontal", 0f},
        };
    }

    void Update()
    {
        inputRecord 1D ["Horizontal"] = Input.GetAxis("Horizontal");

        if(inputRecord2D.Count==maxFrame)
        {
            // If the number of elements in inputRecord 2D reaches the maxFrame value, overwrite the new element next to the old element.
            for (inti=0;i<inputRecord2D.Count-1;i++)
            {
                inputRecord2D[i] = inputRecord2D[i+1];
            }
        }

        inputRecord2D [inputRecord2D.Count-1] = inputRecord1D;

        Debug.Log (inputRecord 2D[0, "Horizontal"));
    }
}

c# unity3d

2022-09-30 21:50

1 Answers

I will write the answer as it has been resolved by the content of the comment.

First of all, regarding the Dictionary type List, the definition, initialization, and operation parts of the code can be successfully built and no errors or exceptions have occurred during execution, so it is possible.

The error Debug.Log(inputRecord2D[0, "Horizontal")); and its error error CS1501: No overload for method'this'takes2arguments are here:
Compiler Error CS1501

There is no overload of method '<method>' with argument '<numeric>'
An attempt was made to invoke a method for the class, but the method definition cannot contain a specified number of arguments.

If you apply this, "this method doesn't have a processing in the form of two arguments", but it's hard to understand the situation directly.

So if you look at it a little bit differently, it's suspicious that you're trying to access the array as a way of specifying values, but there are two subscripts lined up in commas.

I don't know if it's true, but if you think of List nested with Dictionary as a two-dimensional array, the subscripts of List and Key of Dictionary can be considered independent dimensional subscripts.


2022-09-30 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.