I am interpreting when you say ‘long’ you mean the actual code, not execution time because as @lbertocchini says, performance wise this is about as fast as you’re going to get.
If you look at the
attempt_spawn function you can see that it actually converts any single input to a list, and processes a list of locations. Basically, if you pass a list to
attempt_spawn then it will try to spawn each one in the list.
firewall_locations = [[1, 1], [2, 2], [3, 3], [4, 4]]
You’ll notice, however, that I am no longer checking to see if the game state can spawn. Again, if you look inside the
attempt_spawn function you’ll see that when it tries to create each unit it calls
if self.can_spawn(unit_type, location): inside the loop. Thus, there should be no functionality difference in the small snippet of code posted.
An instance of where you would not want to do this is if you wanted to do something else if a creation failed. Then you would need to loop through and check each location and have an
In regards to list length (eg
[[1, 1], [2, 2], [3, 3], [4, 4]]), as @lbertocchini demonstrates you can do some things to create a list, but this will only work if you can apply some function to the locations, for example, a line, a curve, etc. If you have a really massive list I would just save the list somewhere else and then reference it to avoid cluttering your code.
I would also recommend looking at list comprehension for python. To use @lbertocchini’s example above, it would let you do things like this:
game_state.attempt_spawn(DESTRUCTOR, [(x+1,x+1) for x in range(4)])
(I added 1 since
range(4) returns 0,1,2,3)