Events

Learn about events and their configuration options

Events are critical to triggering visual and sound effects and transmitting state that belongs more to an instant in time than any particular entity.

More details about the use-case for events and a step-by-step guide to setting one up can be found in the Trigger an Event guide.

USnapNetEvent

All events must derive from USnapNetEvent and then any networked properties you add will be synchronized automatically.

The event’s OnPredicted() function will trigger on the client when created as a result of client-side prediction and then, when the authoritative server state is received for that frame, the event’s OnConfirmed() or OnCanceled() function will be called depending on whether the server also triggered that event or not, respectively. Events exist in the main UWorld and will always be triggered after client simulation but before the main world ticks.

Upon construction of a USnapNetEvent subclass, there are a couple of options you can configure:

Predicted

When Predicted is set, the event functions will be called as described above. When turned off, only OnConfirmed() will be called. Prediction is on by default.

Reliable

When an event is created, it is sent to clients in the next network update. Due to the unreliable nature of packets sent on the Internet, it is possible for the packet containing that event to be dropped in rare cases. By default, if an event gets lost it will not be resent. This is often desirable for cosmetic things like bullet impacts where triggering the event late may be more strange than not playing it at all. By setting the Reliable flag, the event will be resent in every network update to clients until it is acknowledged.

Event IDs

Generating an event ID is fundamental to an event’s ability to notify the game when it has been confirmed or canceled by the server. The event ID is used to disambiguate multiple events of the same type that occur on the same frame and is generated by hashing the values of all networked properties marked with Use as Event ID.

In general, all properties that are guaranteed to have the same value between client and server should be marked Use as Event ID. Consider an event representing a bullet impact which contains the attacking entity index, victim entity index, weapon used, impact location, and impacted surface normal. The attacking entity index, victim entity index, and weapon used should all be included in the event ID because the client and server would be guaranteed to have identical values. However, due to physics and floating-point differences it’s possible that the impact locations or surface normals are slightly different between client and server simulations. For this reason, Use as Event ID is enabled by default for all properties except those that support interpolation.

Spawning events

To spawn an event, get a pointer to the relevant USnapNetSimulation and call SpawnEvent().

void AExampleEntity::Tick( float DeltaSeconds )
{
    Super::Tick( DeltaSeconds );

    if ( USnapNetSimulation* Simulation = USnapNetSimulation::Get( this ) )
    {
        if ( Simulation->WasInputActionPressed( EntityComponent->GetOwnerPlayerIndex(), ExplodeActionName ) )
        {
            if ( UExampleEvent* ExampleEvent = Simulation->SpawnEvent<UExampleEvent>( ExampleEventClass, EntityComponent ) )
            {
                ExampleEvent->Damage.SetValue( 100 );
                ExampleEvent->TargetEntityIndex.SetValue( EntityComponent->GetEntityIndex() );
            }
        }
    }
}