We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, my code not working, i dont know how to get the variable ... :/ can you tell me how ?
ArrayList<Entity> entities;
void setup() {
size(500, 500);
entities = new ArrayList <Entity>();
entities.add(new Sign("Hello!", 200, 64));
}
void draw() {
background(0);
println(entity.get(0).Sign.text); // WHY ? HOW TO GET VARIABLE FROM SIGN?
for (Entity entity : entities) {
entity.show();
entity.handle();
}
}
public class Sign extends Entity {
String text;
public Sign(String text, int mapX, int mapY) {
super(mapX, mapY, 14, 8);
this.text = text;
}
void show() {
noStroke();
fill(80);
rectMode(CORNER);
rect(xPos, yPos, w, h);
stroke(0);
line(xPos + 2, yPos + 2, xPos + w - 3, yPos + 2);
line(xPos + 2, yPos + 5, xPos + w - 3, yPos + 5);
}
void handle() {
textAlign(CENTER, CENTER);
fill(255);
text(text, xPos, yPos-32);
}
}
public abstract class Entity extends GameObject {
public Entity(int xPos, int yPos, int w, int h) {
this.xPos = xPos;
this.yPos = yPos;
this.w = w;
this.h = h;
}
abstract void show();
abstract void handle();
}
public abstract class GameObject {
int xPos, yPos;
int w, h;
abstract void show();
}
Answers
Please be a bit clearer. What's the code doing wrong? Which variable?
Your ArrayList is called 'entities' so that first bit should be
entities.get(0)
. But this will be an Entity, not a Sign, so you won't be able to get the text field.Your ArrayList needs to be a list of Sign. Then you can use
entities.get(0).text
bud text not existing i want to get the TEXT variable from Sign and print it or change it. How to make list of Sign ?
Prints duplicate :/
That's INSTEAD OF line 1.
Change line 4 as well.
Like this ? But ERROR :/
Instead of line 1.
But I'm repeating myself...
Using your original code (the new code has more errors) change line 9 to
println( ((Sign) entities.get(0)).text);
This is NOT an ideal solution but it works.
The problem is that your arraylist holds elements of type
Entity
, so when you useget(0)
it returns anEntity
object buttext
is a member of a child class so is not visible. What I have done is cast the Entity object to aSign
object sotext
is now visible.The problem with this approach is when you create new child classes from
Entity
. Assume you have created a classNotice
that extendsEntity
and we add aNotice
object to the arraylist. When this object is retrieved withget(...)
it will cause an exception when we try to cast it toSign
.Remember a child class knows about its parent but the parent class knows nothing about any child class.
QUARK I LOVE YOU !!! <3 Thanks very much
Humph.
BTW @koogs solution (working code shown below) not only works but is a better way to solve the problem.
Casting an object from a collection to a child class is not a good idea, in fact Java introduced generics to provide a type safe way to use Java collections such as
ArrayList
.The only problem now is that new child classes of
Entity
(such asNotice
) cannot be added to the array list.You need to think carefully about what classes you want and the relationships between them and then decide on how to store and access them.
continued here: https://forum.processing.org/two/discussion/26597/how-to-get-arraylist-extends-class-name
closing this