I want one device to fire an event when the velocity of a player change more than a set value and another device to receive the event fired.
I tried some solutions from here but I don’t quite understand the right way to do it.
The device receiving the event:
using { /Fortnite.com/Devices }
test_device := class(creative_device):
VelocityTrackerDevice : vel_tracker_device = vel_tracker_device{}
OnBegin<override>() : void =
# MY EVENT
VelocityTrackerDevice.CustomEvent_Velocity.Subscribe(ImSubscribed)
ImSubscribed(result : float ): void = {
Print("Velocity event -> {result}")
}
The device firing the event:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /UnrealEngine.com/Temporary }
using { /Verse.org/Concurrency }
using { /Fortnite.com/Characters }
# THESE COSTUM EVENT CLASSES ARE FROM THE LINK ABOVE
custom_cancelable<public> := class:
var Canceled : logic = false
CancelEvent : event() = event(){}
Cancel():void=
set Canceled = true
CancelEvent.Signal()
custom_subscribable<public>(t:type)<computes> := class<public>(awaitable(t)):
InnerEvent : event(t) = event(t){}
Subscribe(Callback:type {__(:t):void}):custom_cancelable=
Cancelable := custom_cancelable{}
spawn{_WaitForEvent(Callback, Cancelable)}
Cancelable
_WaitForEvent(Callback:type {__(:t):void}, Cancelable: custom_cancelable)<suspends>:void=
race:
# Wait for payload to be sent
loop:
Payload := InnerEvent.Await()
# Just in case
if(not Cancelable.Canceled?):
Callback(Payload)
else:
break # Just in case
# Cancelable got canceled
Cancelable.CancelEvent.Await()
Signal<public>(Payload:t):void =
InnerEvent.Signal(Payload)
Await<override>()<suspends>:t=
InnerEvent.Await()
# DEVICE TO TRACK PLAYERS VELOCITY
vel_tracker_device := class<concrete>(creative_device):
var PlayersLastPositionAndRotation : [agent]tuple(vector3, rotation) = map{}
var PlayersVelocity : [agent]tuple(vector3, float) = map{}
# MY EVENT
var CustomEvent_Velocity<public> : custom_subscribable(float) = custom_subscribable(float){}
@editable
DeltaTime : float = 0.0333
@editable
DeltaVelocitySignal : float = 2.0
OnBegin<override>()<suspends>:void=
spawn:
UpdatePlayersVelocity()
UpdatePlayersVelocity<internal>()<suspends>:void=
loop:
for(i := 0..GetPlayspace().GetPlayers().Length-1, PlayerAgent := GetPlayspace().GetPlayers()[i]):
if(CFortChar := PlayerAgent.GetFortCharacter[], CFortChar.IsActive[]):
CurrentPositionCharacter := CFortChar.GetTransform().Translation
CurrentRotationCharacter := CFortChar.GetViewRotation()
if(LastPosition := PlayersLastPositionAndRotation[PlayerAgent](0), LastRotation := PlayersLastPositionAndRotation[PlayerAgent](1), set PlayersVelocity[PlayerAgent] = ((CurrentPositionCharacter - LastPosition) / DeltaTime, (CurrentRotationCharacter.GetYawPitchRollDegrees()[0] - LastRotation.GetYawPitchRollDegrees()[0]) / DeltaTime), set PlayersLastPositionAndRotation[PlayerAgent] = (CurrentPositionCharacter, CurrentRotationCharacter)):
if (AmountChanged : float = Distance(LastPosition, CurrentPositionCharacter) > DeltaVelocitySignal):
#I WANT THIS EVENT TO FIRE TO ANOTHER DEVICE
CustomEvent_Velocity.Signal(AmountChanged)
else if(set PlayersLastPositionAndRotation[PlayerAgent] = (CurrentPositionCharacter, CurrentRotationCharacter)):
else:
if(set PlayersVelocity[PlayerAgent] = (vector3{}, 0.0)):
Sleep(DeltaTime)