1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Two Factor Authentication is now available on BeyondUnreal Forums. To configure it, visit your Profile and look for the "Two Step Verification" option on the left side. We can send codes via email (may be slower) or you can set up any TOTP Authenticator app on your phone (Authy, Google Authenticator, etc) to deliver codes. It is highly recommended that you configure this to keep your account safe.

UE2 - UT2kX State flow

Discussion in 'Programming' started by SuperApe, May 6, 2008.

  1. SuperApe

    SuperApe Registered Monkey

    Joined:
    Mar 20, 2004
    Messages:
    333
    Likes Received:
    0
    This has been a little unclear to me and after some searching, I've decided to open this thread to ask how the flow is directed upon state change. There is some explanation on the wiki with regards to GotoState():
    But it isn't as clear to me as I'd like.

    Say, I've got an actor in state "StartUp".

    Given:
    Code:
    state StartUp
    {
      function EndState()
      {
        Log("StartUp's EndState function was reached.");
      }
    }
    
    state NewState
    {
      function BeginState()
      {
        Log("the BeginState function has been reached.");
      }
    
    begin:
      Log("the begin label has been reached.");
    
    blahblah:
      Log("the blahblah label has been reached.");
    }
    ... I'm now realizing I should just test this code and see for myself ... I suppose I can leave this thread to help answer this question for others. :rolleyes:

    Anyway, the question was 'In what order are these points reached when I GotoState("NewState");?'.

    My guess is that we would see this in the Log:
    It's the third one I'm not so sure about. Will the flow trace through all labels shown after "begin:", or just the "begin:" label before going through BeginState()?
     
  2. Pfhoenix

    Pfhoenix Programmer Extraordinaire

    Joined:
    Dec 17, 1999
    Messages:
    60
    Likes Received:
    0
    Incorrect. You should see :

    NewState's BeginState called
    StartUp's EndState called
    then when that object next executes, the label logs will fire as state code starts executing
     
  3. Wormbo

    Wormbo Administrator Staff Member

    Joined:
    Jun 4, 2001
    Messages:
    5,909
    Likes Received:
    35
    State code is executed during actor tick, I think after Timer() and Tick().

    Example:
    Code:
    state A
    {
      function fA()
      {
        log("Before GotoState"@GetStateName());
        GotoState('B');
        log("After GotoState"@GetStateName());
      }
    
      event EndState()
      {
        log("End state A" @ GetStateName());
      }
    
    Begin:
      log("In state A");
      fA();
      log("Still in state A");
    }
    
    state B
    {
      function fB()
      {
        log("Function B in state" @ GetStateName());
      }
    
      event BeginState()
      {
        log("BeginState B"@GetStateName());
      }
    
    Begin:
      log("Begin label in state B");
    GoingOn:
      log("GoingOn label in state B");
    }
    
    Say the above code starts in state A and executes state code. You should get logs like the following:
    1. In state A
    2. Before GotoState A
    3. End state A A
    4. Begin state B B
    5. After GotoState B
    6. Begin label in state B
    7. GoingOn label in state B
    Some observations:
    • "Still in state A" will not be logged
    • State code execution continues if the state switch was performed from within state code or a function called from state code
    • Function code execution, even of a state function, continues after state switch, but in the context of the new state. Make sure you don't attempt to call any functions from state A after GotoState('B'), otherwise the game will crash!
    • State code execution of the new state will only start after all functions have returned and only if the state changed happened during state code execution of that object. If the state change happens during another object's state code execution or during an engine event (Timer, Touch, etc.), the new state's code will be executed later, after the object's own Tick() event.
    • Labels are only entry points. State code execution will only stop after GotoState, Stop, latent function calls or the end of the state.
     
  4. SuperApe

    SuperApe Registered Monkey

    Joined:
    Mar 20, 2004
    Messages:
    333
    Likes Received:
    0
    We should correct the wiki if that is the case. It states that the EndState will be called before the state changes. I think Wormbo is contradicting you in his response. Are you sure about that assertion, Pfhoenix?


    Thanks, Wormbo. These are excellent observations and descriptions. (We should integrate some of this back to the wiki when appropriate)

    I'll post back if there are any further questions on this.
     
  5. Pfhoenix

    Pfhoenix Programmer Extraordinaire

    Joined:
    Dec 17, 1999
    Messages:
    60
    Likes Received:
    0
    The quickest way to know for sure would be to setup a test script and log() everything.
     
  6. SuperApe

    SuperApe Registered Monkey

    Joined:
    Mar 20, 2004
    Messages:
    333
    Likes Received:
    0
    Right. Thanks.
     

Share This Page