Home > database >  2-Dimensional Array not updating properly after variable inputted into array index
2-Dimensional Array not updating properly after variable inputted into array index

Time:10-30

My program includes a map controlled by a main array, and to update the map when changes happen the main array must also be updated. My problem is that, when I set the index of a 2-Dimensional array as 2 variables, like such:

array_name[x][y] = variable

The array is not updated properly. This prevents the map from being updated properly. Are the variables not being inputted into the array properly, or is something else wrong?

Main File Code: (map_data is the main array)

        expansion = gamfuncs.expand("None", action[3], action[4], map_data, action[1])
        if expansion[0] == True:
            expansionRow = expansion[1]
            print(expansion[1])
            print(expansion[2])
            print(expansion[3])
            expansionCol = expansion[2]
            map_data[expansionRow][expansionCol] = expansion[3]
            print(map_data)
            mapChanged = True

Expand Function:

import pygame

class gameFunctions:
    def __init__(self, screen ) -> None:
        self.screen = screen
        

    def expand(self, nation, row, column, table, terrain):
        if nation == "None":
            if terrain == "Grass":
                table[row][column] = 7
                return True, row, column, table[row][column]
            if terrain == "Hills":
                table[row][column] = 8
                return True, row, column, table[row][column]

        return False, row, column, table[row][column]

CodePudding user response:

There seems to be nothing wrong with your code, so you should check how you are using the array, because it seems the error actually lies in the execution of the array. Since you haven't put that much code I can't say for sure what is wrong with the array's execution.

  • Related