• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused with references

 
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all. I'm building a game, and I'm having trouble making bullets destroy enemies, I believe because of references.

I have an ArrayList with all the Entities in the game inside. When I create a bullet, each update loop it checks each of the objects in the ArrayList, then if they are an enemy, and they intersect, I'm trying to delete the enemy object.



This is the part where I am having trouble. Up to the part where I print out the message of DEBUG, everything seems to work. However, the enemy doesn't go away when I make it equal to null. Is this because I'm not actually calling upon the enemy, or is it because null takes a while to remove the object, or what?

Thanks in advance.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are setting the enemy variable (declared in line 1) to null. You are doing nothing to the ArrayList (entityList) nor any of its elements (entity).
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is because of how java uses references.

On this line:

enemy = (Enemy)entity;

you are saying "Copy what entity points to, and save it in enemy".

It's sort of like saying "copy the address from THIS piece of paper onto THAT piece of paper".

When you do the "enemy = null" line, that's like saying "Erase the address on this piece of paper". That really doesn't do anything to the house at that address, and someone who still has that address can still find that house.
 
Alix Ollivier
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how would I make the Enemy in the ArrayList null?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because you are setting the variable to null. Doing so does not have any affect on other variables and does not have any affect on the Object itself. Only the variable you assign to null is the problem. It is similar to this:

Since you change variable b from 3 to -1, you wouldn't (shouldn't) expect the value of the variable a to change. The same thing with Object references.

If you want to remove the Enemy from the list of entities, you you need to call either List.remove() or ListIterator.remove(). You probably won't be able to do either when you iterate over the list using the advanced for loop though (you will get a ConcurrentModificationException). So you should change your loop to:
1) Get the list's ListIterator
2) Iterate over the ListIterator until you get to the Enemy
3) Call ListIterator.remove() to remove the enemy

You should also check to see where else the enemy resides. Is removing it from the list good enough to 'kill' him? Or do you also need to remove it from some other collection?

From a design perspective, I am not so sure keeping all your Entities in a single list is a good idea: you might find it more efficient to put Enemies in an ArrayList<Enemy> enemiesList, bullets in an ArrayList<Bullet> bulletList, etc... That way you don't need to check every entity to see if it is an Enemy, you just need to check the Enemies in the enemiesList to see if they intersect.
 
Alix Ollivier
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright thanks Steve. I'll try doing the ListIterator thing, cause I did get that exception you mentioned.

As for the design thing, I'm still learning. I'll probably use that next game. The main reason that I have an EntityList is because I have an abstract class Entity, and I don't want to have all of the different ArrayLists in the constructor. Not sure if I'm doing it right...
 
Alix Ollivier
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, I tried using the ListIterator, and I seem to have another bug. I can shoot one enemy, and have it disappear. However, if I shoot another, then I get a ConcurrentModificationException

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use another list to collect the objects you want to remove first, then use removeAll() to remove them from the original list.
 
Alix Ollivier
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Makes sense. I'll try it. Is it because when I delete multiple, the ArrayList size changes and it no longer reaches a point where it is looking for an object, but doesn't have any?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alix Ollivier wrote:Makes sense. I'll try it. Is it because when I delete multiple, the ArrayList size changes and it no longer reaches a point where it is looking for an object, but doesn't have any?




Nope. An iterator can remove as many items from the list as it wants during iteration. So, it should be okay to remove many items from the arraylist in one iteration.

However, you can't change the arraylist directly while iterating. So, no adding or removing elements using the arraylist object, while you are iterating. Also, you can't have two iterators changing the list at the same time. Removing items using one iterator will cause an exception with the other iterator.

Does you code do any of these?

Henry
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alix Ollivier wrote:Alright, I tried using the ListIterator, and I seem to have another bug. I can shoot one enemy, and have it disappear. However, if I shoot another, then I get a ConcurrentModificationException


I don't know if it's the cause of your problem, but you shouldn't use a for loop to decide how many times to iterate the list. The ListIterator class has a much better method for that.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alix Ollivier wrote:Makes sense. I'll try it. Is it because when I delete multiple, the ArrayList size changes and it no longer reaches a point where it is looking for an object, but doesn't have any?


You now seem to be picking through your code, fixing errors, and then finding that another one pops up. I call this the "Existential School of Programming", and it's not a very successful one.

You've been given several suggestions, and you seem to be trying to "bash" them into your current program.

My advice: StopCoding (←click) and read the relevant parts of the List (or ArrayList) API.

Then take a step back from your code and think about how you're going to have to implement the suggestions you've been given to solve the problem, and re-write that section of your class - from the ground up if necessary.

HIH

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic