user969417
user969417

Reputation: 21

Role Playing Game program: calling input variables from another class

I have been searching for a solution for this for hours and I'm stumped. I'm very new to Java, but I am currently programming a very easy role playing game engine. In this game, the user is to type in the level of his stats. My question is: how can I make a class where this information is stored and easily referenced when the level of the stat is called for?

To be a little more specific: I want to create a variable where the user puts in the level for his skill (using the Scanner), and then I want to let the rest of the java program access the value and use it throughout the game.

I would prefer keeping the stats in another class than where the stats will be used.

Thanks in advance for the answers!

Upvotes: 2

Views: 175

Answers (1)

MaDa
MaDa

Reputation: 10762

How about

class Stats {
    private int skillLevel;
    // ... other stats
    public void setSkill(int level) { skillLevel = level }
    public int getSkill() { return skillLevel; }
}

class Player {
    private Stats stats;
    // ...
    public Stats getStats() { return stats; }
}

You cant then get skill for a player:

Player p;
// ...
p.getStats().getSkill();

Was it that simple or did I fail to notice something?

Upvotes: 1

Related Questions