Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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] Strange reference behaviour.  (Read 598 times)
0 Members and 1 Guest are viewing this topic.
Offline Nicke

Junior Newbie





« Posted 2011-12-03 14:09:15 »

Hi everyone [let's try this again since i forgot to put in the subject line the first time] first time poster here! Wink

I am currently working on a 2d top down shooter game! Now, i have a light engine, just a Light class that deals with lighting tbh. The Light class gives light pretty much the same way Terraria does i.e. sort of floods areas with light. Now, what i want to add is for the lights to fade in *done* when created and fade out when its parent (an Entity) is destroyed. The problem is that my the parent in my Light class never is read as null after beeing set to null in my GameState class. It's structured a little bit like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
public class Light {
    private Entity parent;

    public void Light(Entity parent) {
        this.parent = parent;
    }

    public void update(long delta) {
        if(parent==null) {
            //Start adjusting the light to fade out and flag for destruction when its done.
       }
        //Do the normal lighting stuff
   }

    public boolean isFlagged() {
        //Return true when it's done fading out.
   }
}

public class GameState {
    private ArrayList<Entity> entities = new ArrayList<Entity>();
    private ArrayList<Light> lights = new ArrayList<Light>();

   public void update(long delta) {
       for(int i=0;i<entities.size();i++) {
           Entitiy e = entities.get(i);
           if(e.isFlagged()) {
               entities.remove(i);
               e = null;
           }
               e.update(delta);
       }

       for(int i=0;i<lights.size();i++) {
           Light l = lights.get(i);
           if(l.isFlagged()) {
               lights.remove(i);
               l = null;
           }
               l.update(delta);
       }
   }
}


(I guess Light.parent isnt really a parent since it doesnt extend Entity, but no matter that for now.) So again, my problem is that if(parent==null) is never true even tho im positive i set the object that is the parent in ArrayList<Entity> entities to null. This appears as strange behaviour to me since i run the game with moving entities and the light follows the properly by reference to Light.parent.  Huh  Is there a better way to see if the Entity light is following is destroyed or not?

I hope this makes sense to you and someone can explain why this behaviour is nothing strange at all  Tongue  If not I'll be happy to provide more details about the project and classes. Thanks!
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #1 - Posted 2011-12-03 15:08:20 »

The thing is "l" is a reference to an instance of Light and not Light.parent.  In order to set parent to null from your main loop, you would have to either make it accessible or create a method to do so e.g.:

1  
2  
3  
public void setNull() {
    this.parent = null
}

Then replace l = null with l.setNull.
Offline Nicke

Junior Newbie





« Reply #2 - Posted 2011-12-03 16:23:20 »

Oh, thank you for the quick reply. It sounds promicing i'll try it right away!

Thing is i wanted Entity to be oblivious to its lighting >_< ...As it is now, the Entity has no reference at all to it's lighting. Entity itself is component based [as in i'm adding components that make up the entitys type of movement and logic etc] but i decided not to add light as a component because making a fadeout effect would require the object to be hidden or simlilar for till the light has faded, and i dont like that idea ;p

To sum it up: I'm not sure how to call on setNull() then when an Entity is destroyed in an efficient manner.

Edit: I could simply put the update of lights before i update the entities, this would missplace the lights slightly but it would solve the problem if i did something like if(parent.isFlagged) and then flag the light for fadeout. I'll get back to you when i tried it. Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nicke

Junior Newbie





« Reply #3 - Posted 2011-12-03 17:31:30 »

Thanks it works now! For future reference, if anyone have a similar problem, all I did was change Light.update to:
1  
2  
3  
4  
5  
6  
    public void update(long delta) {
        if(parent.isFlagged()) {
            //Start adjusting the light to fade out and flag for destruction when its done.
       }
        //Do the normal lighting stuff
   }

And I didn't have to change the order of the entities update and lights update.

Heres what it looks like atmo: http://www.youtube.com/watch?v=pmImxvpFqJs (edit: it actually runs at ~1000 before i started fraps >_<)
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #4 - Posted 2011-12-03 18:00:13 »

Thing is i wanted Entity to be oblivious to its lighting >_<

Since you're using a generic Entity system why not have lighting just be another possible component of Entity i.e. any entity could be a light source too?

It would simplify your main loop because you would only have to iterate over one collection instead of two.

The lighting component would keep track of its fade-out time.

Cool demo btw.  I like the lighting effect on the projectiles and nice FoV too.
Offline Nicke

Junior Newbie





« Reply #5 - Posted 2011-12-03 20:11:45 »

Thanks, and well, now that you point it out...  Wink Not sure why i wanted it to be that way, and since all lightsources will be "attached" to entities, i guess it doesnt make sense for them not to be another component.
Pages: [1]
  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.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (67 views)
2014-08-11 02:49:23

BurntPizza (45 views)
2014-08-09 21:09:32

BurntPizza (36 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!