trying to do a boolean with with a string

ItsColdItsCold Posts: 18Member

func on_item_selected(id):
if on_item_selected(str(dropdown.get_item_text(id)))=="subjugate":

is this is correct?
im trying to declare some choice as 'true' in several dropdown/option node if they are selected

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin
    Answer ✓

    You are using a ItemList node and you are wanting to check if the item selected is equal to a certain string, correct?

    If so, then something like the following should work (in theory. I have not tested it):

    extends ItemList
    
    func _ready():
        # Add some items
        add_item("Choice one");
        add_item("Choice two");
        add_item("Choice three");
        # Connect the item selected signal
        connect("item_selected", this, "on_item_selected");
    
    func on_item_selected(idx):
        var item_name = get_item_text(idx);
    
        if (item_name == "Choice one"):
            print ("Choice one was chosen!");
        elif (item_name == "Choice two"):
            print ("Second choice picked!");
        else
            # just for kicks... Add the original options in reverse order
            clear();
            add_item("Choice three");
            add_item("Choice two");
            add_item("Choice one");
            print ("Third choice chosen!")
    

    If you want to track whether a option is enabled or disabled, I would suggest making either a list or a dictionary, and then when a item is selected populate/change the list to reflect whether a item was selected or not.

    Hopefully this helps :smile:

  • ItsColdItsCold Posts: 18Member

    sorry for the very late reply, boss sent me somewhere with little to no internet connection.

    thanks alot sir, you are a savior.

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin

    @ItsCold said:
    sorry for the very late reply, boss sent me somewhere with little to no internet connection.

    thanks alot sir, you are a savior.

    Ah, no worries! I’m glad it helped :smile:

Sign In or Register to comment.