Signals in UnrealScript
Here’s an idiomatic UnrealScript example demonstrating signal handling:
class SignalHandler extends Object;
var bool bShouldExit;
function Initialize()
{
// Register for game events that simulate signals
class'GameEngine'.static.GetEngine().GameViewport.RegisterInputKey(
"Escape", OnEscapePressed);
class'GameEngine'.static.GetEngine().GameViewport.RegisterInputKey(
"Q", OnQuitPressed);
}
function OnEscapePressed()
{
`log("Escape key pressed (simulating SIGINT)");
HandleSignal();
}
function OnQuitPressed()
{
`log("Q key pressed (simulating SIGTERM)");
HandleSignal();
}
function HandleSignal()
{
`log("Signal received, preparing to exit...");
bShouldExit = true;
}
function Tick(float DeltaTime)
{
if (bShouldExit)
{
`log("Exiting...");
ConsoleCommand("exit");
}
}
defaultproperties
{
bShouldExit=false
}This UnrealScript example demonstrates a concept similar to signal handling in other languages. Since UnrealScript doesn’t have direct access to operating system signals, we simulate them using key presses within the Unreal Engine environment.
Here’s a breakdown of the code:
We define a
SignalHandlerclass that extendsObject.The
Initializefunction registers input keys to simulate signals. In this case, we use the Escape key to simulate SIGINT and the Q key to simulate SIGTERM.OnEscapePressedandOnQuitPressedfunctions are called when the respective keys are pressed. They log a message and callHandleSignal.The
HandleSignalfunction sets a flagbShouldExitto true, indicating that the game should prepare to exit.The
Tickfunction, which is called every frame, checks ifbShouldExitis true. If so, it logs an “Exiting…” message and usesConsoleCommand("exit")to quit the game.In
defaultproperties, we initializebShouldExitto false.
To use this in an Unreal Engine game:
- Create a new UnrealScript file named
SignalHandler.ucin your game’s Classes directory. - Paste the above code into the file.
- Compile your game’s scripts.
- In your game’s main GameInfo or GameMode class, create an instance of SignalHandler and call its Initialize function when the game starts.
This example demonstrates how to handle “signals” (simulated by key presses) in UnrealScript, allowing for graceful shutdown of the game when certain keys are pressed. It’s important to note that this is a simulation of signal handling within the context of an Unreal Engine game, as UnrealScript doesn’t have direct access to operating system signals.