You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to create a UI by adding controls directly. Things are showing up on my GUI but none of the buttons are working. I am not sure what I am doing wrong. Any help would be appreciated!
import{Button,Callable,Control,Engine,HBoxContainer,Label,Node2D,Panel,PanelContainer,printerr,VBoxContainer,Vector2,}from"godot";exportdefaultclassMainextendsNode2D{privateresearchButton: Button|null=null;privateworkshopButton: Button|null=null;privatestoryButton: Button|null=null;privatesettingsButton: Button|null=null;privateresearchPanel: Control|null=null;privateworkshopPanel: Control|null=null;privatestoryPanel: Control|null=null;privatesettingsPanel: Control|null=null;privatecontentPanels: Control[]=[];_ready(){if(Engine.is_editor_hint()){return;}constuiRoot=newControl();uiRoot.set_name("UIRoot");uiRoot.set_anchors_preset(Control.LayoutPreset.PRESET_FULL_RECT);this.add_child(uiRoot);constmainLayout=newHBoxContainer();mainLayout.set_name("MainLayout");mainLayout.set_anchors_preset(Control.LayoutPreset.PRESET_FULL_RECT);uiRoot.add_child(mainLayout);constsidebar=newPanelContainer();sidebar.set_name("Sidebar");sidebar.custom_minimum_size=newVector2(200,0);sidebar.size_flags_horizontal=Control.SizeFlags.SIZE_EXPAND;mainLayout.add_child(sidebar);constmenuItems=newVBoxContainer();menuItems.set_name("MenuItems");sidebar.add_child(menuItems);this.researchButton=newButton();this.researchButton.set_name("ResearchButton");this.researchButton.text="Research";menuItems.add_child(this.researchButton);this.workshopButton=newButton();this.workshopButton.set_name("WorkshopButton");this.workshopButton.text="Workshop";menuItems.add_child(this.workshopButton);this.storyButton=newButton();this.storyButton.set_name("StoryButton");this.storyButton.text="Story";menuItems.add_child(this.storyButton);this.settingsButton=newButton();this.settingsButton.set_name("SettingsButton");this.settingsButton.text="Settings";menuItems.add_child(this.settingsButton);constcontentArea=newControl();contentArea.set_name("ContentArea");contentArea.size_flags_horizontal=Control.SizeFlags.SIZE_EXPAND_FILL;contentArea.set_anchors_preset(Control.LayoutPreset.PRESET_FULL_RECT);mainLayout.add_child(contentArea);this.researchPanel=newPanel();this.researchPanel.set_name("ResearchPanel");this.researchPanel.set_anchors_preset(Control.LayoutPreset.PRESET_FULL_RECT);constresearchLabel=newLabel();researchLabel.set_name("ResearchLabel");researchLabel.text="Research";researchLabel.set_anchors_preset(Control.LayoutPreset.PRESET_CENTER);this.researchPanel.add_child(researchLabel);contentArea.add_child(this.researchPanel);this.workshopPanel=newPanel();this.workshopPanel.set_name("WorkshopPanel");this.workshopPanel.set_anchors_preset(Control.LayoutPreset.PRESET_FULL_RECT);constworkshopLabel=newLabel();workshopLabel.text="Workshop Content Area";workshopLabel.set_anchors_preset(Control.LayoutPreset.PRESET_CENTER);this.workshopPanel.add_child(workshopLabel);contentArea.add_child(this.workshopPanel);// Create Story and Settings panels similarly...this.storyPanel=newPanel();this.storyPanel.set_name("StoryPanel");this.storyPanel.set_anchors_preset(Control.LayoutPreset.PRESET_FULL_RECT);conststoryLabel=newLabel();storyLabel.text="Story Content Area";storyLabel.set_anchors_preset(Control.LayoutPreset.PRESET_CENTER);this.storyPanel.add_child(storyLabel);contentArea.add_child(this.storyPanel);this.settingsPanel=newPanel();this.settingsPanel.set_name("SettingsPanel");this.settingsPanel.set_anchors_preset(Control.LayoutPreset.PRESET_FULL_RECT);constsettingsLabel=newLabel();settingsLabel.text="Settings Content Area";settingsLabel.set_anchors_preset(Control.LayoutPreset.PRESET_CENTER);this.settingsPanel.add_child(settingsLabel);contentArea.add_child(this.settingsPanel);// Store valid panels in the arraythis.contentPanels=[this.researchPanel,this.workshopPanel,this.storyPanel,this.settingsPanel,];if(!this.researchButton||!this.workshopButton||!this.storyButton||!this.settingsButton){printerr("Error: Cannot connect signals, one or more buttons references are null!");return;}console.log(" Connecting button signals...");this.researchButton.pressed.connect(Callable.create(this,()=>this._on_sidebar_button_pressed(this.researchPanel)));this.workshopButton.pressed.connect(Callable.create(this,()=>this._on_sidebar_button_pressed(this.workshopPanel)));this.storyButton.pressed.connect(Callable.create(this,()=>this._on_sidebar_button_pressed(this.storyPanel)));this.settingsButton.pressed.connect(Callable.create(this,()=>this._on_sidebar_button_pressed(this.settingsPanel)));console.log(" Button signals connected.");}_process(delta: number){}private_on_sidebar_button_pressed(panel_to_show: Control|null): void{console.log(`Sidebar button pressed. Requesting panel: ${panel_to_show?.get_name()??"NULL"}`);if(panel_to_show){this.show_panel(panel_to_show);}else{printerr("Error: _on_sidebar_button_pressed called with a null panel!");}}privateshow_panel(panel_to_show: Control|null): void{if(!panel_to_show){console.log("show_panel called with null, hiding all panels.");}else{console.log(`Attempting to show panel: ${panel_to_show.get_name()}`);}for(constpanelofthis.contentPanels){if(panelinstanceofControl){panel.visible=false;}}if(panel_to_showinstanceofControl){panel_to_show.visible=true;console.log(`Successfully shown panel: ${panel_to_show.get_name()}`);}}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to create a UI by adding controls directly. Things are showing up on my GUI but none of the buttons are working. I am not sure what I am doing wrong. Any help would be appreciated!
Beta Was this translation helpful? Give feedback.
All reactions