0

So I'm coding a small game in python using curses, I have a function to find out a tile's data as shown below:

def tileGrabber(position):
    global tile_data
    global window

    for k in tile_data:
        if k[0] == position:
            return k
    return None

I call this later in an object pick-up function to move said data into the players inventory using the following:

def pickUp(what):
    global inven_data
    global player_position

    if what == 'thisItem':
        thisItem = tileGrabber(player_position)
        inven_data.append(thisItem)
        tileEditor(player_position, [1, 2, 3, 4], [' ', 'empty', None, None])

then the tile editor changes the data of that position empty / None. tile editor as follows:

def tileEditor(position, indexs_to_edit, new_values):
    global tile_data
    global window

    tile_to_edit = tileGrabber(position)
    location_of_tile = tile_data.index(tile_to_edit)
    for k in range(len(indexs_to_edit)):
        tile_data[location_of_tile][indexs_to_edit[k]] = new_values[k]

Using the Tile Editor makes the data stored in the players inventory also set to empty / None. How could I change the tile grabber to return a copy of the data instead of the actual data?

I got it to work using the .copy() function on the tile grabber return but was wondering if there was a better way around this.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • BTW, all those `global` declarations are entirely unnecessary. – deceze Apr 06 '22 at 06:31
  • And, well, yes, if you want to make a copy of a list or dict at some point, then that's how you do it. There may be other ways, like constructing an entirely new list with a list comprehension while also filtering it, which may make more sense in some situations than `list.copy`. Maybe consider http://codereview.stackexchange.com for something like this. – deceze Apr 06 '22 at 06:33

0 Answers0