Trouble setting up tabs in TabContainer

Godot Version

4.2.1

Question

Hi everyone,

I’m having some trouble with setting up tabs using a TabContainer for my game. I’ll add the code below, but the error monitor is showing the “Unexpected identifier in class body” message, and I’m not quite sure why. I’ve followed a reasonably new guide online to try and set this up so it may just be that an update has gonked what I’m trying to do. Any assistance would be appreciated. All lines causing problems I’ll mark with a " * ".

extends TabContainer

var tab1 = Control.new()
tab1.name = “Tab 1” *
tab_container.add_child(tab1) *

var tab2 = Control.new()
tab2.name = “Tab 2” *
tab_container.add_child(tab2) *

var tab3 = Control.new()
tab3.name = “Tab 3” *
tab_container.add_child(tab3) *

var tab4 = Control.new()
tab4.name = “Tab 4” *
tab_container.add_child(tab4) *

var tab5 = Control.new()
tab5.name = “Tab 5” *
tab_container.add_child(tab5) *

var tab6 = Control.new()
tab6.name = “Tab 6” *
tab_container.add_child(tab6) *

tab_container.set_tab_title(0, “Politics”) *
tab_container.set_tab_title(1, “Provinces”) *
tab_container.set_tab_title(2, “Economy”) *
tab_container.set_tab_title(3, “Trade”) *
tab_container.set_tab_title(4, “Research”) *
tab_container.set_tab_title(5, “Military”) *

tab_container.tab_align = TabContainer.ALIGN_TOP *

func _ready():
TabContainer.connect(“tab_changed”, self, “_on_TabContainer_tab_changed”)

func _on_TabContainer_tab_changed(tab_index):
print("Switched to tab: ", tab_index)

A couple of things:

  1. You can’t execute code outside of functions. That’s why you get the errors.
  2. Use the TabBar class for TabContainer content, not the Control class
  3. The approach you use for connecting signals in the _ready() function is not how it’s done in Godot 4.

I notice that “tab_container” is a variable name that has not been declared anywhere.

Also, outside of functions, you can only declare variables (and their initial values). So you can’t do the tab_container.add_child() stuff. You should do all that in _ready() at the very least.

Try:

  1. replace all “tab_container” with “self”
  2. move all the calls to methods (the dot-somethings) into _ready
  3. change your connect line to: self.tab_changed.connect(_on_TabContainer_tab_changed)

I have not used a tab container yet, so there may still be bugs, but it will be closer to working :wink:
hth

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.