Action Gameplay Helper Components
Morpheus provided components to help you network shooter-related gameplay.
Last updated
Was this helpful?
Morpheus provided components to help you network shooter-related gameplay.
Last updated
Was this helpful?
The Morpheus Platform contains components that you can add to your Morpheus Character to help you network common parts of a shooter game, such shooting a target, having the target respond, and observers being able to see the interactions. The main pieces are these two components:
Shootable Component: Used to configure and replicate behaviour related to the target of a shot, such as playing on-hit effects to everyone and taking damage.
Combat Component: Used to configure and replicate behaviour related to the actor that fired the shot, such as sending an RPC to the target and playing the shot-fired effects for everyone.
These components are not necessary to create a shooter game with the Morpheus Platform, and you can create your own networked state and RPCs instead!
However, replicating state related to shooter gameplay using midground and background properties efficiently can be tricky, so we would recommend using these components if you're new to the platform. Additionally, these components are what we use to verify that shooter-style gameplay works at scale.
First, let's look at the flow of events that happens when a player shoots a target (such as another player or replicated object). The player has a Combat Component, and the target has a Shootable Component. In practice, players usually have both a Combat and Shootable component. Shooter and Target refer to MorpheusActors that are shooting and being shot at respectively.
(Shooter's client): Shooter fires their weapon and determines the target(s), e.g. via a line trace.
(Shooter's client): Call Shooter.CombatComponent.Fire(). This replicates to observers that a shot has been fired with some data about the shot.
(Shooter's client): Call Shooter.CombatComponent.ApplyShotToTarget(). This does some validation logic, and if successful, sends an RPC to the target client (which must have a ShootableComponent) indicating the target should take damage.
(Shooter's client): Call Target.ShootableComponent.LocalValidateShot(). This is the stage for the shooter to locally predict whether the shot is a valid shot; for example, is the target alive and not immune to damage?
(Shooter's client): If the above validation passed, send RPC to target's client.
(Target's client): Call Target.ShootableComponent.AuthIsShotValid. This is the stage for the target's client to authoritatively determine whether the shot was valid, in case of mispredictions.
(Target's client): Call Target.ShootableComponent.ValidShotReceived. This authoritatively processes the effects of the shot; for example, taking damage.
(Target's client): Send an RPC to the shooter's client indicating the shot was successful and what effects the shot had.
(Shooter's client): Call Shooter.CombatComponent.OnShotAcknowledged. This is the stage to play hit markers and update kill counters.
Additionally, the above flow causes delegates to replicate in the midground and background whenever a shot is fired (from the Combat Component) and whenever a shot is received (from the Shootable Component).
The Combat Component handles the Shooter's part of the replication: firing a shot, and receiving an acknowledgment that the shot is fired. To use the Combat Component, you can directly add an M2M_CombatComponent
to your Morpheus Character class, or you can create a child component.
Typical usage is for the Shooter to call Fire
when the shot is fired, and to bind to OnShotFired
to play any visual or sound effects of the shot. Fire
allows you to pass custom data about the shot, such as a weapon index or other custom data, which is passed to OnShotFired
. OnShotFired
is replicated in the midground, so all nearby players can play the full accurate effects of the shot.
If a player sees the Shooter in the background net relevancy level, OnShotFiredBackground
is called instead. Since the background includes all players not in the fore/midground, this should be used to play any lightweight effects of the shot.
The Shooter then calls ApplyShotToTarget
, which sends a request to the target's ShootableComponent
. On the Shooter's client, the target validates whether the shot is valid, and if this passes then this is send as an RPC to the target's client. The ShootableComponent
processes the shot and sends an RPC back to the Shooter's client, which broadcasts OnShotAcknowledged
. Bind to OnShotAcknowledged
for things like hit markers.
Fire
: Call when your Shooter fires a shot. Causes the OnShotFired
events to replicate to observers with the data supplied.
FireShotNumOverride
: For an advanced use case. Each shot when fired replicates a ShotNum
to observers; a unique integer that increments with every shot. FireShotNumOverride
allows you to override the ShotNum
field that is replicated, allowing your shot to reference a previous shot, for example if your shot effects occur in multiple stages.
FireHitResult
: A utility function that calls Fire
but populates the parameters from a given FHitResult
.
ApplyShotToTarget
: Call when your Shooter's shot damages a target. Can be called on multiple targets, and doesn't need to be called simultaneously with Fire
in the case where your shot effects are separated from the shot firing (e.g. non hitscan weapons).
ApplyShotToTargetHitResult
: Similar to FireHitResult
, a helper function for ApplyShotToTarget
.
OnShotFired
: Broadcast on all clients who are in the Shooter's Foreground and Midground net relevancy levels whenever the Shooter shoots with Fire(). Bind to this to play the effects of a shot being fired such as the VFX of a muzzle flash or beam. Contains data that was passed to the Fire() function such as destination and custom data.
OnShotFiredBackground
: Broadcast on all non-auth clients in the Shooter's Background whenever the Shooter shoots. For performance reasons, this contains no data, but you can use GetBackgroundWeaponIndex
to determine the WeaponIndex
and GetBackgroundShotEndPoint
to get an approximate end destination of the shot. Bind to this to play your low fidelity, approximate effects for shots that are happening in the distance.
OnTargetHit
: Broadcast on auth client only when you shoot a target locally, before any RPCs. Bind to this to play any local prediction effects.
OnShotAcknowledged
: Broadcast on auth client when the shot has been received by the receiver's client and the effects of the shot have been sent back to the Shooter. This contains data about the effects of the shot, such as whether it killed the player. Bind to this to play shot confirmation effects, such as hit markers.
The Shootable Component handles the Target's part of replication: receiving an RPC with the data about a shot they have received; applying the effects of the shot on the Target; and replicating the effects of the shot to observers and the Shooter. To use the Shootable Component, you will probably need to subclass M2M_ShootableComponent
and add it to your Morpheus Character.
Usage is to override some functions to determine whether the shot is valid and to apply the effects of the shot. These are:
LocalIsShotValid
: Called on the Shooter's client to determine whether the Shooter's client thinks the shot is valid. Use for local prediction purposes, such as whether the Target is on the correct team, isn't already dead, or had some invulnerability ability active. If this passes, the shot data is sent as an RPC to the Target's client. The result of this validity check is also passed to Shooter.CombatComponent.OnTargetHit.
AuthIsShotValid
: If the above validity check passes and the shot is send as an RPC, this function is run on the Target's client. Use to authoritatively determine whether the shot is valid.
ValidShotReceived
: If the above validity check passes, this is called on the Target's client to process the effects of the shot. This function is the place where the player should change their state as a result of being shot, such as reducing their health. This also returns data that is sent as an RPC back to the Shooter's client to let them know what effects the shot had.
If the shot is valid, the Shootable Component broadcasts delegates. OnHitReceived
is broadcast to the midground with data relating to the shot and the data populated by CombatComponent.Fire
. Bind to this to play the effects of the Target being hit such as damage effects and animations.
OnHitReceivedBackground
is broadcast to the background with no additional data, so should be used to play lightweight, low fidelity effects of the Target being hit.
LocalIsShotValid
: Called on the Shooter's client to determine whether the Shooter's client thinks the shot is valid. Override with logic to determine if the Shooter thinks the shot is valid. If this passes, the shot proceeds to an RPC to the Target.
AuthIsShotValid
: Called on target's client to authoritatively determine whether the shot is valid. Override with logic to determine if the Target thinks the shot is valid. If this passes, the shot proceeds to ValidShotReceived
.
ValidShotReceived
: Called on the target's client if AuthIsShotValid
passes. Override to apply the effects of the shot (such as taking damage) and returning an acknowledgment with data about the effects to the shot to the Shooter's CombatComponent.
LocalOnHitReceived
: Broadcast on the Shooter's client when this actor is shot.
OnHitReceived
: Broadcast for all observers who have the Shooter in foreground or midground, with full data relating to the shot. Bind to this to play damage taken effects on the Shooter.
AuthInvalidShotReceived
: Broadcast on the Target's client if they received a shot that the Target determined was invalid.
OnHitReceivedBackground
: Broadcast for all observers who have the Shooter in the background net relevancy level. Use to play lightweight, low fidelity effects of damage taken on the shooter.