0

I have looked at several other questions regarding this error and I still can't figure out what's wrong.

To select units, I have lists/arrays which draw from classes:

unit_orc_grunt = Unit("Grunt",[35,70,105,140,175,210,245,280,315,350],[35,70,105,140,175,210,245,280,315,350])

positions = numpy.array([
                            [unit_orc_grunt.locationx[0],unit_orc_grunt.locationy[0]],
                            [unit_orc_grunt.locationx[1],unit_orc_grunt.locationy[1]],
                            [unit_orc_grunt.locationx[2],unit_orc_grunt.locationy[2]],
                            [unit_orc_grunt.locationx[3],unit_orc_grunt.locationy[3]],
                            [unit_orc_grunt.locationx[4],unit_orc_grunt.locationy[4]],
                            [unit_orc_grunt.locationx[5],unit_orc_grunt.locationy[5]],
                            [unit_orc_grunt.locationx[6],unit_orc_grunt.locationy[6]],
                            [unit_orc_grunt.locationx[7],unit_orc_grunt.locationy[7]],
                            [unit_orc_grunt.locationx[8],unit_orc_grunt.locationy[8]],
                            [unit_orc_grunt.locationx[9],unit_orc_grunt.locationy[9]],
                        ])

not_selected = [
                    positions[0],
                    positions[1],
                    positions[2],
                    positions[3],
                    positions[4],
                    positions[5],
                    positions[6],
                    positions[7],
                    positions[8],
                    positions[9]
                ]

selected = []

Then I have this in the game loop:

        elif button_type[0] == 1:
            for position in positions:
                if numpy.any(position == not_selected) and numpy.any(position != selected) and mouse_position[0] >= position[0] and mouse_position[0] <= position[0] + character_width and mouse_position[1] >= position[1] and mouse_position[1] <= position[1] + character_height:
                    selected.append(position)
                    print("Position = ",position)
                    print("Selected Units = ",selected)

This works fine, and the selected units would then be drawn from the list 'selected', however, when I try to add the line not_selected.remove(position) I received the error:

        elif button_type[0] == 1:
            for position in positions:
                if numpy.any(position == not_selected) and numpy.any(position != selected) and mouse_position[0] >= position[0] and mouse_position[0] <= position[0] + character_width and mouse_position[1] >= position[1] and mouse_position[1] <= position[1] + character_height:
                    selected.append(position)
                    print("Position = ",position)
                    print("Selected Units = ",selected)
                    not_selected.remove(position)

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Why does selected.append(position) work but not_selected.remove(position) doesn't work here? How do I fix this?

roguedev
  • 175
  • 1
  • 1
  • 12
  • `remove` is a list method that works by comparing `position` with all values of `not_selected`. But comparison methods involving arrays produce boolean arrays - multiple values. This error arises when a boolean array is used in a scalar context such as a Python `if`, or here a list `remove`. Look at the sidebar links. – hpaulj Oct 22 '16 at 18:45
  • What is `Units`, and why is `positions` an array, not a list? – hpaulj Oct 22 '16 at 20:09
  • Thanks for the explanation. Units are soldiers on the map, and positions has to be an array as it contains all the coordinates for the units, so that I can change them with simple rules. Although I understand your explanation, I can't work out how to go about removing the selected units (I read the sidebar links). – roguedev Oct 22 '16 at 21:59
  • Update - I found a post where someone made a function to remove arrays http://stackoverflow.com/questions/3157374/how-do-you-remove-a-numpy-array-from-a-list-of-numpy-arrays – roguedev Oct 22 '16 at 22:19
  • Yes, that's the kind of thing you need to do if you want to remove array objects from a list based on values. – hpaulj Oct 23 '16 at 00:11

0 Answers0