[solved] Strange reference behaviour.

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:



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 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. ??? 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 :stuck_out_tongue: If not I’ll be happy to provide more details about the project and classes. Thanks!

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

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

Then replace l = null with l.setNull.

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. :slight_smile:

Thanks it works now! For future reference, if anyone have a similar problem, all I did was change Light.update to:


    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 >_<)

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.

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.