I’m having 2 issues. When I try to remove everything from my scene with:
[java]
rootNode.detachAllChildren();
LightList list = rootNode.getWorldLightList();
System.out.println("list size initially: "+list.size());
for(Light l : list)
{
rootNode.removeLight(l);
}
System.out.println("lights: "+rootNode.getWorldLightList().size());
[/java]
The printout is the same as the initial, should it not be 0 as I get the list again after removing the lights from the rootNode?
Also, I store lights in two LinkedLists, one list is a ‘global’ list of all lights (in a particular level), the other is a list that a ‘Button’ class contains so it can trigger them (turn them on and off).
I add the lights to the rootNode from the ‘global’ list of all lights, then try to remove them using the triggers list. This does not work for me, not really sure why. I have a reference ‘Object light;’, and I cast this object to SpotLight and PointLight depending on which it is, is that a problem?
I scrapped the casting business, still cannot remove the lights though.
EDIT: I gave three on my lights names with “setName()”, and upon adding them I print out their name, and upon removing I also print the name. The names are the same, yet they never get removed :S
removeLight() removes from the local list only. If any lights were added to other spatials, you need to apply removeLight() to the spatial(s) where they were added.
[java]
public class GameLight implements Activatable
{
PointLight plight;
public boolean active;
public GameLight(ColorRGBA color, float radius, Vector3f pos, boolean active)
{
plight = new PointLight();
plight.setColor(color);
plight.setPosition(pos);
plight.setRadius(radius);
this.active = active;
}
public void activate(Node rootNode)
{
if(active)
{
Game.rootNode.removeLight(plight);
active = false;
}
else
{
Game.rootNode.addLight(plight);
active = true;
}
}
public void initialise(Node rootNode)
{
if(active)
{
Game.rootNode.addLight(plight);
}
}
}
[/java]
In this class, first I call ‘initialise’ for all my lights (some start switched on), then when a ‘button’ is pressed, every light is has a reference to is activated. In this case, activate is not removing my light, I cannot figure out why
(I made my rootNode static so there just to make sure it is the same node and there is no confusion)
I did try this before I posted, here is the class with printouts
[java]
public class GameLight implements Activatable
{
private PointLight plight;
public boolean active;
public GameLight(ColorRGBA color, float radius, Vector3f pos, boolean active)
{
plight = new PointLight();
plight.setColor(color);
plight.setPosition(pos);
plight.setRadius(radius);
this.active = active;
}
public void activate(Node rootNode)
{
System.out.println("activate on light called");
if(active)
{
System.out.println("light active, removing plight");
Game.rootNode.removeLight(plight);
active = false;
}
else
{
System.out.println("light not active, adding plight");
Game.rootNode.addLight(plight);
active = true;
}
}
public void initialise(Node rootNode)
{
System.out.println("initialise method called");
if(active)
{
System.out.println("this light starts active, adding plight");
Game.rootNode.addLight(plight);
}
}
}
[/java] (I made the PointLight private so we know its not touched elsewere)
GameLight light1 = new GameLight(new ColorRGBA(1.0f,1.0f,1.0f,1f), 3.2999988f, new Vector3f(-1.5500007f, 2.7500026f, -7.1000094f), true); <-- lights creation
This is the printout I get
loading data for map----------
initialise method called
this light starts active, adding plight
activated the trigger button < ------ This is from what is essentially a class with a hitbox that causes the GameLight to be activated
activate on light called
light active, removing plight
Prints out exactly what it should, but nothing changes in the scene - the light is never removed.
When I press it again, it actually adds another light. Press again, and it removes this other light. The original light added in initialise() is always there though.
activate on light called
light active, removing plight
world light list size: 2
local light list size: 2
The light is added, its in both (I have 2 as I have 1 following the player at all times), but it cannot be removed. When I try to remove it, nothing changes. When I add it again, it can be added and removed.
Only this I have noticed is that after adding a light or removing light it is initially done in the local list, but not instantly in the world list. Can’t see this being anything to do with it though.
@jayfella said:
Doesn't iteration-based looping create a copy of the object? Try using a traditional loop instead.
Well this is more complex,
Java in fact can only do callbyCopy, meaning every parameter is copied for every method call,
BUT!!! the parameters are either primitives wich are actually copied, or they are references(eg a int or long) pointing into the heap.
So due to this logic it behaves more like a callByReference.