Trouble pulling array value from dictionary using C#.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider

See title. I have a dictionary which hold various data for the player’s weapons in my game.

public Dictionary wpnDict = new Dictionary {};

Entries in the dictionary.

//Add entries to wpnDict. The key ID is the currently equipped weapon. The array stored within stores number of ticks between bullets, how many bullets spawn with each shot, and eventually, the animation needed.
        wpnDict.Add(0, new Array {8, 1, null});
        wpnDict.Add(1, new Array {16, 5, null});
        wpnDict.Add(2, new Array {12, 2, null});

However, when I attempt to PULL the needed data from the array, I encounter errors, here’s the code.

if(plyr1BulletTicks = wpnDict[plyr1CurrWeap][0])
//Spawn bullet

And the error I receive is:

Cannot apply indexing with [] to an expression of type 'object'

What is the proper way for me to pull the array entry that I want with C#? Not having any luck finding the right syntax.

:bust_in_silhouette: Reply From: dewcked

Add using

using System.Linq;


if(plyr1BulletTicks == wpnDict.ElementAt(plyr1CurrWeap).Value[0]) // Use double equal
    //spawn bullet

I think you’d better use another STL types.

What do you recommend?

9BitStrider | 2021-11-26 11:46

STL type that you can access by index. like Array of Arrays, Lists, Vectors…

dewcked | 2021-11-26 16:03