Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Passing variables through classes?

    My problem is quite simple, yet I haven't found any ways of doing this as of this moment.
    I need to get the integer "playerHP" working in a subclass. For example, I'm creating a class for all the items my text RPG needs, yet I need them to have an effect on the initial battle. This would mean that they'd for example heal the player. How do I bring the variable from the initial main to the subclass?

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    It would make it a lot easier to help you if you posted some code.

  3. #3
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    There isn't very much code displaying what I've tried to do, and the rest of the code doesn't refer to it very much, as I've had problems knowing where to start. Also, I need this for a text RPG I'm making.

    What I do have of code to display, though, is this:
    Java Code:
    if (atkordfs.equals("drink potion")){
    		        items itemsObject = new items();
    			itemsObject.Potion();
    Where atkordfs is a string that's used to execute commands, items is a class, and Potion is a public void that executes a heal.
    Java Code:
    public class items {
                    public void Potion(){
    	             System.out.print("You drink the potion, healing 50 HP");
    		     trainerHP += 50;
    Where trainerHP is the HP of the player.
    What my problem is, is basically to transfer trainerHP from the main to the Items-class.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default

    It sounds like you're at an early stage of development. It might be a good time to rethink your object design. Objects should model the real-world objects they represent. That means they should contain the data that represents their characteristics, and provide methods that represent actions they could perform, or have performed on them.

    I'd create a class that represents your player, including all their stats and equipment. Then give it methods to alter its state, like this:

    Java Code:
    class Player {
         private int hp;
         void modifyHP(int amount) {
              hp = hp + amount;
              // test if 0 < hp < max hp
         }
    }
    Then think about what your items have in common. All of them can probably be "used" in some fashion. You could create an interface called Item that includes methods that can be called on any Item. (Or if it makes sense to have code in common between Items, make Item a class.) Either way, your potion could look something like this:

    Java Code:
    class Potion implements Item {
         private boolean used = false;
         void useItem(Player target) {
              if(used == false) {
                   target.modifyHP(50);
                   used = true;
              }
         }
    }
    Then when your player drinks a potion, it might look like this:

    Java Code:
    Player self = new Player();
    Item[] inventory = new Item[20];
    inventory[0] = new Potion();
    // yadda yadda
    inventory[0].useItem(self);
    Last edited by kjkrum; 04-27-2011 at 12:37 AM. Reason: bugs in fake code :-P

  5. #5
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

Similar Threads

  1. A bit of confusion with passing variables/infinite loop
    By kaizen9001 in forum New To Java
    Replies: 12
    Last Post: 03-27-2011, 09:05 PM
  2. please help with passing variables between class's
    By jasonwucinski in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 01:27 AM
  3. Passing dynamic variables between classes
    By Brekk in forum New To Java
    Replies: 1
    Last Post: 03-22-2010, 06:07 AM
  4. passing variables to another frame
    By Grom in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 05:55 PM
  5. Replies: 0
    Last Post: 11-10-2007, 04:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •