Summit's Gate: friend.findNearest(self.findByType("catapult")) returns NULL


#1

As far as I understand,

catapult = self.findByType("catapult") self.command(friend, "attack", friend.findNearest(catapult))

should let minions attack the catapult nearest to them.
However, self.command throws a null exception for target.


#2

Can you post the full code you are trying? This snippet wouldn’t work as it doesn’t define friend and would run even if catapult is empty. Keep in mind that an empty list is considered truthy in Code Combat. Use len(catapult) to check to see if it is empty. Oftentimes enemies are added after the first frame.


#3

Check if there IS a catapult. such as:
if catapult:
self.command(friend, "attack", friend.findNearest(catapult))


#4

Do you have glasses or lenses with infinite vision range? Otherwise, self.findByType may return an empty list.


#5

@Hinkle
@ARasberrypy-thon

enemies = self.findEnemies()
if enemies:
    catapult = self.findByType("catapult")
friends = self.findFriends()
if friends:
    for friend in friends:
        if catapult:
            self.command(friend, "attack", friend.findNearest(catapult)))
            continue

It is this code that throws the exception, which confuses me as self.say(friend.findNearest(catapult)) returns both catapults, and all minions are moving to attack “their” catapult, despite this error.

@UltCombo
Yes, I do have twilight glasses.

I hate this “forum” system.


#6

I am not sure why self.say(friend.findNearest(catapult)) isn’t working correctly.

I do know why your code is throwing errors and mentioned it in the last post:

catapult = []
if catapult:
    self.say('Empty Lists Are True!!!! This code will run!!!!")

catapult = []
if len(catapult) or catapult[0]:
    self.say('These are not True! This code will not run!')

#7

Well that’s just the issue: Both catapults are still alive before this error occurs, so the array is actually not empty.

edit:
It seems to work, however.


#8

Does friends have findNearest method? It must be self.findNearest(catapult))


#9

Minions have findNearest()


#10

Erm… I think it is because either:

  1. You are trying to say a list
    2:
    Try:
catapult = self.findByType('catapult', self.findEnemies())

This is because you have the option to add a second argument, and it basically says:
Find catapult, inside of this array! (Arrays can be taken as arguments, I think.)


#11

@ARasberrypy-thon, in your example catapult will still be a list. It will just not contain allied catapults.

You can either:

catapult = self.findNearest(self.findByType('catapult'));

or

catapults = self.findByType('catapult')
if len(catapults):
    catapult = catapults[0]
else:
    catapult = None

#12

Well, will saying a list work though?
Also, just give it a try? :sweat: