Toggled Menu Entries
How to make Check Boxes and Radio Buttons
Another type of Menu is the Checkbox style - is something on or off?
__init__ requirements
The first requirement is to tell our Menu Class that it is a checkbox menu type. We can do this in our __init__ by adding the following line:
self.data.advanced.user_interface_action_type = unreal.UserInterfaceActionType.TOGGLE_BUTTON
This will display the Menu Class as a checkbox, but we'll need to do a few extra steps to make it work!
Property to Track The Checked State
The next requirement is a variable we can use to keep track of whether our class is checked or not:
@unreal.uclass()
class PythonMenuTool(unreal.ToolMenuEntryScript):
is_checked = unreal.uproperty(bool)
Function Overrides
lastly, we'll want to declare our functions for get_check_state()
and execute()
to make use of our tracker variable:
@unreal.ufunction(override=True)
def get_check_state(self, context):
"""determine the icon to display"""
checked = unreal.CheckBoxState.CHECKED
unchecked = unreal.CheckBoxState.UNCHECKED
return checked if self.is_checked else unchecked
@unreal.ufunction(override=True)
def execute(self, context):
"""determine the icon to display"""
self.is_checked = not self.is_checked
This will update the checkbox when executed and make sure the correct state is shown:
Toggled Types
This class setup works for the following interface action types:
unreal.UserInterfaceActionType.CHECK
unreal.UserInterfaceActionType.RADIO_BUTTON
unreal.UserInterfaceActionType.TOGGLE_BUTTON
Full Class Code
@unreal.uclass()
class PythonMenuTool(unreal.ToolMenuEntryScript):
name = "checkbox"
label = "Checkbox"
tool_tip = "tool tip!"
is_checked = unreal.uproperty(bool)
def __init__(self, menu, section=""):
"""Initialize our entry for the given menu_object's section"""
super().__init__()
# Initialize the entry data
self.init_entry(
owner_name="custom_owner",
menu=menu.menu_name,
section=section,
name=self.name,
label=self.label,
tool_tip=self.tool_tip
)
self.data.advanced.user_interface_action_type = unreal.UserInterfaceActionType.BUTTON
menu.add_menu_entry_object(self)
@unreal.ufunction(override=True)
def get_check_state(self, context):
"""determine the icon to display"""
checked = unreal.CheckBoxState.CHECKED
unchecked = unreal.CheckBoxState.UNCHECKED
return checked if self.is_checked else unchecked
@unreal.ufunction(override=True)
def execute(self, context):
"""determine the icon to display"""
self.is_checked = not self.is_checked
Last updated