What is the best way to store global / static variables in an advanced java game?

I am a Java programmer programmer, so I do not know how to do a lot of things correctly.

I wrote some simple games (such as Asteroids, Snake, ect.), So I know how to make the basics. Now I am working on a more complex game, role-playing game with zombies. I began to write it without thinking about how I structure it. First, I created the "item" class to store data for a simple item (value, weight, damage). Then I made a class to store variables for the player (health, armor, items). As a result, I created a menu in which variables were needed on it to hold the selected menu item.

Soon I realized that I had a lot of global variables that I would need to somehow store. I will need to use variables in separate classes (for example, the class that prints on the screen must know the location of everything).

So what would be the best way to write a large number of global variables? As I said, I don’t know how to do it right, and I can’t find a good site that explains variable declarations on a large scale. Can I create a class containing all the variables and make the "Main" class a static declaration of this "VariableStorage" class?

Thanks in advance!

PS Please provide links if you can !: D

+3
source share
3 answers

, , , , , , .

, . - , - .

, :

class Player {
    String name;
    Location loc;
    List<Item> inventory;

    void pickup() {
        if (itemsOnGround.isEmpty()) {
            throw new InvalidActionException("There appears to be nothing to pick up here");
        }
        inventory.add(loc.itemsOnGround.get(0));
        loc.itemsOnGround.remove(0);
    }
}

class Location {
    List<Items> itemsOnGround;

    /** null if there isn't a monster here */
    Monster monster;
}

class Game {
    Player player;
}

class Ui {
    Game game;

    void keypressed(char key) {
        try {
            if (key == 'p') {
                game.player.pickup();
            } else {
                showKeyboardHelp();
            }
        } catch (InvalidActionException e) {
            showErrorMessage(e.getMessage());
        }
    }
}

, . ( - , ? , yendor , ?), , ( , NPC ) , - , , .

, . , , , , , . , , , , , , ( "getters" ), .

+8

, . , " " - - - , , , - , . .

? , , ObjectMap, .

-

public class Everything {
  public static HashMap<Object, Object> everythingHolder = new HashMap<Object, Object>();
}

, .

+1

, , , , . MVC . , , .

//A possible heirarchy
PlayableCharacter
|-Stats
| |-int totalHealth
| |-int currentHealth
| |-int speed
| \-int strength
|
|-List<Equipment> inventory
|-Helmet head //Helmet would be a subclass of Equipment
|-HandEquipment leftHand
|-HandEquipment rightHand
|-ChestEquipment torso
\-LegEquipment legs

, , .

public int getAttackLevel() {
  return leftHand.getAttackPoints() + 
      rightHand.getAttackPoints() + 
      stats.getStrength();
}

, - , , .

GameVariables
|-LocationVariables
| |-int worldX
| |-int worldY
| \-int worldZ
|
|-QuestVariables
| |-String questName
| \-double questProgress
|
|-GameSettings
| |-int resolutionX
| |-int resolutionY
| \-Difficulty difficultyLevel
|
| //And so on....
+1

All Articles