Magic Knight
Posts: 33
Joined: Sat Jan 12, 2013 5:14 pm

classy question

Wed Nov 30, 2016 11:58 am

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

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: classy question

Wed Nov 30, 2016 2:09 pm

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:

Code: Select all

my_room = [rm for rm in list_of_rooms if rm.number == ROOM_NUMBER][0]
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?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

PiGraham
Posts: 4276
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: classy question

Wed Nov 30, 2016 2:24 pm

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.

Magic Knight
Posts: 33
Joined: Sat Jan 12, 2013 5:14 pm

Re: classy question

Wed Nov 30, 2016 9:35 pm

Thats basically what I do at the moment. Thought there maybe a better way. Thanks MK.

Magic Knight
Posts: 33
Joined: Sat Jan 12, 2013 5:14 pm

Re: classy question

Wed Nov 30, 2016 9:38 pm

The code is on the forum. Search for text adventure or The Web of Fear.

PiGraham
Posts: 4276
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: classy question

Thu Dec 01, 2016 10:18 am

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?

User avatar
B.Goode
Posts: 10917
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: classy question

Thu Dec 01, 2016 10:44 am

Magic Knight wrote:The code is on the forum. Search for text adventure or The Web of Fear.
To save others the trouble of searching, the previous thread on this topic is here: viewtopic.php?f=32&t=143025&hilit=Web+of+fear

(Curious about why that thread got locked... )

Magic Knight
Posts: 33
Joined: Sat Jan 12, 2013 5:14 pm

Re: classy question

Thu Dec 01, 2016 2:04 pm

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.

Return to “Python”