0

I've got a class named Player and there will be 200-300 instances of this class. There is a function within this class called Move and this function requires knowledge of the map.

I also have a class named Map with 1-2 instances. What is the best way to feed an instance of Map into the Player instances?

I just ask because if i feed it to the instance upon Player init so I can access the instance via self.map - won't that be creating hundreds of copies of the Map instance (one for each instance of Player)?

For all I know this could be the standard way of doing it but I have a nagging feeling that this isn't proper.

Thanks

2 Answers 2

1

If you pass the Map to the Player at init this just passes a reference, not a copy. You will not create redundant instances of Map this way, and it's the best way to do it.

Sign up to request clarification or add additional context in comments.

Comments

1

Nothing in Python is ever implicitly copied. Whenever you do x = y, whether that's a function call or a variable/element/attribute assignment, x and y afterwards refer to the same object.

I see two pitfalls with your plan, though:

  • Would the map also know about the players? If so, you'll have a lot of circular references on your hands. Just pass the map to move(), instead.
  • It seems more appropriate to have the map itself, or some other rich "game logic" class, handle the actual movement. (I'm not a game dev, though; my gut instinct is just that a thing on a map shouldn't reach outside of itself and move itself around.)

1 Comment

The thing on the map can ask or tell the map to move it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.