hero.spawnXY VS. game.spawnXY for adding a hero to the game - The Difference? PYTHON


#1

A question from the Game Development 2: 1.Guard Duty level. I wrote game.spawnXY instead of hero.spawnXY but it is still working as it should (this is why I did not notice the difference in the code at first). So I am wondering, since both of them works in this code, what is the difference between using hero.spawn and game.spawn when it comes to adding a hero to the game?

I wrote:
def soldierLogic():
while True:
enemy = soldier.findNearestEnemy()

    if enemy:
        soldier.attack(enemy)

    else:
         soldier.moveXY(42, 48)

This assigns your spawned unit to the soldier variable.

soldier = game.spawnXY(“soldier”, 42, 48)

soldier.on(“spawn”, soldierLogic

I compared with:
HINT 1

def soldierLogic():
while True:
enemy = soldier.findNearestEnemy()
if enemy:
soldier.attack(enemy)

soldier = hero.spawnXY(“soldier”, 42, 48)
soldier.on(“spawn”, soldierLogic)


#2

Please format your code using </>.


#3


#4

Hello there! The difference is, is that hero.spawnXY()sets whatever you spawned to be your hero. So you can move it around by clicking the map. While game.spawnXY() just spawns it as a thang with an AI controlled code and next time be sure to use </> to preformat your code so it’s easier for us to see your code. If you are unsure about how to do this, check the official FAQ.

https://discourse.codecombat.com/faq

#5

:face_with_raised_eyebrow:
Isn’t the hero spawning made with game.spawnHeroXY(x,y)?