Hi there
I've created a text adventure game that all works fine and I'm now trying to tidy up the code a bit.
My question is, how do I obtain a class object name from one of its attributes ?
Example. I have a Room class, with room description and room number attributes.
room_001 = Room("This is the first location",1)
If I have the room I can easily get the room number with room_001.number
But, if I just have the room number how do I get the room name, in the case above from room_001.number obtain room_001
I hope this makes sense.
Thanks
-
- Posts: 33
- Joined: Sat Jan 12, 2013 5:14 pm
- elParaguayo
- Posts: 1943
- Joined: Wed May 16, 2012 12:46 pm
- Location: London, UK
Re: classy question
If each room is a separate object, then you'd need to maintain a list of all the rooms that have been created. You could then just do something like:
NB This isn't a perfect way of doing this: this assumes that there's only one room with that number and will give you an error if the room is not found.
There will be other ways of doing this too - this was just the first that came to mind for me.
Also, when you say you "have the room number" - how have you got this and why is it separated from the room itself?
Code: Select all
my_room = [rm for rm in list_of_rooms if rm.number == ROOM_NUMBER][0]
There will be other ways of doing this too - this was just the first that came to mind for me.
Also, when you say you "have the room number" - how have you got this and why is it separated from the room itself?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.
Re: classy question
Presumably you have some sort of list or array that hold information on which rooms connect to which other rooms. You could enumerate/walk that list. You could maintain an index ( a list of number, reference pair class objects) by number and/or by name with the number and a reference to the room object stored in numerical or alphabetic order.
-
- Posts: 33
- Joined: Sat Jan 12, 2013 5:14 pm
Re: classy question
Thats basically what I do at the moment. Thought there maybe a better way. Thanks MK.
-
- Posts: 33
- Joined: Sat Jan 12, 2013 5:14 pm
Re: classy question
The code is on the forum. Search for text adventure or The Web of Fear.
Re: classy question
What is the problem you are trying to solve here?
Why do you have a number or name without a reference to the class object?
Why do you have a number or name without a reference to the class object?
Re: classy question
To save others the trouble of searching, the previous thread on this topic is here: viewtopic.php?f=32&t=143025&hilit=Web+of+fearMagic Knight wrote:The code is on the forum. Search for text adventure or The Web of Fear.
(Curious about why that thread got locked... )
-
- Posts: 33
- Joined: Sat Jan 12, 2013 5:14 pm
Re: classy question
I haven't exactly got a problem, as I said everything works fine, I was just trying to tidy up the code.
I felt that some of it was a bit of a bodge as I'm just learning Python.
The query concerns the part of the code for player movement, I thought there should be a better way of doing this.
Cheers MK.
I felt that some of it was a bit of a bodge as I'm just learning Python.
The query concerns the part of the code for player movement, I thought there should be a better way of doing this.
Cheers MK.