Verse Library verse

06 Removing A Widget

Subscribes to button interaction to toggle a personalized text widget on a player's screen.

extracted-snippets/documentation/en-us/fortnite/creating-and-removing-widgets-in-unreal-editor-for-fortnite/06-removing-a-widget.verse

# Source URL: https://dev.epicgames.com/documentation/en-us/fortnite/creating-and-removing-widgets-in-unreal-editor-for-fortnite
# Local doc:  epic-docs/documentation/en-us/fortnite/creating-and-removing-widgets-in-unreal-editor-for-fortnite.md
# Section:    Removing a Widget
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/UI }
using { /Fortnite.com/UI }
using { /UnrealEngine.com/Temporary/SpatialMath}
hello_world_device := class(creative_device):
# Set the Button device in the Editor to reference the device in the level
@editable
MyButton : button_device = button_device{}
# A localizable message to display as text in the UI
TextForMyUI<localizes> : message = "Hello, world!"
# A mapping between the Player and the widget that may have been added to their UI
var MaybeMyUIPerPlayer : [player]?text_block = map{}
# Runs when the device is started in a running game
OnBegin<override>()<suspends> : void =
MyButton.InteractedWithEvent.Subscribe(HandleButtonInteraction)
# A custom UI can only be associated with a specific player, and only that player can see it
HandleButtonInteraction(Agent : agent) : void =
# Agents can be a player or AI, but you can only get the UI of a player
# so you must cast the Agent, who interacted with the Button device, to the player type
if (InPlayer := player[Agent], PlayerUI := GetPlayerUI[InPlayer]):
if (MyUI := MaybeMyUIPerPlayer[InPlayer]?):
PlayerUI.RemoveWidget(MyUI)
if (set MaybeMyUIPerPlayer[InPlayer] = false) {}
else:
NewUI := text_block{DefaultText := TextForMyUI}
PlayerUI.AddWidget(NewUI)

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