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


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.


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.


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


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



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)))

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.

Yes, I do have twilight glasses.

I hate this “forum” system.


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!')


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

It seems to work, however.


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


Minions have findNearest()


Erm… I think it is because either:

  1. You are trying to say a list
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.)


@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'));


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


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