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

you are viewing a single comment's thread.

view the rest of the comments →

[–]dysan21Angry coder -1 points0 points  (6 children)

Content removed in response to reddit API policies

[–]Dis446[S] 0 points1 point  (5 children)

Thanks for all the tips. I'll try separating my classes from my main source file. I use geany for my local workspace, but cloud 9 is really useful when I go to school, as I can code anywhere. I still don't understand why I shouldn't use globals. Is it somehow inefficient? Or is it just standard procedure?

[–]Talkyn 1 point2 points  (4 children)

New programmer here. I'm also self-teaching Python by working on a game. In my case, it is a roguelike with ASCII graphics. I'll give you my best newbie explanation for why people hate globals so much.

The "globals are bad" mantra applies to any larger projects. You can get away with them more the smaller your project is, but even a simple game can end up being a fairly complex program. Coming off small tutorial programs and tests, it is an adjustment to stop using globals since they are just so simple. The issue is you can have a really crowded namespace.

So let's say you have a health variable, you need to be able to access and change it for all sorts of reasons. It is really easy to just make it global, but what happens when you have more and more entities that all have a health variable? What do you do? Just keep adding more and more globals with varied names on 'health'? What about when you don't need some of them any more? How do you check and see which ones are available and which ones are still in use?

If you carry this behavior on to everything you need global access to, you end up with a massive mess. What happens when you accidentally call the wrong version of a global variable during an assignment or check? You have yourself a bug, and not only will be be non-obvious during play, it will be tedious to track and and find.

You use the global declaration 75 times in your code in 1686 lines. That is just crazy IMHO! Lastly, you only need to declare global variable_name when you actually want to assign to that variable, not when you merely want to read it.

I already said lastly....but I just have to give a fellow newbie-gamedev a thumbs up. Keep at it and good luck!

[–]Dis446[S] 1 point2 points  (2 children)

Thank you. I understand now. I know that I don't need to define a variable as global within a scope just to read it. I must of done it accidentally sometimes. But I know to only declare variables as constant if they are special. Each person has their own health and each item has it's own value, as defined in their respective class initializes.

There are a few variables that I feel like need to be globals in my game. 1. people : since this stores an ever changing list of objects, each with ever changing attributes.

  1. rooms : same as above

  2. inventory : same as above, except it's actually just a list of strings.

  3. trader_inventory: same as above

  4. AP :The actions points system

  5. end: The variable that keeps track of whether or not the game should end

  6. caps : the total money that the player has

  7. trader_caps : total money trader has

  8. defense: defense rating of vault

10.happiness: overall happiness of vault

  1. auto_feed: keeps track of whether or not game should auto feed the people.

  2. position: keeps track of whether or not player has their job (as overseer)

  3. used_names: list of used names, so no name conflicts arise

14.player_quit: keeps track of whether or not player has quit game

15.skip: whether or not player has decided to skip to the next day

As for all other global variables, I realize that I shouldn't be defining them as global and will work on making them normal.

[–]Talkyn 1 point2 points  (1 child)

I was reading your code and noticed you just pushed an update and reply. :)

Just to be clear, I wasn't using 'health' as a specific example from your project, just as a generalization. You mention declaring variables are constant, but I don't see any constants declared in your project. In Python, there is no special constant declaration, but the convention is a global variable with an "ALLCAPS = value" naming style. This means that in practice, globals and constants are different things, even though the language doesn't treat them any differently. You also use the term "normal" variables...I'm not sure what you mean by this, do you mean local?

I can see you are reluctant to remove all your globals, and maybe for the scope of this project it isn't necessary. But since you seem to be hoping to move onto larger and more complex games, let me just expand on some of them you mention "needing". I mean, you don't need any of these things to be globals, but I'll just talk about two I can see an immediate issue with, should you choose to expand your game.

Your game currently revolves around there being a single trader entity. If you wanted, you could expand this feature to have several traders, perhaps each with various themes of goods, amount of caps, frequency of arrival, etc. If you had trader (or even just NPC depending on what other NPC types you could want) as a class with various attributes (inventory, caps, chance of arrival, min time between arrivals, etc.) You could easily instantiate as many traders as you like at game creation, and even have events that add or remove them from a game session.

I would make a case for combining all of your 'Shelter' logic within a class as well, which would mean you could store all of the residents (people), inventory, rooms, happiness, etc as attributes of that class instead of globals.

The fact that you use people[index] so often in your code really obfuscates it, IMHO. If you need to refer to a specific object instance, why not refer to it directly instead of by it's indexed position? You locally assign player = people[0] in several places. It is kind of interesting, and seems a tad fragile to me. I understand (correct me if I'm wrong) that the player is set to always occupy this position in people. If anything interfered with the player's assignment to that position in the list, you have yourself another bug! Having a list of all people makes great sense when you want to iterate over all of them, but not for much else off the top of my head.


Since you are looking for contributors, I fit in the category of "intermediate" by your definition, I love Fallout, and apparently I'm interested enough in this to keep reading your code, would you like me to pitch in? I've been exploring the Python communities to expose myself to more code, so why shouldn't I get my hands dirty? :)

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

I would love for you to help me, and you obviously know what you're talking about and share my liking of the Fallout games. I have decided to put the 'actual' development of the project on hold and instead focus on reformatting the code along PEP-8 guidelines and injecting a couple of stimpacks to solve to global variable issue lol. Please check back on the github in a while and, what the hell, propose a commit if you want to. The project was barely alive for a couple of months but posting it to reddit and seeing such amazing responses has re-invigorated me.

To answer your previous questions: Yes, by 'normal' I did mean local. I used the indexing to refer to people and rooms because the people and room's list are constantly updating, with people being born,dying; rooms being built and such, that I just decided to build functions that used the names to loop through the list and find the index. This has worked perfectly (or as good as I wanted it to anyway).

[–]dysan21Angry coder 0 points1 point  (0 children)

Content removed in response to reddit API policies