Accessing variables in an array of different objects

I am working on a bigger project with a game that has an AI and Human player. I want to expand it so I have an array of players that I can set to whatever I want. The AI and Human classes are different in logic but have the same functions and variables. I want to access the functions and variables from both types of players in the array. When searching online I found ‘interface’ and that worked great for the functions, but not for the variables. Since I am kind of a beginner I am probably doing something wrong. Could you please explain how I can access variables in a class from my main sketch?

My not working code:

Player[] players = new Player[]{new PlayerAI(), new PlayerHuman()};

void setup() {
  players[0].printText();
  players[1].printText();
  println(players[0].myVar); // <- Warning here
  println(players[1].myVar); // <- Warning here
}

interface Player { 
  void printText();
  int myVar = 0; // Maybe something like this?
}

class PlayerAI implements Player {
  int myVar = 10;

  void printText() {
    println("I'm an AI");
  }
}

class PlayerHuman implements Player {
  int myVar = 20;

  void printText() {
    println("I'm a human");
  }
}

Output:

I'm an AI
I'm a human
0
0

Expected Output:

I'm an AI
I'm a human
10
20

EDIT:
I know I can make a function like ‘getMyVar()’ that returns that variable, but I was wondering if I could do it in an easier way without lots of functions.

1 Like

https://processing.org/reference/extends.html

idea is to not declare the variables again ( you could do new ones )
just set them at call?

1 Like
// https://Discourse.Processing.org/t/
// accessing-variables-in-an-array-of-different-objects/17324/3

// 2020-01-25

final Player[] players = {
  new PlayerAI(), 
  new PlayerHuman()
};

void setup() {
  for (final Player p : players)  println(p, p.myVar);
  exit();
}

abstract class Player { 
  int myVar;

  String toString() {
    return "I'm a " + getClass().getSimpleName();
  }
}

class PlayerAI extends Player {
  PlayerAI() {
    myVar = 10;
  }
}

class PlayerHuman extends Player {
  PlayerHuman() {
    myVar = 20;
  }
}
1 Like

Just to confirm, I just needed to use abstract and extends instead of interface and implement. Thanks for the help :smiley:

2 Likes

Exactly. Loosely speaking, interfaces specify ‘common behaviour’ for classes—methods—, not properties—member variables—; as you can read in oracle’s java 8 tutorials

In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.

So what you tried to do in your code fits best an abstract class, as you want two classes to share member variables.


Adding a method ‘getMyVar()’ to your classes would have not worked either, as it would not be part of the interface ‘Player’. On the other side, adding such method to the interface seems like a bad decision. So once again, an abstract class ‘Player’ is more suitable than an interface, as @kll has prompted :slight_smile:


Take a look at this page on abstract classes from the oracle’s java 8 tutorial

2 Likes