wessles
|
 |
«
Posted
2013-07-21 04:49:58 » |
|
So, I have a problem. I need to loop through an arraylist of Zombies. The method that does this is inside of a Zombie object. I need to loop through all zombies except itself. So I tried this: 1 2
| if(zombie != this) Do crap |
But that did not work. So what do I do? Googling has brought me nowhere.
|
|
|
|
Phased
|
 |
«
Reply #1 - Posted
2013-07-21 04:54:19 » |
|
This should work: 1 2 3
| if(!(zombie instanceof this)){
} |
|
|
|
|
Troncoso
|
 |
«
Reply #2 - Posted
2013-07-21 04:58:54 » |
|
I don't see how that would work. But, I've never tried/seen it to know. (In reference to Phased's solution)
You could just give each zombie an id and check that.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Phased
|
 |
«
Reply #3 - Posted
2013-07-21 05:04:10 » |
|
I don't see how that would work. But, I've never tried/seen it to know. (In reference to Phased's solution)
You could just give each zombie an id and check that.
It does work, I checked it with my screen manager class. for example 1
| if(monster instanceof skeleton) |
would return true if monster is a skeleton, and continue the if statement. if you used 1
| if(!(monster instanceof skeleton)) |
the if statement would only continue if the statement returned false, as the ! wants a non true value from 1
| (monster instanceof skeleton) |
|
|
|
|
Phased
|
 |
«
Reply #4 - Posted
2013-07-21 05:07:13 » |
|
I fixed it up just before you replied, it was fine until i refreshed it, guess the save didn't actually save.
|
|
|
|
Phased
|
 |
«
Reply #5 - Posted
2013-07-21 05:13:28 » |
|
monsters and skeletons was just a easier example to explain the code.
Well, what is "this", you gave us a snippet of your code.
1 2 3
| if(!(zombie instanceof this)){
} |
After re reading it a few times, I think i miss understood what you wanted, troncoso is right, you would need to have a unique ID for every single entity and compare the entitys ID with each other. What are you trying to do that will compare a zombie with its self?
|
|
|
|
ra4king
|
 |
«
Reply #6 - Posted
2013-07-21 05:14:21 » |
|
First of all, why does the Zombie class have access to an ArrayList of Zombies? Secondly, if(zombie != this) should work as that checks for instance equality, which is exactly what you want.
"instanceof" is not the answer here, and was actually used incorrectly everywhere in this thread. You can only check "instanceof" with a class type, not an object. "instanceof this" or "instanceof anotherObject" will throw an error.
|
|
|
|
ra4king
|
 |
«
Reply #7 - Posted
2013-07-21 05:46:27 » |
|
If giving the entities IDs is used only for this operation, then this is a pointless misuse of memory.
|
|
|
|
Jimmt
|
 |
«
Reply #8 - Posted
2013-07-21 15:28:42 » |
|
If giving the entities IDs is used only for this operation, then this is a pointless misuse of memory.
I have to agree. There's no reason for your Zombie class to have an ArrayList of Zombies. Then how does your horde work? Every Zombie instance has its own ArrayList of Zombies? That's startlingly inefficient.
|
|
|
|
Jimmt
|
 |
«
Reply #9 - Posted
2013-07-21 16:32:30 » |
|
Cycling through the same list/array in every single zombie instance is still a really bad idea, not to mention it doesn't make any sense (OOP wise). If you're going to do something to the zombies of the World do it either in the World class or make a separate class.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
RobinB
|
 |
«
Reply #10 - Posted
2013-07-21 16:59:22 » |
|
Look at this: (This does not go inside the zombie class ofcourse) 1 2 3 4 5 6 7
| for(int zom = 0; zom < getWorld().getZombies().size(); zom++) { for(int zom2 = zom+1; zom2 < getWorld().getZombies().size(); zom2++) { Zombie zombie1 = getWorld().getZombies().get(zom); Zombie zombie2 = getWorld().getZombies().get(zom2); } } |
* Why "zombutt instanceof Zombie"  if it has diffrent behaviour with colliding (e.g. none), dont put it in the same array.
|
|
|
|
RobinB
|
 |
«
Reply #11 - Posted
2013-07-21 18:23:48 » |
|
Sorry, minecraft hardcore has given me a psychological complex where I insult such mobs monsters by ending their name with the shape of their face.
The name does not not bother me, but instanceof does  No probelm anyways.
|
|
|
|
ReBirth
|
 |
«
Reply #12 - Posted
2013-07-22 02:05:21 » |
|
If you're on OOP: world -> horde -> zombie where horde is holding zombie arraylist. There can be more than one horde in a world and more than one zombie in a horde.
If you're on the ES like Artemis, well, it's obvious that the corresponding system will fetch entire entities with zombie component and presents it as list. You just need to design that only ONE system that will need to fetch/process that list, ONCE in a loop if you can.
|
|
|
|
Jimmt
|
 |
«
Reply #13 - Posted
2013-07-22 20:11:16 » |
|
When did I mention a horde? They all have individual AI. They don't work together.
Either way, his point still holds...
|
|
|
|
ra4king
|
 |
«
Reply #14 - Posted
2013-07-23 00:58:09 » |
|
...Managed
|
|
|
|
ReBirth
|
 |
«
Reply #15 - Posted
2013-07-23 12:46:06 » |
|
When did I mention a horde? They all have individual AI. They don't work together.
My point is that it's too mess to have a world which only single (you only have one right?) to handle all entities. Some entities share similarity that can make them to be grouped.
|
|
|
|
meingrosserfreundjo
Senior Newbie 
|
 |
«
Reply #16 - Posted
2013-07-24 15:59:14 » |
|
Thats a pun refering to the Marauder's Map in Harry Potter which had to be wiped clean with the spell "Mischief Managed"
|
|
|
|
Jimmt
|
 |
«
Reply #17 - Posted
2013-07-24 23:03:59 » |
|
No one said that because there's no reason you would ever need to do that...
|
|
|
|
HeroesGraveDev
|
 |
«
Reply #18 - Posted
2013-07-24 23:23:29 » |
|
If you look at the title, that is a reason  Yes, but if I ask "How do I make a game without a game loop?", are you going to tell me how to do that, or are you going to tell me that I shouldn't be doing it that way?
|
|
|
|
HeroesGraveDev
|
 |
«
Reply #19 - Posted
2013-07-24 23:32:08 » |
|
-snip- Why shouldn't I do it? Inefficient? What?
(Assuming you're referring to your collision code:)You're checking every possible collision twice!(If you're referring to your solution:)"!=" works fine, as long as it's the same object. If it's not the same object... you have a problem. !.equals() is a method which usually involves a few if() statements, and so will take more proccessing power then !=, which is only checking if both pointers are the same. However, the difference is tiny, and is not going to be something you need to change. But either way, the collision detection should be done outside of the zombie class, which is what most people were telling you.
|
|
|
|
HeroesGraveDev
|
 |
«
Reply #20 - Posted
2013-07-25 00:43:32 » |
|
Someone has already posted: Look at this: (This does not go inside the zombie class ofcourse) 1 2 3 4 5 6 7
| for(int zom = 0; zom < getWorld().getZombies().size(); zom++) { for(int zom2 = zom+1; zom2 < getWorld().getZombies().size(); zom2++) { Zombie zombie1 = getWorld().getZombies().get(zom); Zombie zombie2 = getWorld().getZombies().get(zom2); } } |
|
|
|
|
|