Best way to manage arrays?

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

I’m trying to make a “library” of minigames that can be loaded into a queue and played.

Within this all-encompassing library, I would want a smaller library for each level, which would have its own unique games. Within that, the games would hold their data (like a title, description, time limit, etc).

In other words, a LIBRARY that stores LEVELS which store GAMES which store DATA.

Originally, my plan was to do an array like this:

tldr: library[level][game][game_data]

var library = [

    var level_1 = [

        var gameA = [
            "Title A",
            "Description A",
            30
        ],
        var gameB = [
            "Title C",
            "Description C",
            10
        ],
        var gameC = [
            "Title C",
            "Description C",
            60
        ]
    ],

    var level_2 = [

        var gameD = [
            "Title D",
            "Description D",
            30
        ],
        var gameE = [
            "Title E",
            "Description E",
            10
        ],
        var gameF = [
            "Title F",
            "Description F",
            60
        ],
    ]    

]

This doesn’t compile though because you can’t nest declarations like that. It also looks like a huge mess. If an actual programmer looked at this they might pull all their hair out and strangle me.

What’s the best way to go about doing something like this? The end goal is to store games neatly so that they can be added to a queue. I figured arrays would be the best way to do that, but I am curious to know if there are any smarter solutions to this problem.

:bust_in_silhouette: Reply From: timothybrentwood

You can create Game objects with all the variables you need then instance/sub-class them or if they’re simple enough you can create an array of dictionaries:

const GAME_KEY_TITLE = 'title'
const GAME_KEY_DESCRIPTION = 'description'
const GAME_KEY_TIME_LIMIT = 'time_limit'
const GAME_KEY_LEVEL = 'level'

var gameA =  {
GAME_KEY_TITLE : "Title A",
GAME_KEY_DESCRIPTION : "Description A",
GAME_KEY_TIME_LIMIT : 30,
GAME_KEY_LEVEL : 1
}

var gameB = {
GAME_KEY_TITLE : "Title B",
GAME_KEY_DESCRIPTION : "Description B",
GAME_KEY_TIME_LIMIT : 10,
GAME_KEY_LEVEL : 1
}

var gameC = {
GAME_KEY_TITLE : "Title C",
GAME_KEY_DESCRIPTION : "Description C",
GAME_KEY_TIME_LIMIT : 60,
GAME_KEY_LEVEL : 1
}

var library = [gameA, gameB, gameC]

func get_games_for_level(level_number : int) -> Array:
    var games_in_level = []
    for game in library:
        if game.get(GAME_KEY_LEVEL) == level_number:
            games_in_level.push_back(game)
    return games_in_level

func display_game_details(game : Dictionary) -> void:
    for key in game.keys():
        prints(key, ":", game.get(key))

func _ready() -> void:
    for game in get_games_for_level(1):
	    display_game_details(game)

More on how to use dictionaries:
Dictionary — Godot Engine (stable) documentation in English?