Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (601)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
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 775 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:

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()) {
               e = null;

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

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

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

Jesse (12 views)
2015-07-29 04:35:27

Riven (35 views)
2015-07-27 16:38:00

Riven (18 views)
2015-07-27 15:35:20

Riven (20 views)
2015-07-27 12:26:13

Riven (11 views)
2015-07-27 12:23:39

BurntPizza (30 views)
2015-07-25 00:14:37

BurntPizza (41 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (27 views)
2015-07-22 22:16:53

NoxInc (18 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!