using { /Fortnite.com/Devices } using { /Verse.org/Simulation } using { /UnrealEngine.com/Temporary/Diagnostics } # CAPSTONE: an interactive jukebox that toggles the stage music on a button. # # This pulls the whole series together. A Button Device is the jukebox face; # an Audio Player Device is the speaker stack on the stage. Press the button # and the music starts; press it again and it stops. The button's # InteractedWithEvent gives us the press; an @editable label and a playing # flag give us a real on/off jukebox a designer can drop into any club. jukebox_device := class(creative_device): # The jukebox face the player walks up to and holds-to-use. @editable JukeboxButton : button_device = button_device{} # The speaker stack that actually plays the track. @editable StageSpeakers : audio_player_device = audio_player_device{} # The hint shown on the button when a player looks at it. @editable StartLabel : string = "Play the music" @editable StopLabel : string = "Stop the music" # Tracks whether the music is currently playing, so each press toggles it. var IsPlaying : logic = false OnBegin() : void = StageSpeakers.Enable() JukeboxButton.Enable() JukeboxButton.SetInteractionText(StringToMessage(StartLabel)) # Subscribe once; the handler runs on every successful interaction. JukeboxButton.InteractedWithEvent.Subscribe(OnButtonPressed) Print("Jukebox armed") # Fires each time a player holds-to-use the jukebox button. OnButtonPressed(Agent : agent) : void = if (IsPlaying?): # It was playing — stop it and flip the label back to "Play". StageSpeakers.Stop() set IsPlaying = false JukeboxButton.SetInteractionText(StringToMessage(StartLabel)) Print("Jukebox stopped") else: # It was silent — start the track and offer "Stop" next time. StageSpeakers.Play() set IsPlaying = true JukeboxButton.SetInteractionText(StringToMessage(StopLabel)) Print("Jukebox playing") # SetInteractionText takes a `message`; this is the standard helper that # turns a plain string into the localizable message the device expects. StringToMessage(Value : string) : message = "{Value}"