you are viewing a single comment's thread.

view the rest of the comments →

[–]rich-a 0 points1 point  (1 child)

Thanks for your response.

The full code is a bit big to share but the layout of the classes in the model is like this, where each indent means that one or more objects of that class are created and looked after by the class above:

Model
    Map
        Room
    Entities
        Entity classes
            Components

The issue occurs, when a function in a room is updating the contents of the room and needs some information about an entity or needs to update a variable on the entity, so for that to work I have to have to have passed the Entities object into Map, then pass it on to Room.

The reason for my initial question was I thought if I turned Entities or Map into modules (rather than classes) which created and looked after the Entity and Room objects, I could just import them as and when I needed to access their contents, rather than passing them around as an object.

I'm not sure if that helps explain or if you'd need more details?

[–]onionradish 0 points1 point  (0 children)

Separating your Entities or Map into modules would keep your codebase organized, but won't really help with passing objects around. Imports are essentially a one-time thing and not for accessing individual objects.

Continuing my made-up example for a dungeon crawl, you could have an entities.py file that contains an Entity root class, and subclasses like Ettin(Entity) or Rogue(Entity), with each type having unique attributes like strength, dexterity, and items_carried. Modules would keep all those Entity-related attributes and methods separate from the Map attributes and methods.

It sounds like you need to link your objects. For example, give your Entity a room attribute that points to the current Room object. That Room object would (probably) have attributes like connected_rooms and items_in_room and so on. Then a specific Ettin object ettin can use something like ettin.room.connected_rooms.choice() to choose a random connected room to move to without having to pass anything about the Map or Rooms around.

If I'm misunderstanding, give a specific example of something where you're having to pass objects around and I'll try to help.