UE2 - UT2kX State flow

  • 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.

SuperApe

Registered Monkey
Mar 20, 2004
333
0
16
Inna Jungle
wiki.beyondunreal.com
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():
UnrealWiki said:
GotoState (optional name NewState, optional name Label)
Switches to a new state. State code execution begins at the specified label or at "Begin:" if Label is not specified. Before the state is changed the old state's EndState function is called. After the state changed BeginState of the new state is called.​
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:
ScriptLog: StartUp's EndState function was reached.
ScriptLog: the begin label has been reached.
ScriptLog: the blahblah label has been reached.
ScriptLog: the BeginState function has been reached.

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()?
 

Pfhoenix

Programmer Extraordinaire
Dec 17, 1999
60
0
0
mep.beyondunreal.com
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
 

Wormbo

Administrator
Staff member
Jun 4, 2001
5,913
36
48
Germany
www.koehler-homepage.de
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.
 

SuperApe

Registered Monkey
Mar 20, 2004
333
0
16
Inna Jungle
wiki.beyondunreal.com
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
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.