I have started development on a text-based adventure game. The environment behaves like a Sierra game, but there's no graphic interface and all commands are text.
Currently, any objects in the game (such as characters, rooms, items, etc) are defined by a dictionary. The keys are the name of the data value (name, description, etc) like this:
flash_light={
'name':'Flashlight',
'desc':'A flashlight, probably belonging to an eningeer on the ship.',
'consumable':False
}
The player's inventory and the global inventory are dictionaries where the key is the 'codename' string to call the item by, and the item is the dictionary referencing it. If you wanted to, say, take an item you found in the room, the dictionary of the room has a property that tracks the inventory of the room as a list. This list contains strings that are 'codenames' for the items in it.
my_room={
'name':'My Room',
'codename':'my_room',
'desc':'A hot, stuffy room shared with 3 other passengers. My trunk is here.\
The door is on the NORTH wall.',
'access':True,
'direc':{'north':'st_hall_1'},
'inven':['flashlight']
}
After the command has decided the item you asked to take is actually IN the room, it runs:
current_pos.inven().remove(target)
player_inv.update({target:global_inv[target]})
These lines remove the string from the room's inventory list, then add a key and item to the player's inventory dictionary. The key is the string name of the object you tried to take, and the item is defined as the dictionary in the GLOBAL inventory at the string name.
The reason I have so many calls around the program is that I kept running into problems when I gave dictionaries references to other dictionaries (i.e. A room has a list of the rooms it connects to), and those dictionaries may not be defined yet. I kept the functions as general as possible, and use the string-to-call-a-dict thing to avoid giving something undefined names as a parameter.
My main question is this: How could I make this more efficient, if possible? Or, the way I built the system, would it take major restructuring to improve?