Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Army Training 2, Python

Game Development 2, Army Training 2, Python

# Defeat the ogres using event handlers to command units.

# Spawn 2 "soldier"s.
game.spawnXY("soldier", 35, 20)
game.spawnXY("soldier", 45, 20)
# Spawn 2 "archer"s.
game.spawnXY("archer", 36, 22)
game.spawnXY("archer", 88, 12)
def fightEnemies(event):
    while True:
        # event.target is the unit that is executing this event handler function!
        friendUnit = event.target
        enemy = friendUnit.findNearestEnemy()
        # Have friendUnit attack() the enemy!
        friendUnit.attack(enemy)

# This attaches the fightEnemies handler to all soldiers' "spawn" events.
game.setActionFor("soldier", "spawn", fightEnemies)

# Now, attach fightEnemies to the archers' "spawn" events:
fightEnemies()

Here put instead something like this:

game.setActionFor("archer","spawn",fightEnemies)

After this put

if enemy:

And put this

In the if.
Does it work now?

1 Like

Yes, it worked. Thanks!