Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED by RobinB]How to check if an object is not itself?  (Read 12986 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.
Offline Phased
« Reply #1 - Posted 2013-07-21 04:54:19 »

This should work:

1  
2  
3  
if(!(zombie instanceof this)){

}
Offline Troncoso

JGO Coder


Medals: 20



« 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!
Legends of Yore - The Casual Retro Roguelike
Offline 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)
Offline 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.
Offline 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?

Offline ra4king

JGO Kernel


Medals: 508
Projects: 3
Exp: 5 years


I'm the King!


« 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.

Offline ra4king

JGO Kernel


Medals: 508
Projects: 3
Exp: 5 years


I'm the King!


« 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.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 167
Projects: 5
Exp: 6 years



« 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.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 167
Projects: 5
Exp: 6 years



« 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!
Legends of Yore - The Casual Retro Roguelike
Offline RobinB

JGO Ninja


Medals: 45
Projects: 1
Exp: 3 years


Spacegame in progress


« 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);
        //Colission stuff
    }
}


* Why "zombutt instanceof Zombie" Sad
if it has diffrent behaviour with colliding (e.g. none), dont put it in the same array.
Offline RobinB

JGO Ninja


Medals: 45
Projects: 1
Exp: 3 years


Spacegame in progress


« 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 Smiley
No probelm anyways.
Offline 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.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 167
Projects: 5
Exp: 6 years



« 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...
Offline ra4king

JGO Kernel


Medals: 508
Projects: 3
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2013-07-23 00:58:09 »

...Managed

Offline 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.

Offline 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"
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 167
Projects: 5
Exp: 6 years



« 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...
Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #18 - Posted 2013-07-24 23:23:29 »

If you look at the title, that is a reason  Grin

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?

Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« 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.

Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« 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);
        //Collision stuff
    }
}

                                           

Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1641 views)
2018-06-10 19:43:48

EgonOlsen (1702 views)
2018-06-10 19:43:44

EgonOlsen (1155 views)
2018-06-10 19:43:20

DesertCoockie (1583 views)
2018-05-13 18:23:11

nelsongames (1183 views)
2018-04-24 18:15:36

nelsongames (1706 views)
2018-04-24 18:14:32

ivj94 (2514 views)
2018-03-24 14:47:39

ivj94 (1731 views)
2018-03-24 14:46:31

ivj94 (2812 views)
2018-03-24 14:43:53

Solater (952 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!