lordmauve / adventurelib

A minimal library for writing text adventure games in Python 3
https://adventurelib.readthedocs.io/
MIT License
152 stars 40 forks source link

Room lets you use exit as a direction even though it is a method #43

Open coco7nut001 opened 2 years ago

coco7nut001 commented 2 years ago

Whenever I do 'north', 'east', 'west' or 'south', the game crashes with an error:

Traceback (most recent call last):
  File "C:\Users\84367\Desktop\main.py", line 81, in <module>
    start()
  File "G:\Python\lib\site-packages\adventurelib.py", line 536, in start
    _handle_command(cmd)
  File "G:\Python\lib\site-packages\adventurelib.py", line 509, in _handle_command
    func(**args)
  File "C:\Users\84367\Desktop\main.py", line 53, in go
    room = current_room.exit(direction)
TypeError: 'NoneType' object is not callable
coco7nut001 commented 2 years ago

I'm using Python 3.10.1 Not sure if that helps

lordmauve commented 2 years ago

Please can you provide a minimal reproducible example?

coco7nut001 commented 2 years ago

Please can you provide a minimal reproducible example? Oh it return error when I go in a customized direction like up-down

coco7nut001 commented 2 years ago
from adventurelib import *
Room.add_direction('up', 'down')
Room.add_direction('enter', 'exit')

@when('north', direction='north')
@when('south', direction='south')
@when('east', direction='east')
@when('west', direction='west')
def go(direction):
    global current_room
    room = current_room.exit(direction)
    if room:
        current_room = room
        print(f'You go {direction}.')
        look()
tent = Room(...)
camp = Room(...)
river = Room(...)
camp.enter = tent
camp.down = river
lordmauve commented 2 years ago

The problem is that you are using exit as a direction, when it is also used as a method to get an exit. I think adventurelib should stop you using directions that conflict with methods in the Room class.

coco7nut001 commented 2 years ago

Is there any way to get around that annoying thing?

lordmauve commented 2 years ago

Yes:

Vaporbook commented 9 months ago

I got snagged on this too ... turns out I had done a copypasta from the docs, so you might want to change this line: https://github.com/lordmauve/adventurelib/blob/master/doc/rooms.rst?plain=1#L209 ...

I'm grateful for this very cool library - having a lot of fun!