Skip to content

Variable

Refaltor77 edited this page Mar 29, 2026 · 1 revision

Variable

Conditional variable system for platform-specific and state-dependent layouts.

Creation

use refaltor\ui\components\Variable;

Factory Methods

Simple Condition

$var = Variable::create("$is_using_gamepad")
    ->set('$action_text', "Press A")
    ->set('$btn_size', [200, 40]);

Inline

$var = Variable::when("$pocket_screen", [
    '$panel_width' => 200,
    '$panel_height' => 150,
]);

Methods

$var->requires("$condition");          // Set condition
$var->set('$name', $value);            // Set single variable
$var->setValues(['$a' => 1, '$b' => 2]); // Set multiple

Attaching to Elements

$panel->addVariable($variable);
$panel->addVariables([$var1, $var2, $var3]);

Common Bedrock Variables

Variable True when
$is_using_gamepad Gamepad connected
$touch Touch input
$pocket_screen Small screen (phone)
$desktop_screen Desktop screen
$is_holographic HoloLens / VR
$trial_screen Trial/demo mode
$is_pregame In menus (not in world)
$is_living_room_mode Console living room
$is_secondary_client Split screen guest
$win10_edition Windows 10
$education_edition Education Edition

Complete Example — Platform-Adaptive Layout

$panel = Panel::create("adaptive_panel")
    ->setCustomSize(['$panel_w', '$panel_h'])
    ->addVariable(
        Variable::when('$pocket_screen', [
            '$panel_w' => 200,
            '$panel_h' => 150,
            '$font' => Label::FONT_SMALL,
        ])
    )
    ->addVariable(
        Variable::when('$desktop_screen', [
            '$panel_w' => 400,
            '$panel_h' => 300,
            '$font' => Label::FONT_LARGE,
        ])
    )
    ->addVariable(
        Variable::when('$is_holographic', [
            '$panel_w' => 500,
            '$panel_h' => 400,
            '$font' => Label::FONT_EXTRA_LARGE,
        ])
    );

Input-Mode Dependent Text

$panel = Panel::create("input_hint")
    ->setSize(280, 40)
    ->addVariable(Variable::when('$is_using_gamepad', ['$hint' => "Press A"]))
    ->addVariable(Variable::when('$touch', ['$hint' => "Tap here"]))
    ->addVariable(Variable::when('(not $is_using_gamepad and not $touch)', ['$hint' => "Click here"]));

$panel->addChild(
    Label::create("hint_label", '$hint')
        ->setShadow()
        ->setAnchorFrom(Element::ANCHOR_CENTER)
        ->setAnchorTo(Element::ANCHOR_CENTER)
);

See Also

  • Element — Where variables are attached
  • Binding — Dynamic data
  • Panel — Container with variable support

Clone this wiki locally