BurnySc2 / python-sc2

A StarCraft II bot api client library for Python 3
MIT License
491 stars 157 forks source link

No possible to call any from `self.` #158

Closed dnim closed 1 year ago

dnim commented 1 year ago

Overview

Always getting such errors

AttributeError: 'MyBot' object has no attribute 'larva'

The implementation: Took from the example from the site:

import random

from sc2 import maps
from sc2.bot_ai import BotAI
from sc2.data import Difficulty, Race
from sc2.ids.ability_id import AbilityId
from sc2.ids.unit_typeid import UnitTypeId
from sc2.ids.upgrade_id import UpgradeId
from sc2.main import run_game
from sc2.player import Bot, Computer
from sc2.position import Point2
from sc2.unit import Unit
from sc2.units import Units

MAP_TO_PLAY = "BlackburnAIE"

import sc2
from sc2.bot_ai import BotAI
from sc2.player import Bot, Computer

class MyBot(BotAI):
    async def on_step(self, iteration: int):
        print(f"This is my bot in iteration {iteration}!")
        self.larva # only this added

sc2.run_game(
    sc2.maps.get(MAP_TO_PLAY),
    [Bot(sc2.Race.Zerg, MyBot()), Computer(sc2.Race.Zerg, sc2.Difficulty.Hard)],
    realtime=False,
)

Logs:

➜  sc2-bot git:(master) ✗ /usr/local/opt/python@3.9/bin/python3.9 hydralisk_push.py
INFO:sc2.protocol:Client status changed to Status.launched (was None)
INFO:sc2.controller:Creating new game
INFO:sc2.controller:Map:     BlackburnAIE
INFO:sc2.controller:Players: Bot(Race.Zerg, <__main__.MyBot object at 0x1069bb580>), Computer Hard(Zerg, RandomBuild)
INFO:sc2.protocol:Client status changed to Status.init_game (was Status.launched)
INFO:sc2.protocol:Client status changed to Status.in_game (was None)
INFO:root:Player 1 - Bot(Race.Zerg, <__main__.MyBot object at 0x1069bb580>)
This is my bot in iteration 0!
ERROR:sc2.main:AI step threw an error
Traceback (most recent call last):
  File "/Users/sergeykorsik/Library/Python/3.9/lib/python/site-packages/sc2/main.py", line 150, in _play_game_ai
    await ai.on_step(iteration)
  File "/Users/sergeykorsik/programming/sc2-bot/hydralisk_push.py", line 24, in on_step
    self.larva
AttributeError: 'MyBot' object has no attribute 'larva'
ERROR:sc2.main:resigning due to previous error
INFO:root:Result for player 1 - Bot(Race.Zerg, <__main__.MyBot object at 0x1069bb580>): Defeat
INFO:sc2.protocol:Client status changed to Status.launched (was Status.in_game)
INFO:sc2.protocol:Client status changed to Status.quit (was Status.launched)
INFO:sc2.sc2process:kill_switch: Process cleanup
INFO:sc2.sc2process:Cleaning up...
INFO:sc2.sc2process:Cleanup complete
BurnySc2 commented 1 year ago

Do you remember the command you used to install the library? Maybe you accidently ran pip install sc2 instead of pip install burnysc2 and installed this library instead? They use the same site-packages folders, so they overwrite each other. Try pip freeze to check for installed packages.

If this is the case, you can run pip uninstall sc2 and pip install burnysc2

dnim commented 1 year ago

Hi @BurnySc2!

You was absolutely right, I've installed both while experimenting with libs. Thanks for the fast and detailed answer!