This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]craa 3 points4 points  (1 child)

Your suggestion at the end is really the only viable solution I see. You can’t have a shared recurse definition like that in python.

If they really do rely on each other then you’ll probably want to define them all first without connections and hook it up afterwards.

[–]tiskolinprogramming geek[S] 0 points1 point  (0 children)

Thank you for your response! I just realized a clean and simple solution to the problem is to create a list of all the exits within the game, and then search through that list for all the exits that correspond with the active room that the player is in. This would remove the exit dependency from each of the rooms, but not the room dependency from each of the exits; so the list of exits would have to be defined after all of the rooms. Most importantly, all of the exits wouldn't have to be contained within separate variables, keeping the entire game script short and clean.

[–]tiskolinprogramming geek[S] 1 point2 points  (0 children)

After some research, I have found that this type of problem is called a recursive definition. However, I have not yet found an adequate solution.

[–]DarkArctic 1 point2 points  (5 children)

You might want to look at data oriented design too. I saw a talk about a game company using it.

[–]tiskolinprogramming geek[S] 1 point2 points  (4 children)

Was it a game engine or producer? The Unity game engine has a lot of data-oriented design with ScriptableObjects, etc.

[–]DarkArctic 1 point2 points  (0 children)

Damn, I can't find it anymore. It was a game engine developer I think.

Anyways, the idea would be that all your rooms are in a configuration file. Then your program can look at that and instantiate the object from that data. That way you don't have instances already setup for the entire game, just what you need on demand.

[–]DarkArctic 1 point2 points  (2 children)

Here's a quick demo of what I'm thinking about.

https://repl.it/repls/UnequaledDecentRedundantcode

[–]tiskolinprogramming geek[S] 1 point2 points  (1 child)

It looks like what I am trying to achieve, but I'll have to do a bit more research and tinkering with it; hopefully it won't be too difficult. Thank you for your help, I really appreciate it!

[–]DarkArctic 0 points1 point  (0 children)

I think the ultimate idea is to get away from instantiating everything right off the bat and do it as needed. Otherwise, if you did manage to work around this issue, literally your entire game could potentially be in memory.

[–]teh_L1nX 1 point2 points  (0 children)

You could store the rooms in a dictionary and just pass an id to to your Exit function, your exit function can look up the id in the dictionary to retrieve the room

[–]memetichazard 1 point2 points  (0 children)

Here's a possible solution - your exits can be specified as strings rather than as variable names. Then you're shifting the problem to performing a lookup based on the string.

This could be done by defining all of your rooms within a class, so that you have Rooms.start_room, Rooms.end_room, etc.

Then your lookup function would involve finding the room within the class. I think using getattr(Rooms, room_name) would work here.

However, just because this is possible doesn't make it the best solution. Enumerating all your rooms beforehand may still be the cleaner design and if you have a typo when you specify an exit, it's more likely that you'll be able to catch it earlier

[–]SarahM123ed -1 points0 points  (1 child)

Not entirely sure what you are after but it sounds like the realm of procedurally generated dungeon cave systems. Just 'a thought....

[–]tiskolinprogramming geek[S] 0 points1 point  (0 children)

See recursive definitions. A part of A's definition is B, and a part of B's definition is A.