I wanted to make a function that would strike lightning on the closest enemy but the lightning is only targeting the giants even if the enemies are closer, any help?
here is my code:
Protect your base.
Spawn units to assault the enemy base.
Cast spells to react on enemies.
unit = hero.findNearestEnemy()
while True:
if unit:
hero.findNearestEnemy(unit.x, unit.y)
else:
pass
if unit:
hero.cast(“lightning”, unit.x, unit.y )
else:
pass
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!
1 Like
please format your code properly
unit = hero.findNearestEnemy()
while True:
if unit:
hero.findNearestEnemy(unit.x, unit.y)
else:
pass
if unit:
hero.cast(“lightning”, unit.x, unit.y )
else:
pass
I’ll do it for you, but surround your code with triple backticks (these → `)
also do:
enemies=hero.findEnemies()
nearest=hero.findNearest(enemies)
if nearest and nearest.type!="giant":
#lightning or fireball
You can just use
enemy = hero.findNearestEnemy()
if enemy and enemy.type != "giant":
#whatever you want here
excuse me for being a monster idiot, i’m still fairly new. I know i didn’t format this right, but did I at least have the right idea? Thank you
enemies=hero.findEnemies()
nearest=hero.findNearest(enemies)
if nearest and nearest.type!="giant":
while True:
if enemies:
hero.findNearestEnemy(enemies.x, enemies.y)
else:
pass
if enemies:
hero.cast("lightning", enemies.x, enemies.y )
else:
pass
again as crypticrystal said remove the else and also instead of if enemies do nearest
enemies=hero.findEnemies()
nearest=hero.findNearest(enemies)
if nearest and nearest.type!="giant":
while True:
if nearest and nearest.type=="#whatever type":
#do something
elif nearest:
hero.cast("lightning", enemies.x, enemies.y )
do this
You’re not an idiot! Every new person does it. It’s fine! Just remember to do it in the future.
3 Likes
you can remove the if statements and the else statements that are in the while loop (you don’t need if you have pass in an else statement you don’t need it. But keep the first one) and you can just write
hero.cast("lightning", nearest.x, nearest.y)
because you are guaranteed to have nearest because of the first if statement
enemies=hero.findEnemies() nearest=hero.findNearest(enemies)
this can be replaced with
enemy = hero.findNearestEnemy
and then just replace all of the
enemies
and
nearest
with enemy. You may also want to use hero.canCast(“lightning”) because it might be on cooldown? I’ve never done this before.
Sorry if the above doesn’t format, I’m sending this by email lol.
1 Like
Thats ok it formatted fine!
So i replaced all of the if statements except for the first one
enemy = hero.findNearestEnemy
if enemy and enemy.type!="giant":
while True:
hero.cast("lightning", enemy.x, enemy.y)
what’s weird here is it says “ArgumentError, X and Y coordinates should be numbers” even though they should be defined.
Also for some reason it doesn’t accept canCast as a function…
remove the while true
do this for every enemy not just the nearest one
while True:
enemies=hero.findEnemies()
for enemy in enemies:
friends=hero.findFriends()
if enemy.health>0 :
if enemy and enemy.type!="giant" and enemy.type!=hero.type :
if enemy.type!="munchkin" and enemy.type!="thrower" and enemy.type!="shaman":
hero.cast("lightning",enemy.x,enemy.y)
else:
hero.cast("fireball", enemy.x,enemy.y)
should be
enemy.pos.x
and enemy.pos.y
instead of enemy.x
and enemy.y
respectively
1 Like
Thank you so much, it works perfect!
2 Likes
nope in giants gate its enemy.x,enemy.y
1 Like
No, it’s enemy.pos
or enemy.pos.x # or y
like everywhere else.
nope check the arena and ask brukyh its enemy.x,enemy.y