adventurelib icon indicating copy to clipboard operation
adventurelib copied to clipboard

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

Open coco7nut001 opened this issue 3 years ago • 8 comments

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 avatar Jan 15 '22 00:01 coco7nut001

I'm using Python 3.10.1 Not sure if that helps

coco7nut001 avatar Jan 15 '22 00:01 coco7nut001

Please can you provide a minimal reproducible example?

lordmauve avatar Jan 15 '22 08:01 lordmauve

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

coco7nut001 avatar Jan 16 '22 08:01 coco7nut001

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

coco7nut001 avatar Jan 16 '22 08:01 coco7nut001

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.

lordmauve avatar Jan 16 '22 09:01 lordmauve

Is there any way to get around that annoying thing?

coco7nut001 avatar Jan 17 '22 10:01 coco7nut001

Yes:

  • Don't use enter/exit as your directions, use enter/out for example
  • Bind the command exit to the direction name you use, so out if that's what you go with:
    @when('exit', direction='out')
    

lordmauve avatar Jan 17 '22 11:01 lordmauve

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!

Vaporbook avatar Jan 08 '24 23:01 Vaporbook