How to use SetText() to make a UI button change text when clicked on

So I have this code to set up my UI buttons (there is a bit more code before this to set up the UI):

    @editable Button2_Text : string = ""
    @editable Button2_Position : vector2 = vector2{}
    @editable Button2_Size : vector2 = vector2{}

    EnableMsg<localizes>():message="ENABLE"
    DisableMsg<localizes>():message="DISABLE"

    MakeCanvas(Agent:agent):canvas=

        Button2 : button_loud = button_loud{}
        Button2.SetText(StringToMsg(Button2_Text))
        Button2.OnClick().Subscribe(HandleButton2)

        var MyCanvas : canvas = canvas:
            Slots:= array:
                canvas_slot:
                    Widget:=Button2
                    ZOrder:=4
                    SizeToContent:=true
                    Offsets:=margin{Top:=Button2_Position.Y, Left:=Button2_Position.X, Bottom:=Button2_Size.Y, Right:=Button2_Size.X}

        return MyCanvas

and so once Button2 is clicked (HandleButton2) I want its text to change to “DISABLE”, so assign DisableMsg() to it. Then if it’s clicked again assign the “ENABLE” text to it. When I try to refer to Button2 in HandleButton2 it throws an unknown identifier error, so how could this be done?

What do you do inside HandleButton2() here ?

That was my question because Im working on this function

Ah okay lol, well you need to store the button somewhere (which is the player’s button btw, it cannot be shared among players), then call MyPlayerButton.SetText() with a different message based on if it’s currently disabled or enabled for the player

alright sry for the delay it took some time to load session and test. ignore the deleted reply cause I think I got it to work. ouside of MakeCanvas() i put:

var MyButton : button_loud = button_loud{}

then inside MakeCanvas() I put:

set MyButton = Button2

inside HandleButton2() :

MyButton.SetText(DisableMsg())

Well idk what’s this button for, but it will not work as is, because you only have one button for the whole server, instead of one per player, unless I’m not understanding right, you need to use a map

Oh well I’m showing this HUD to just one player. Only this player can access it not the others

1 Like

Alright, all good then :+1:

1 Like