How can i cancel the DamagedEvent()? Everytime i try to set a variable of type ?cancelable. It says no roll back error? is there another way to cancel the damagedevent of a player?
Hey @A_Spycrap how are you?
I couldn’t find a way to unsubscribe from that event. But I found a cool workaround to do the same!
First of all, you need a map to store whether players have taken damage. Then you initialize that map to “false”. And after that, you can check that map before applying the rest of your code to the damaged player.
Here you have the code with comments:
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Game }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
unsubscribe_damage := class(creative_device):
# Map variable that stores whether each player (agent) has taken damage.
# Key = agent (player), Value = true/false.
var IsPlayerDamaged : [agent]logic = map{}
# --------------------------------------------------------
# - Initializes the map for all existing players
# - Subscribes to their damage events
# - Subscribes to the PlayerAddedEvent for future players
# --------------------------------------------------------
OnBegin<override>()<suspends>:void=
for (Agent : GetPlayspace().GetPlayers()):
InitializeMap(Agent)
SubscribeToDamageEvent(Agent)
GetPlayspace().PlayerAddedEvent().Subscribe(OnPlayerAddedToSession)
# --------------------------------------------------------
# Function: InitializeMap
# Sets the given player's entry in the damage tracking map to "false",
# meaning they have not yet been damaged.
# --------------------------------------------------------
InitializeMap(Agent : agent) : void =
if (set IsPlayerDamaged[Agent] = false):
# --------------------------------------------------------
# Function: OnPlayerAddedToSession
# Runs whenever a new player joins the game.
# - Adds the player to the damage tracking map (not damaged yet).
# --------------------------------------------------------
OnPlayerAddedToSession(Player : player):void =
InitializeMap(Player)
# --------------------------------------------------------
# Function: SubscribeToDamageEvent
# Subscribes a player’s FortCharacter to listen for damage events.
# - Only works if the Agent has a valid FortCharacter.
# --------------------------------------------------------
SubscribeToDamageEvent(Agent : agent) : void =
if(Character := Agent.GetFortCharacter[]):
Character.DamagedEvent().Subscribe(SurvivorDamaged)
# --------------------------------------------------------
# Function: SurvivorDamaged
# Called automatically whenever a subscribed FortCharacter takes damage.
# - Identifies the Agent (player) who owns the damaged character
# - If this is their first time taking damage, prints a message (which
# you should replace by your own code) and updates the map to mark them
# as "damaged".
# --------------------------------------------------------
SurvivorDamaged(DamageResult : damage_result) : void =
TargetDamageable := DamageResult.Target
if(TargetCharacter := fort_character[TargetDamageable]):
if(TargetAgent := TargetCharacter.GetAgent[]):
if(IsPlayerDamaged[TargetAgent] = false):
Print("Your code here")
if(set IsPlayerDamaged[TargetAgent] = true):
I added some checks and functions to make the code work with more than one player, just in case you need something like that later!
Let me know if you need more help with this!