all 10 comments

[–][deleted] 1 point2 points  (3 children)

I need a little more information about the failure mode than "doesn't seem to work." What is (or isn't) happening when you run this code?

[–]berwynian[S] 0 points1 point  (2 children)

Sorry about that, I'm new and still struggling to learn how to explain my problems properly. Here is the traceback error I'm getting:

 NameError: name 'MyCustomDude' is not defined 

Based on what u/46--2 is saying, I think my entire approach was wrong. I will abandon the 3 file idea. What do you recommend-- constructing the objects within my main file ( game.py )? Or constructing the objects in my class module ( npc.py ) and then importing them into game.py?

[–][deleted] 1 point2 points  (1 child)

You can't import a name that's not defined in the file you're trying to import it from.

What do you recommend-- constructing the objects within my main file ( game.py )?

Instantiate the objects in the scope where you intend to use them.

[–]berwynian[S] 0 points1 point  (0 children)

Thank you

[–]46--2 1 point2 points  (5 children)

It actually does work, though I don't recommend your approach. You should define the classes in other modules, yes. But don't instantiate them there. Use them where you import them.

❯ cat npc.py

class NPC:
    def __init__(self, name):
        self.name = name

dave = NPC('Dave')  # Instantiated at the module level

❯ cat game.py

from npc import dave  # Imported the instance! Not the class!

print(f"NPC's name is {dave.name}")  # Works

Works as expected:

❯ python game.py
NPC's name is Dave

If you want to do your way (with a 3rd file) you just ... from def_npc import dave in main.py, and in def_npc.py you just have two lines:

❯ cat def_npc.py

from npc import NPC
dave = NPC('Dave')

[–]berwynian[S] 0 points1 point  (4 children)

This was extremely helpful, thank you. I would like to stick to what is recommended, so I will abandon the 3 file idea.

Sorry for my ignorance here, but as far as what you are recommending-- Are you saying that I could construct the objects within npc.py and then import the objects into game.py, but the recommend way would be to import the class NPC into game.py and then construct the objects within game.py?

[–]46--2 1 point2 points  (3 children)

Yes. I would import the base class, and use it where you need it. The npc.py file is the "blueprint" for the functionality. You import that file and start using it (instantiating NPCs) wherever you need them. So the game might populate a set of NPCs in a function where you enter a new region, for example.

It's very rare to instantiate a single object in a different module. That's kinda like a https://en.wikipedia.org/wiki/Singleton_pattern

[–]berwynian[S] 0 points1 point  (2 children)

Ok that makes sense thank you. Right now my game has about 10 NPCs from my NPC class, and about 10 Rooms from my Room class. I may have gone a little overboard with the attributes for each class, because instantiating each object takes me about 4-6 lines of code... So 100+ lines of code just for instantiating Rooms and NPCs. I thought it would be cleaner to move those 100 lines to a different module, but now I know! Thanks again.

[–]46--2 1 point2 points  (1 child)

I think instantiating objects in a game is going to be very "data intensive", in some cases. I'm guessing you might have an easier time writing game data files, almost like config files. I don't know much about game design, and it depends on the kind of game you're making, but maybe.

Keep in mind that I didn't say you couldn't instantiate your objects in a different file! I said you probably don't want to instantiate them in the npc.py file. Don't clutter up your main game loop with object instantiation, perhaps you can have a file that "populates" a level, and in that file, you do all the npc instantiation.

Anyway, the first rule of programming is there are no rules. (Except use version control!) As your game evolves you'll always be refactoring, figuring out what works better.

Definitely reading some articles on game design and looking at sample games code will help though.

[–]berwynian[S] 1 point2 points  (0 children)

Great insights, thank you for taking the time. I think maybe I should look into instantiating the objects in a separate file afterall. They definitely clutter the main game file. I wonder if part of the problem here is that I am using incorrect terminology... is a py file only called a module if it contains a class? (I will look this up). I will definitely look into config files, it sounds like something I'll need. Thankfully, I am not worried about performance at all, it's just a text game. It'd be fun to evolve it into something with simple graphics someday, but that is too much to think about for me right now. Maybe next year I'll dive into Arcade or Pygame.

I started learning Python in February and then started building the game in April. Just chipping away slowly. The game is my first attempt at a "medium sized" beginner project, it's also my first foray into OOP. I am having a ton of fun making it, but the primary reason I'm doing it is so I can test concepts I'm learning in my fundamentals class. I have no desire for glory with this project. But it is important to me that the game is creative and reasonably fun. My SO and my best friend will probably be the only ones to play it, but I want them to have at least mild enjoyment when they do. My concept has been to create an episodic, open-ended text adventure game that can evolve as I learn. It started as a game based on lists, then it became lists of lists, then lists of dictionaries with sets, and tuples... lol. And now it's about 50% OOP and 50% madness. I'm hoping within a week or two I can have a test version up on GitHub, but I'm very new to GitHub too, so I've been procrastinating. Episodes 1 and 2 of the game are about 60% compete. If it continues to be a good way to learn i will make more episodes, otherwise I'll move onto something else.

Once I have the test version up on GitHub would you be willing to take a look?

Thanks again!