pointers and ArrayLists problem

hi, iam using an array list to store an object of class entity, which is a parent to classes weapon and tribute. i want to be able to do:

entitiesList.get(currentEntity.getID()).pickUp();

where pickUp is a function of the subclass weapon; however i cannot do that as i get a, "function doesnt exist" error, now i havent actually tried it but would

weapon temp = (weapon)entitiesList.get(currentEntity.getID()); temp.pickUp();

that work ? would the weapon object created point to the original, or would it be a new object ? if it is the latter, then is there any way i can store the originals in one arraylist and then use two other arraylists to store weapons and tributes as pointers to the original array ?,

Answers

  • Try it

    You might find that that casts entitiesList to be a weapon and then has problems calling get on it.

    If everything in the list is a weapon then you can tell it that and not have to cast when you fetch:

    List<Weapon> weaponList = new ArrayList<Weapon>();
    

    You'll notice that Java classes traditionally start with an uppercase letters...

  • edited May 2016

    ... , or would it be a new object?

    In almost all cases in Java, in order to create some object, we need to use its keyword new:
    https://Processing.org/reference/new.html

    List's get() method doesn't use new internally:
    http://docs.oracle.com/javase/8/docs/api/java/util/List.html#get-int-

    So you can rest assured the object returned by it is the very same you had add() to it in that index.

    Moreover (cast) operators over objects merely change the perception the Java thinks its datatype is.

    In other words, it doesn't change its actual datatype. But it can restrict or expand the members we can access from it from that moment on though. :-\"

    Now you say that variable entitiesList is a List container which stores references of datatype Entity.

    However you also mention that you actually store datatypes Weapon & Tribute in it instead.

    It means that every time you need to access some member which isn't present in the Entity class, you're gonna need to (cast) the element to its actual specialized datatype.

    At the particular mentioned case above, Weapon's pickUp() method.

    However if it happens that all of your Entity subclasses need that pickUp() method, you can make it a part of Entity itself.

    You can either implement it right there in Entity or declare both Entity & pickUp() as abstract.
    Then leave to all of its subclasses the obligation to implement it. *-:)

  • edited May 2016

    thank you for your quick reply :) the problem here is, that weapon and tribute differ quite a lot, but have position, velocity, and other trivial attributes in common. i want to just use one table to store all of the entities present as it saves me on the code in collision detection(till today i used normal arrays and stored the ID's of objects as references. but i wanted to use arrayLists with actuall objects this time). the pickUp method should be only present in weapon or it will defeat the purpose :c so the solutions to this would be: -to be able to reference to objects and not instantiate new ones. for example i could have two separate tables, for normal purposes that will point to the original table with all the entities which i will use for collision detection. and then the other ones for the code above. however i dont see any clear way of using pointers.

    -to have the arraylist know that they contain subclasses.

    is there any class atributes that would help here ?

    the entity class will never actually be instantiated, i just want to access weapon and tribute objects from it.

  • edited May 2016

    that's very weird. i ran the code another day and now i no longer get the error :o. i cant test if it works however because i need to finish the re-writing of the code first. i'll say as soon as i find out. it seems like it doesnt even care what i put after the dot, it doesnt give me an error even if i give it a function that doesnt exist never mind, still error, just a IDE problem

  • edited May 2016

    The Entity class will never actually be instantiated, ...

    So declare it abstract as a protection against direct instantiation. L-)

    ... with all the entities which i will use for collision detection.

    Just have isColliding() method inside the Entity class, so any subclass can use it: *-:)

    // forum.Processing.org/two/discussion/16417/pointers-and-arraylists-problem
    // GoToLoop (2016-May-04)
    
    abstract class Entity {
      float x, y;
      PImage img;
    
      void script() {
        display();
        update();
      }
    
      void display() {
        image(img, x, y);
      }
    
      abstract void update();
    
      boolean isColliding(Entity e) {
        return 
          x+img.width  > e.x & x < e.x+e.img.width &
          y+img.height > e.y & y < e.y+e.img.height;
      }
    }
    
    class Weapon extends Entity {
      float vx, vy;
    
      Weapon (float xx, float yy, float sx, float sy, PImage pic) {
        x = xx;
        y = yy;
        img = pic;
      }
    
      @ Override void update() {
        x = constrain(x + vx, 0, width  - img.width);
        y = constrain(y + vy, 0, height - img.height);
      }
    }
    
  • i wish it was so simple :P i'm using a collision mesh, to save on memory, so all the collision handling is done on a separate object.

Sign In or Register to comment.