Verse Library verse

01 Snippet

Implements a reusable generic publish-subscribe pattern with callback cancellation for handling game events across modules.

extracted-snippets/community/snippets/KNrb/fortnite-generic-events/01-snippet.verse

# Source URL: https://dev.epicgames.com/community/snippets/KNrb/fortnite-generic-events
# Local doc:  epic-docs/community/snippets/KNrb/fortnite-generic-events.md
  #Handle the event subscriptions and signaling
  generic_event(payload:type):= class(subscribable(payload),signalable(payload)):
  Subscriptions : generic_subscriptions(payload)
  Subscribe<override>(Callback:type {_(:payload):void})<transacts>:cancelable=
  var NewSub: generic_cancelable(payload) = generic_cancelable(payload){ Callback := Callback }
  var NewSubscriptions : [] generic_cancelable(payload) = Subscriptions.GetSubscriptions()
  set NewSubscriptions += array{ NewSub }
  Subscriptions.SetSubscriptions(NewSubscriptions)
  return NewSub
  Signal<override>(Payload:payload):void=
  var UncanceledSubscription : [] generic_cancelable(payload) = array{}
  for(Subscription : Subscriptions.GetSubscriptions(),not Subscription.IsCanceled()?):
  Subscription.Callback(Payload)
  set UncanceledSubscription += array{Subscription}
  Subscriptions.SetSubscriptions(UncanceledSubscription)
  #Handle cancelations
  generic_cancelable(Payload:type):=class(cancelable):
  Callback: type {_(:Payload):void}
  Cancelable: base_cancelable = base_cancelable{}
  Cancel<override>()<transacts>:void=
  Cancelable.Cancel()
  IsCanceled<public>()<transacts>:logic=
  return Cancelable.IsCanceled()
  #Base Cancelable Implementation for generic because we can't use "var" inside Parametric Types for now.
  base_cancelable<public>:= class(cancelable):
  var HasBeenCanceled<private>:logic = false
  Cancel<override>()<transacts>:void=
  set HasBeenCanceled = true

Sign in free to read the full source 🌴

This Verse Library file is members-only. Create a free account to view the complete source and download the .verse file.

Sign in with Discord

Comments

    Sign in to vote, comment, or suggest an edit. Sign in