Custom game states
RED4ext offers the possibility to attach custom game states to the game. These can be used to initialize or do some things before or after the game system are available, such as scripting system.

State functions

Every state has the following functions:

OnEnter

Called immediately after the state is activated. This function is called once by the game, so be careful what you want to do here. A good example is to initalize some memory that is needed later.

Return

  • The return value is not taken in consideration. It is recommended to always return true for future proofing.

OnUpdate

Called every frame. This function can contain more complex code.

Return

  • false - if the state did not finish executing.
  • true - if the state finished executing.
If the game / custom state returns true it will not be called next frame. For example:
  • InitializationState::OnUpdate - returns false
  • CustomInitState::OnUpdate - returns true
Next frame only InitializationState::OnUpdate will be called. Same for the reverse.
The only exception from this rule is the RunningState where only the returned value by the game's state is taken into consideration. For example:
  • RunningState::OnUpdate - returns true
  • CustomRunningState::OnUpdate - returns false
Then the frame, RunningState::OnExit will be called.

OnExit

Called when the state is ending. This function is called once by the game, so be careful what you want to do here.

Return

  • The return value is not taken in consideration. It is recommended to always return true for future proofing.

Game's Life Cycle

Example

1
#include <RED4ext/RED4ext.hpp>
2
​
3
bool BaseInit_OnEnter(RED4ext::CGameApplication* aApp)
4
{
5
return true;
6
}
7
​
8
bool BaseInit_OnUpdate(RED4ext::CGameApplication* aApp)
9
{
10
static uint32_t i = 0;
11
if (i == 3)
12
{
13
return true;
14
}
15
​
16
i++;
17
return false;
18
}
19
​
20
bool BaseInit_OnExit(RED4ext::CGameApplication* aApp)
21
{
22
return true;
23
}
24
​
25
bool Shutdown_OnUpdate(RED4ext::CGameApplication* aApp)
26
{
27
static uint32_t i = 0;
28
if (i == 3)
29
{
30
return true;
31
}
32
​
33
i++;
34
return false;
35
}
36
​
37
RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::PluginHandle aHandle, RED4ext::EMainReason aReason, const RED4ext::Sdk* aSdk)
38
{
39
switch (aReason)
40
{
41
case RED4ext::EMainReason::Load:
42
{
43
RED4ext::GameState initState;
44
initState.OnEnter = &BaseInit_OnEnter;
45
initState.OnUpdate = &BaseInit_OnUpdate;
46
initState.OnExit = &BaseInit_OnExit;
47
​
48
aSdk->gameStates->Add(aHandle, RED4ext::EGameStateType::BaseInitialization, &initState);
49
50
RED4ext::GameState shutdownState;
51
shutdownState.OnEnter = nullptr;
52
shutdownState.OnUpdate = &Shutdown_OnUpdate;
53
shutdownState.OnExit = nullptr;
54
​
55
aSdk->gameStates->Add(aHandle, RED4ext::EGameStateType::Shutdown, &shutdownState);
56
​
57
break;
58
}
59
case RED4ext::EMainReason::Unload:
60
{
61
break;
62
}
63
}
64
​
65
return true;
66
}
Copied!