How to use set_tab_title() correctly?

MisterBNGMisterBNG Posts: 2Member
in GUI

I have a TabContainer node with two Tabs nodes as children. I want to be able to set their titles at runtime as one of them has to change its title when needed. However all I get is the error: Index p_tab = 0 is out of bounds (tabs.size() = 0) when using set_tab_title(). Set_tab_title() is being called on the child Tabs nodes in the TabContainer's script which extends TabContainer, if that helps.

I'm guessing perhaps I have to set up their indeces somewhere but I can't find anything on how to do that. Help would be appreciated.


Tags :

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 2,831
    edited July 3 Accepted Answer

    Welcome to the forums @MisterBNG!

    I have not tried it myself, but I think the following code should work, or at very least the print statement may help indicate what is going on:

    extends TabContainer
    func _ready():
        # Add tabs, etc.
        var tab_count = get_tab_count()
        print ("There are ", tab_count, " tabs")
        # Get the first tab's current label
        print ("First tab label: ", get_tab_title(0))
        # Set the first tab's label
        set_tab_title(0, "New Tab Title Here")
        # make sure it's changed
        print ("First tab label: ", get_tab_title(0))
    

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 2,831Admin
    edited July 3 Accepted Answer

    Welcome to the forums @MisterBNG!

    I have not tried it myself, but I think the following code should work, or at very least the print statement may help indicate what is going on:

    extends TabContainer
    func _ready():
        # Add tabs, etc.
        var tab_count = get_tab_count()
        print ("There are ", tab_count, " tabs")
        # Get the first tab's current label
        print ("First tab label: ", get_tab_title(0))
        # Set the first tab's label
        set_tab_title(0, "New Tab Title Here")
        # make sure it's changed
        print ("First tab label: ", get_tab_title(0))
    
  • MisterBNGMisterBNG Posts: 2Member

    Fantastic, thank you! My mistake was calling it wrong, your example showed me that. I was using:

    [tabs_node].set_tab_title(0, "New Title")

    Which of course meant it was trying to do that on the Tabs node rather than the TabsContainer where it was written. Thanks a lot! (apologies if my explanations don't quite make sense, I'm pretty new to programming)

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file