Hi I’m confused on how this code works. Like it says it returns a list of enemy’s missile in air. So as it is a list, to specify a specific projectile, I need to use hero.findEnemyMissiles()[0], right? And if I have to find the type of the projectile, is it simply by adding the property ‘type’, like hero.findEnemyMissiles()[0].type?
I don’t know why it seems my character couldn’t detect projectiles coming, and thus it doesn’t know when to cast a shield. ;(
My code for that part is like this:
while True:
if hero.nextSpawnDuration < 6:
hero.spawnCollector()
hero.findEnemyMissiles()
if hero.findEnemyMissiles():
if hero.findEnemyMissiles()[0]:
if (hero.findEnemyMissiles()[0].type == "fire-ball") and (hero.canCast("water-shield")):
hero.cast("water-shield")
elif (hero.findEnemyMissiles()[0].type == "fire-beam") and (hero.canCast("water-shield")):
hero.cast("water-shield")
elif (hero.findEnemyMissiles()[0].type == "fire-arrow") and (hero.canCast("water-shield")):
hero.cast("water-shield")
elif (hero.findEnemyMissiles()[0].type == "water-ball") and (hero.canCast("earth-shield")):
hero.cast("earth-shield")
elif (hero.findEnemyMissiles()[0].type == "water-beam") and (hero.canCast("earth-shield")):
hero.cast("earth-shield")
elif (hero.findEnemyMissiles()[0].type == "water-arrow") and (hero.canCast("earth-shield")):
hero.cast("earth-shield")
elif (hero.findEnemyMissiles()[0].type == "earth-beam") and (hero.canCast("fire-shield")):
hero.cast("fire-shield")
elif (hero.findEnemyMissiles()[0].type == "earth-ball") and (hero.canCast("fire-shield")):
hero.cast("fire-shield")
elif (hero.findEnemyMissiles()[0].type == "earth-arrow") and (hero.canCast("fire-shield")):
hero.cast("fire-shield")
How does this method actually work? Sorry if this is a stupid question ;3, cuz I am actually quite new to coding.
Welcome to the forum! This is a family-friendly place where coders can share bugs, ask for help on any CodeCombat level (don’t forget to post your code correctly), or just hang out with other coders. But before you proceed, please check out our guidelines: this topic.
Have a great time!
Yes… I understand. There are no “enemyMissiles” so it can’t “find-Nearest”.
Try making a variable for hero.findEnemyMissiles and put an if variable:.
Put the x = ... and if x and ... code in the if statement
Shouldn’t the error message be ‘something is not defined’, if there are no enemyMissiles?
I just can’t figure it out… so strange.
enemyMissiles = hero.findEnemyMissiles()
if enemyMissiles:
nearestEnemyMissile = hero.findNearest(enemyMissiles)
if nearestEnemyMissile:
if nearestEnemyMissile and nearestEnemyMissile.mana == 'fire':
hero.cast('water-shield')
if nearestEnemyMissile and nearestEnemyMissile.mana == 'earth':
hero.cast('fire-shield')
if nearestEnemyMissile and nearestEnemyMissile.mana == 'water':
hero.cast('earth-shield')
I see, thanks! I was just hoping to minimize errors here. But still, I have no idea why is this a type error. Isn’t it an entity? (Or is it how we call it idk)
I have been struggling on this method like for these 2 days
Hmm I have opened a fresh new code to test this out and I would still get this ‘type’ error
Here is my shortened version (cuz this is sort of a competition, I kinda not want to post my full code publicly) if you would like to try:
def choose(collector):
global count
count += 1
return ['fire', 'water', 'earth'][count % 3]
def collectHandler1(data):
unit = data.target
while True:
if unit.isReady():
unit.special()
if unit.item:
unit.bring()
else:
fruit = unit.findNearestFruit()
if fruit:
unit.pick(fruit)
def collectHandler2(data):
unit = data.target
while True:
enemy = unit.findNearestEnemy()
if enemy:
if unit.distanceTo(enemy) < 5:
unit.speical()
else:
unit.move(enemy.pos)
def collectHandler3(data):
unit = data.target
while True:
fruit = unit.findNearestFruit()
if fruit:
if unit.distanceTo(fruit) > 5:
unit.move(fruit.pos)
else:
unit.special()
def collectHandler4(data):
unit = data.target
while True:
if unit.item:
unit.bring()
else:
nearest = unit.findNearestEnemy()
if nearest and nearest.item:
if unit.distanceTo(nearest) < 5:
unit.special(nearest)
else:
unit.move(nearest.pos)
def collectHandler5(data):
unit = data.target
fruit = unit.findNearestFruit()
if fruit:
if unit.item:
if unit.distanceTo(fruit) > 5:
unit.move(fruit.pos)
else:
unit.special(fruit)
unit.bring()
else:
unit.pick(fruit)
hero.on('spawn-runner', collectHandler1)
hero.on('spawn-popper', collectHandler2)
hero.on('spawn-grower', collectHandler3)
hero.on('spawn-thief', collectHandler4)
hero.on('spawn-merger', collectHandler5)
spawndurationlimit = 10
while True:
if hero.nextSpawnDuration <= spawndurationlimit and hero.health > 350 and hero.time < 130:
hero.spawnCollector()
if hero.health < 450:
spawndurationlimit = 3
enemyMissiles = hero.findEnemyMissiles()
if enemyMissiles:
nearestEnemyMissile = hero.findNearest(enemyMissiles)
if nearestEnemyMissile and nearestEnemyMissile.mana == 'fire':
hero.cast('water-shield')
if nearestEnemyMissile and nearestEnemyMissile.mana == 'earth':
hero.cast('fire-shield')
if nearestEnemyMissile and nearestEnemyMissile.mana == 'water':
hero.cast('earth-shield')
Don’t know if hero.findNearest() works with enemyMissiles, since it keeps popping out such ‘type’ error. I have also tried specifying the object with array on hero.findEnemyMissiles(), assuming the least value of the index in the list would be the closest to the hero. Still no luck (you can refer to the first post).
I figured it out! You know, the balls/ arrows/ beams goes fast, right? So when you cast the shield, CoCo can’t find it again.Please also hover over the “!” and take a picture with it. it will say something different, I think.
For me, I think there shouldn’t be any significant delay in the if statement, even though they do travel fast, as those if operations are done within milliseconds, right? (I don’t know if that’s how we call the whole thing)
Apparently I see some top leaderboard players being able to cast a shield when a projectile is coming. Most others don’t. I’m beginning to think they could be tracking the opponent’s hero mana to determine if earth/water/fire balls are casted. Hope that’s not the case, else this is so braindead like how come a method dedicated to detecting projectiles not working or idk being so non-user friendly.
I will go and check on the exclamation mark to see what it says. Wait me a moment.
I tried before, it does work without any errors, but same it doesn’t seem to be able to findEnemyMissiles.
Off-topic: summer is ending and i still got loads of homework hehe. Since I have already invested quite much time in coding and I’m satisfied with my rank and what I have learnt, so maybe I might take a break, thanks everyone here! Very heart-warming community