Verse API
Events, subscriptions, methods and types of the WorldUIKit module.
Everything is in the WorldUIKit module. From a component on the widget entity, get the kit with
GetComponent:
using { /Verse.org/SceneGraph }
using { WorldUIKit }
my_logic<public> := class<final_super>(component):
OnBeginSimulation<override>():void =
if (Kit := Entity.GetComponent[interactive_widget_component]):
Kit.SubscribeClicked(OnClicked)
OnClicked(Event:interactive_event):void =
Print("{Event.Element} clicked")Keep the returned interactive_subscription and call Cancel() on it in OnEndSimulation, as the
generated logic file does.
Events
| Event | Payload | Sent when |
|---|---|---|
ClickedEvent | interactive_event | Every click of an enabled element: buttons, toggles, choices, tabs, steppers, and sliders when fire is released. |
ChangedEvent | interactive_event | A click changes a state: a toggle switches, a choice or tab gets selected, a tab changes the screen, a slider value changes (also while it is dragged). |
HoveredEvent | interactive_hover | A player's crosshair arrives on an element (Hovered true) or leaves it (Hovered false). |
- When a click changes a state,
ChangedEventis sent beforeClickedEvent. - A stepper sends
ClickedEventwith its own name, andChangedEventwith the name of the slider it moves. In both,Valueis the new value of the slider. - States changed from Verse (
SetOn,SetValue,SetScreen) send no event.
The events are Verse events: Await() them in a suspends function, or use the subscriptions below.
Subscriptions
SubscribeClicked<public>(Callback:type{_(:interactive_event):void})<transacts>:interactive_subscription
SubscribeChanged<public>(Callback:type{_(:interactive_event):void})<transacts>:interactive_subscription
SubscribeHovered<public>(Callback:type{_(:interactive_hover):void})<transacts>:interactive_subscriptioninteractive_subscription.Cancel() stops the calls.
Methods
| Method | Description |
|---|---|
ElementNames()<transacts>:[]string | Names of the elements, in layout order. |
IsOn[Name:string, ?Player:?player]<transacts><decides>:void | Succeeds when the toggle is On, or the choice or tab is selected. |
GetValue(Name:string, ?Player:?player)<transacts>:float | Value of a slider (0.0 for an unknown name). |
GetScreen(?Player:?player)<transacts>:int | The active screen. |
SetOn(Name:string, On:logic, ?Player:?player):void | Switches a toggle, or selects a choice or tab. A tab turned on opens its screen. |
SetValue(Name:string, Value:float, ?Player:?player):void | Sets a slider, snapped to its Step. |
SetScreen(Screen:int, ?Player:?player):void | Opens a screen and selects the tab that opens it. |
SetEnabled(Name:string, Enabled:logic):void | Enables or disables an element, for everyone. |
SetPrompt(Name:string, Prompt:string):void | Changes the interact prompt of an element, for everyone. Empty: its name. |
WidgetFor(?Player:?player)<transacts>:entity | The widget a player sees: their per-player copy, else the placed widget. Cast it to your WBP's _entity class to write your own fields. |
?Player targets one player's state when States is PerPlayer. Without it, the Set methods change every
player's state, and the reads return the state new players start with. With Shared states, there is one state and
?Player changes nothing. See Shared and per-player states.
Kit.SetOn("Music", false, ?Player := option{Event.Player})
if (Kit.IsOn["Music", ?Player := option{Event.Player}]):
Print("Music is on for this player")Your own fields
The kit writes the fields of the layout (hover, on, value…). To write other fields of your WBP from your logic (gold, a
message, a code), get the widget the player sees with WidgetFor and cast it to your WBP's _entity class:
WidgetFor<public>(?Player:?player = false)<transacts>:entityIt returns the player's per-player copy when there is one, else the placed widget (also without ?Player). From the
Shop example:
Show(Kit:interactive_widget_component, Who:player, Text:string):void =
if (W := Widgets.WBP_WorldUIKit_Shop_entity[Kit.WidgetFor(?Player := option{Who})]):
set W.Gold = GoldOf(Who)
set W.Message = InteractiveText(Text)Widgets.WBP_WorldUIKit_Shop_entity is the class of the kit's example; yours is named after your WBP's folder, e.g.
Menus.WBP_Shop_entity.
Types
interactive_event
| Field | Type | Description |
|---|---|---|
Widget | interactive_widget_component | The component that sent the event. |
Element | string | Name of the element. |
Kind | interactive_kind | Kind of the element. |
Player | player | The player who clicked. |
On | logic | Toggle, choice, tab: the new state. |
Value | float | Slider: the new value. Stepper: the new value of its slider. |
Screen | int | The screen shown to the player after the click. |
interactive_hover
| Field | Type | Description |
|---|---|---|
Widget | interactive_widget_component | The component. |
Element | string | Name of the element. |
Player | player | The player whose crosshair moved. |
Hovered | logic | true when the crosshair arrives, false when it leaves. |
Enums
interactive_kind<public> := enum{Button, Toggle, Choice, Tab, Slider, Stepper}
interactive_scope<public> := enum{Shared, PerPlayer}Layout and WBP fields
A layout is any component, next to interactive_widget_component, that implements interactive_layout. It can also
implement interactive_fields: the kit draws nothing on the widget, it calls these methods so that the WBP shows
hover, clicks and states from its own fields. The online editor writes both. Without
interactive_fields, the widget works but shows no state, and the kit says so in the log.
interactive_layout_data<public> := struct:
Width<public>:float = 0.0
Height<public>:float = 0.0
Elements<public>:[]interactive_element = array{}
interactive_layout<public> := interface:
GetLayout<public>()<transacts>:interactive_layout_data
interactive_fields<public> := interface:
SetHovered<public>(Widget:entity, Element:string, On:logic):void
SetPressed<public>(Widget:entity, Element:string, On:logic):void
SetOn<public>(Widget:entity, Element:string, On:logic):void
# Fraction: 0..1 along the slider. Text: the value as shown to players.
SetValue<public>(Widget:entity, Element:string, Value:float, Fraction:float, Text:string):void
SetEnabled<public>(Widget:entity, Element:string, On:logic):void
SetScreen<public>(Widget:entity, Screen:int):void
# A new, unplaced widget of the same WBP, for per-player copies. false: no copies.
NewCopy<public>():?entityWidget is the entity whose fields are written: the placed widget, or a player's copy. The kit only calls a method
when its value changes. InteractiveText(Value:string):message turns a string into a message for a WBP text field.