Dr.Zovil's Game Lab

7/21/2010

[tutorial] How do states work in unrealscript?


GoToState(), Label, goto()
state PlayerWalking
{           
    exec function Init()
    {
       `log("exec Init() of"@GetStateName());
       GotoState('A'); // 4. Manually executed
    }
       
    event BeginState (Name PreviousStateName) // 1. From auto state by default, if (bDelayedStart=false)
    {
        `log("BeginState Event of"@GetStateName());
}    

    event EndState (Name NextStateName) // 5.
    {
       `log("EndState Event of"@GetStateName());
    }

Begin: // 2. The special label
    `log("Begin: of"@GetStateName());
    sleep (1);
    goto ('Begin'); // 3. Looping until this state ends

End:
    `log("End: of"@GetStateName());
}


state A
{     
    event BeginState (Name PreviousStateName) // 6.
    {
        `log("BeginState Event of"@GetStateName());      
    }    

    event EndState (Name NextStateName)
    {
       `log("EndState Event of"@GetStateName());
    }

Begin:
    `log("Begin: of"@GetStateName()); // 7. The special label
   
End:
    `log("End: of"@GetStateName()); // 8. Another label
}




PushState()
state PlayerWalking
{           
    exec function Init()
    {
       `log("exec Init() of"@GetStateName());
       PushState('A','End'); // 4. Manually executed
    }
       
    event BeginState (Name PreviousStateName) // 1. From auto state by default, if (bDelayedStart=false)
    {
        `log("BeginState Event of"@GetStateName());   
    }    

    event EndState (Name NextStateName) // Passed
    {
       `log("EndState Event of"@GetStateName());
    }

event PausedState () // 5. Paused
    {
       `log("PausedState Event of"@GetStateName());
    }

Begin: // 2. The special label
    `log("Begin: of"@GetStateName());
    sleep (1);
    goto ('Begin'); // 3. Looping until this state ends

End:
    `log("End: of"@GetStateName());
}



state A
{     

    event BeginState (Name PreviousStateName) // Passed

    {
        `log("BeginState Event of"@GetStateName());      
    }    

    event EndState (Name NextStateName)
    {
       `log("EndState Event of"@GetStateName());
    }

event PushedState () // 6. Pushed
    {
       `log("PushedState Event of"@GetStateName());
    }

Begin: // Passed because of the specification PushState('A','End');
    `log("Begin: of"@GetStateName());
   
End: // 7. The specified label
    `log("End: of"@GetStateName());
}


GoToState() within Function
GoToState within StateCode

state PlayerWalking
{           
    exec function Init() // Manually executed
    {
       `log("exec Init() of"@GetStateName());//4
       GoToState('A');
       `log("after exec Init() of"@GetStateName());//7
    }
       
    event BeginState (Name PreviousStateName) //1
    {
        `log("BeginState Event of"@GetStateName());   
    }    

    event EndState (Name NextStateName) //5
    {
       `log("EndState Event of"@GetStateName());
    }

Begin:
    `log("Begin: of"@GetStateName());//2  

End:
    `log("End: of"@GetStateName());//3
}



state A
{     
event BeginState (Name PreviousStateName) //6
    {
        `log("BeginState Event of"@GetStateName());      
    }    

Begin:
    `log("Begin: of"@GetStateName());//8
   
End:
    `log("End: of"@GetStateName());//9
}


state PlayerWalking
{           
event BeginState (Name PreviousStateName) //1
    {
        `log("BeginState Event of"@GetStateName());   
    }    

    event EndState (Name NextStateName) //3
    {
       `log("EndState Event of"@GetStateName());
    }

Begin:
    `log("Begin: of"@GetStateName());//2
    GoToState('A');
    `log("After Begin: of"@GetStateName());//Skipped

End:
    `log("End: of"@GetStateName());//Skipped
}

state A
{  
    event BeginState (Name PreviousStateName) //4
    {
        `log("BeginState Event of"@GetStateName());      
    }    

Begin:
    `log("Begin: of"@GetStateName());//5
   
End:
    `log("End: of"@GetStateName());//6
}


When you call GotoState from within a function, it does not go to the destination immediately, rather it goes there once execution returns back to the state code.


state PlayerWalking
{            
    exec function Init() // Manually executed
    {
       `log("exec Init() of"@GetStateName());//4
       PushState('A');
       `log("after exec Init() of"@GetStateName());//7
    }
       
    event BeginState (Name PreviousStateName) //1
    {
        `log("BeginState Event of"@GetStateName());   
    }    

    event EndState (Name NextStateName)
    {
       `log("EndState Event of"@GetStateName());
    }

    event PushedState () //5
    {
       `log("PushedState Event of"@GetStateName());    
    }

    event ContinuedState () //12
    {
    `log("ContinuedState Event of"@GetStateName());
}

Begin:
    `log("Begin: of"@GetStateName());//2
   
End:
    `log("End: of"@GetStateName());//3
}



state A
{     
    exec function Alpha()
    {
      `log("exec Alpha() of"@GetStateName());//10
      PopState(); // Manually executed
      `log("after exec Alpha() of"@GetStateName());//13
    }

    event BeginState (Name PreviousStateName)
    {
        `log("BeginState Event of"@GetStateName());      
    }    

    event EndState (Name NextStateName)
    {
       `log("EndState Event of"@GetStateName());
    }

    event PushedState () //6
    {
       `log("PushedState Event of"@GetStateName());
    }

    event PoppedState () //11
    {
       `log("PoppedState Event of"@GetStateName());
    }

Begin:
    `log("Begin: of"@GetStateName()); //8
   
End:
    `log("End: of"@GetStateName()); //9
}


state PlayerWalking
{           
    event BeginState (Name PreviousStateName) //1
    {
        `log("BeginState Event of"@GetStateName());   
    }    

    event EndState (Name NextStateName)
    {
       `log("EndState Event of"@GetStateName());
    }

    event PausedState () //3
    {
       `log("PausedState Event of"@GetStateName());
    }

    event ContinuedState () //8
    {
       `log("ContinuedState Event of"@GetStateName());
    }

Begin:
    `log("Begin: of"@GetStateName());//2
PushState('A');
    `log("After Begin: of"@GetStateName());//9

End:
    `log("End: of"@GetStateName());//10
}



state A
{     
    event BeginState (Name PreviousStateName)
    {
        `log("BeginState Event of"@GetStateName());      
    }    

    event EndState (Name NextStateName)
    {
       `log("EndState Event of"@GetStateName());
    }

    event PushedState () //4
    {
       `log("PushedState Event of"@GetStateName());
    }

    event PoppedState () //7
    {
       `log("PoppedState Event of"@GetStateName());
    }

Begin:
    `log("Begin: of"@GetStateName());//5
   
End:
    `log("End: of"@GetStateName()); //6
    PopState();
    `log("Popping at End: of"@GetStateName());
}



Tag