Removing game components

Coordinator
Mar 25, 2011 at 7:27 AM

I've been trying to switch from one screen to another but I can't seem to unload or clear previously added components from the engine's component manager. 

After I exit a screen, and then try to load a new one, I get this error:

 

Here's a code snippet of the different things I have tried. None of these worked on their own or in any combination with each other.

scene.ExitScreen();            
            engine.ScreenManager.RemoveScreen(scene);
            engine.Components.Clear();
            engine.Dispose();
            this.engine.Game.Components.Clear();
            engine.RemoveComponent("MessageDeliver");
            this.engine.Components.Clear();
            this.engine.Dispose();

            TriggerBepuScreen scene1 = new TriggerBepuScreen(engine);
            engine.ScreenManager.AddScreen(scene1);

I'm sure I'm missing something really simple.

Coordinator
Mar 25, 2011 at 4:33 PM

its because you have already added the component in the other screen,

and you cant add two igual components (components are global, in the sense that screens share then)

there is 2 solutions.

the first is: 

InputAdvanced inp = new InputAdvanced(engine);          

if (!engine.HasComponentAdded(inp.getMyName()))        

{

                engine.AddComponent(inp);                            

}

///Check to see if the component is already added.

//if you know the name, you dont even need to create the component

 

the second solution is:

When you leave a screen, you call the method ExitScreen() of the IScreen

for example: 

if(keyboard.getState().isKeyDown(Keys.Escape))

{

ExitScreen();

so you can override the method ExitScreen of the IScreen like:

public override void ExitScreen()        {            

engine.RemoveComponent(component.getMyName());

base.ExitScreen();        

}

to remove the component.

 

 

 

 

Coordinator
Mar 25, 2011 at 4:37 PM

im making a simple sample about this right now

wait a little

Coordinator
Mar 25, 2011 at 5:32 PM

I'll hold off for a sec.

Two things though:

1) You are saying that game components are reusable from one screen to another? If the game component already exists from a previous screen, should I keep reusing that component until I don't need it anymore? If so how do I access it from another screen?

2) I thought that for components like MessageDeliver, that the default name the engine assigns them when added is "MessageDeliver". It seemed to me that naming the components using strings was done so you could access the component from anywhere.

Thanks for the help. :)

Coordinator
Mar 25, 2011 at 5:56 PM

1) You are saying that game components are reusable from one screen to another? If the game component already exists from a previous screen, should I keep reusing that component until I don't need it anymore? If so how do I access it from another screen?

Yes, you are not suposed to contact components directly, you should use commands (ICOMMAND) for this.

The MessageDeliver is an exception, i put it static because its easier for the user. imagine, every time you need to send a message you would have to build a command, get the commandProcessor and send the command (like the Input component) its boring ....

2) I thought that for components like MessageDeliver, that the default name the engine assigns them when added is "MessageDeliver". It seemed to me that naming the components using strings was done so you could access the component from anywhere.

you can acess them, but you should not,

the ComponentManager has a function called GetComponent(String name) that returns the IComponent (in the current release you can)

---

to remove a screen and the component, use just this:

scene.ExitScreen();                        
engine.RemoveComponent("MessageDeliver");

There was an error by my side, the engine.RemoveComponent is not removing the component entirely, iam fixing.

Iam working in a new release right now, it will take some time (one hours) to put it in the server

 if you want it now mail me (i send you the dll ) thiagodiaspastor@gmail.com (if you have a gmail, better, you can chat !!! )

 

 

 

Coordinator
Mar 25, 2011 at 9:05 PM

 fixed, just uploading the files right now

  Adding twice a component no longer is an error (it wont add twice, and will return false when you call addcomponent)

  Remove components now works as it should, remove completely the component.

  to remove a screen use:

scene.ExitScreen();                        
to add clean up code to a screen just extend the method CleanUp() of the IScreen (this method is called when you exit the screen, you dont overrides ExitScreen anymore)
to remove a component use 
engine.RemoveComponent(COMPONENT_NAME);
to get a component use 
engine.GetComponent(COMPONENT_NAME); ///return an IComponent, cast to the right one

Fixed all the demos (introduction and advanced). Also fixed the docs and dlls
Added a new Demo about ScreenStates and how to use cleanup tp remove a component

Thanks for telling me about the problem.