Aaron
Aaron

Reputation: 1016

Reference Object Created at Runtime

I have two classes: "Player" and "Armor". "Player" contains generic variables you would expect to find in an RPG game. "Armor" contains generic variables relevant to armor.

In the "armor" class, I have this method:

public boolean canEquip() {

    boolean tf = false;

    if (this.wieldLevel <= [NEED CODE HERE]) {

        tf = true;
    } else
        tf = false;

    return tf;
}

I'm trying to reference an object that wont be created until the player loads a savefile or creates a new game, at which point the Player object would be created. Is there a way to write this method correctly?

if (this.wieldLevel <= Player.getLevel())
//this doesn't seem to work.

Upvotes: 0

Views: 101

Answers (2)

scottb
scottb

Reputation: 10084

As an aside, you can simplify your entire block of the code to the following with the exact same functionality and behavior:

public boolean canEquip() {

    return (this.wieldLevel <= [NEED CODE HERE] ); 
}

Your statement that you can't check against player properties here is a little confusing for me. Why would you be checking to see if a player "canEquip" something if there is no player object available that could equip something?

Upvotes: 0

Thomas W
Thomas W

Reputation: 14164

As Ashwin says, Player should be a parameter to the canEquip() function.

public boolean canEquip (Player player) {
    return (player.getLevel() >= wieldLevel);
}

Upvotes: 3

Related Questions