adventurelib
adventurelib copied to clipboard
Room lets you use exit as a direction even though it is a method
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
I'm using Python 3.10.1 Not sure if that helps
Please can you provide a minimal reproducible example?
Please can you provide a minimal reproducible example? Oh it return error when I go in a customized direction like up-down
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
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.
Is there any way to get around that annoying thing?
Yes:
- Don't use
enter
/exit
as your directions, useenter
/out
for example - Bind the command
exit
to the direction name you use, soout
if that's what you go with:@when('exit', direction='out')
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!