Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  [SOLVED by RobinB]How to check if an object is not itself?  (Read 2227 times)
0 Members and 1 Guest are viewing this topic.
Offline wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Posted 2013-07-21 06: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.

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline Phased
« Reply #1 - Posted 2013-07-21 06:54:19 »

This should work:

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

}
Offline Troncoso

JGO Coder


Medals: 20



« Reply #2 - Posted 2013-07-21 06: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 07: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 wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #4 - Posted 2013-07-21 07:05:43 »

Can you clean up that reply? You messed up the, er...  Huh Idk.

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline Phased
« Reply #5 - Posted 2013-07-21 07: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 wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #6 - Posted 2013-07-21 07:09:11 »

Who said anything about monsters or skelibutts  Grin?
Well... I don't know how I would apply that. Can you put it in my terms? And also, I have the list of zombies ready, I just need to rule out the 1 in that list that is the zombie performing the check.

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #7 - Posted 2013-07-21 07:12:35 »

In other words, How could the zombie rule itself out of the list? It will still have all of the others of its type there. I think I should just give entities Ids. Should I? Sounds messy.

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline Phased
« Reply #8 - Posted 2013-07-21 07: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: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #9 - Posted 2013-07-21 07: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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #10 - Posted 2013-07-21 07:39:15 »

Yeah, like I said, not doing any of those, and I recall saying:
And also, I have the list of zombies ready, I just need to rule out the 1
Meaning, I can't use instanceof. I also said later:
I think I should just give entities Ids.
I already decided what to do.

1  
Forum.getThread(30201.0).setSolved(true);


Gnight all!

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #11 - Posted 2013-07-21 07: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: 115
Projects: 4
Exp: 3 years



« Reply #12 - Posted 2013-07-21 17: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 wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #13 - Posted 2013-07-21 18:28:35 »

NO! Oh my gosh you got this wrong...  Sad
There is a parent class called world. They can access stuff from the parent by calling
1  
getWorld()
. Then, they can do
1  
getWorld().getZombies();
to get an arraylist of all zombutts. They don't all have a storage of that! They just use this:

1  
2  
3  
4  
for(Zombie zombutt : getWorld().getZombies()) {
    if(zombutt.getId() != this.getId() && zombutt instanceof Zombie)
        Do crap;
}


Like said, case closed.

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 115
Projects: 4
Exp: 3 years



« Reply #14 - Posted 2013-07-21 18: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.
Offline wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #15 - Posted 2013-07-21 18:33:19 »

Well, what should I do? I am checking if any zombies are stuck inside each other...

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #16 - Posted 2013-07-21 18: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 wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #17 - Posted 2013-07-21 19:03:15 »

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.

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #18 - Posted 2013-07-21 19:06:54 »

Oh, and thanks!

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #19 - Posted 2013-07-21 20: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 #20 - Posted 2013-07-22 04: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 wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #21 - Posted 2013-07-22 19:42:35 »

When did I mention a horde? They all have individual AI. They don't work together.

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 115
Projects: 4
Exp: 3 years



« Reply #22 - Posted 2013-07-22 22: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 wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #23 - Posted 2013-07-23 01:53:01 »

Yeah, kinda how I check if enemies are close to the player as apposed to all enemies figuring it out Smiley. I already did that though... Mischeif...

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #24 - Posted 2013-07-23 02:58:09 »

...Managed

Offline wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #25 - Posted 2013-07-23 06:44:43 »

What?

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline ReBirth
« Reply #26 - Posted 2013-07-23 14: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 wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #27 - Posted 2013-07-23 16:35:07 »

Wait, what are you responding to?
This?
What?
I was asking about this:
...Managed
Don't worry. I know what he was saying. Just wondering what he meant by ...Managed.

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Offline meingrosserfreundjo

Senior Newbie





« Reply #28 - Posted 2013-07-24 17: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 wessles

JGO Ninja


Medals: 49
Projects: 4
Exp: 3 years


Coding with bad posture since 2011... Nonstop.


« Reply #29 - Posted 2013-07-24 21:07:02 »

Oh yeah! Lol, it's been a bit since I've read Harry Potter.

You don't know nerdiness yet; you haven't even met me!
www.wessles.com
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!