State attached *then* initialized?

Hello all,



to solve a little problem of mine, i looked into the Appstatemanager code (my version is up to date) and found the attach(state) method:



[java] public boolean attach(AppState state){

synchronized (states){

if (!states.contains(state) && !initializing.contains(state)){

state.stateAttached(this); // Attach…

initializing.add(state); // Before initializing?

return true;

}else{

return false;

}

}

}[/java]



The idea of attaching before initializing the state seems illogical to me. Is it just me?

I mean, a state is initialized, then attached and detached a number of times, then destroyed, right?

Well, states are only intializied on the update loop, so you must wait until next loop for him to get initializied. If you need to do some logic in the stateAttached that requires it to be initializied, you’ll need to change your logic.



[java] public void update(float tpf){



// Cleanup any states pending

terminatePending();



// Initialize any states pending

initializePending();



// Update enabled states

AppState[] array = getStates();

for (AppState state : array){

if (state.isEnabled()) {

state.update(tpf);

}

}

}[/java]

Attached means it’s attached, ie: you called stateManager.attach().



Initialized means it’s initialized, ie: state manager called initialize() for you on the render thread.



You cannot be initialized and not attached… it’s impossible since attaching is what causes your state to get initialized.

Just a note: Initializing != Instantiation