This site requires JavaScript, please enable it in your browser!
Greenfoot back
RUMMAKER
RUMMAKER wrote ...

2014/4/19

am i using list wrong?

RUMMAKER RUMMAKER

2014/4/19

#
Hello community, I am stuck. I'm trying to make a RTS game (like "starcraft" or "age of empires") and iv decided to use the same method to get units that this scenario (http://www.greenfoot.org/scenarios/349) used (get all objects in range then remove all objects on the same faction from the list) honestly its pretty difficult reading through that code (there was alot >_<) and from what i could make out he initialized a list in his world class that contains units(which belong to a faction) and used that list to remove units belonging to the same faction when scanning for units in range. so i made some lists in my world public List<Unit> side0 = getObjects(Unit.class); and in my units class im trying to use something like this to get enemies in range public Units getNearestEnemy() List<Units> list = getObjectsInRange(attackRange, Units.class); list.removeAll(((Field)getWorld()).side0);//remove all units from same side from the list so in order to do this i have to fill my list in my world up with units form the correct factions up first right? so i tried to do this by putting
public void addedToWorld(World w)
    {
        
        if(side == 0)
        {
            ((Field)w).side0.add(this);
        }
        else if(side == 1)
        {
            ((Field)w).side1.add(this);
        }
        else
        {
            ((Field)w).side2.add(this);
        }
    }
in my Unit class and ofcourse when the unit die i will remove them from the list. but i get an error and its because of ((Field)w).side0.add(this); and i dont understand why :| i get a java.lang.NullPointerException i dont understand D: wtf makes no sense
danpost danpost

2014/4/19

#
Insert the following code before line 6 (the troublesome line).
if (side0 == null) System.out.println("the list was null");
if (w == null) System.out.println("the world was null);
Run it and when you get a message, you will have a better idea of where to look to solve it. However, unless you coded a call to the 'addedToWorld' method, the problem would most probably be the list. How do you declare and initialize the lists in your world class (show the code)?
danpost danpost

2014/4/19

#
I am not entirely sure why you would need the lists at all. You already have a way to distinguish one faction from another with the 'side' field. You should just add a 'get' method to the Units class to return the value of 'side'. Then when getting Units in range, iterate through the list and remove all the ones with the same 'side' value (or remove the ones that have a different value from the world).
for (Object obj : getObjectsInRange(attackRange, Units.class))
{
    Units unit = (Units)obj;
    if (unit.getSide() != side) getWorld().removeObject(unit);
}
davmac davmac

2014/4/19

#
danpost:
    if (unit.getSide() != side) getWorld().removeObject(unit);  
This would remove objects of a different 'side' from the world, not from the list... I would suggest instead:
List<Units> list = getObjectsInRange(attackRange, Units.class);
for (Iterator<Unit> i = list.iterator(); i.hasNext(); ) {
    Unit unit = i.next();
    if (unit.getSide() != side) i.remove();
}
danpost danpost

2014/4/19

#
davmac: My suggestion was to not use the lists at all. The lists were being used to track the members of each faction and a member of a faction was getting a list of nearby Units and removing members of its own faction, so as to remove what was left in the list (members not of its faction) from the world. EDIT: ok, well, I looked back and realized that it was never stated that the remaining Units would be removed from the world -- that the end result was to get a list of nearby Units not of the same faction. Still, my suggestion holds that the lists to track the members of each faction are not needed.
RUMMAKER RUMMAKER

2014/4/19

#
thanks for the replies il try the suggestions out, part of the reason i wanted to do it with the lists is i (think?) that its faster performance wise (is it?) because currently the game is sloooooow :C and i could use every optimization i can get.
davmac davmac

2014/4/19

#
part of the reason i wanted to do it with the lists is i (think?) that its faster performance wise (is it?)
Almost certainly not. Removing one list from another involves iterating through each element in the first list, checking whether it is in the second list (which requires iterating through that), and removing it if so. The solution I posted above is probably your best bet.
RUMMAKER RUMMAKER

2014/4/19

#
ah in that case last question is it faster to use the listIterator or is it faster to manually go through the list. ie
for(int n = 0; n < list.size(); n ++)
{
Unit u = list.get(n);
}
i just prefer it this way but if its faster to use the iterator then il switch to using that
davmac davmac

2014/4/19

#
it is faster to use the iterator (well actually, it depends on the type of list; but the iterator technique has relatively good performance in all cases).
RUMMAKER RUMMAKER

2014/4/19

#
ty for the help davmac and danpost :D.
You need to login to post a reply.