Why is my Custom Player not being found?

I am trying to code a GUI with buttons that Set the class for the player. However my Custom Player object is not found when I try it out although it works when I press a button to assign the class:

ClassSetter(Agent:agent):void=
    if(IsAgent:agent = agent[Agent]):
        if(CP:CustomPlayer = PlayersMap[Agent]):
            CP.SetClass(6)
            Print("Class Set")
        else:
            Print("Player is not a custom object")

(This was in the Game Manager Script)
In the Script of the UI I have the following code to send the agent to the function:

Interface := class():

   var MainCanvas : canvas = canvas{}
   var AbilityButton1 : button_loud = button_loud{}
   var AbilityButton2 : button_loud = button_loud{}
   var AbilityButton3 : button_loud = button_loud{}


   var GM: game_manager = game_manager{}

   ToMessage<localizes>(String : string) : message = "{String}"


   AddUI(Agent:agent):void=
       if(Player := player[Agent],PlayerUI := GetPlayerUI[Player]):
           set MainCanvas = CreateUI()
           PlayerUI.AddWidget(MainCanvas, player_ui_slot{InputMode := ui_input_mode.All})

        

   AbilityHandler(Message : widget_message):void=
       Player := Message.Player
       if(PlayerUI := GetPlayerUI[Player]):
           PlayerUI.RemoveWidget(MainCanvas)        
       if(Agent:agent = agent[Player]):
           GM.ClassSetter(Agent)

I am really confused because I am using the same code and don’t understand why it wouldn’t work, also sorry for bad formatting, I am writing on my phone at 1:30 AM :grimacing:
I would really appreciate it if some1 could help me, thanks in advance :smiley:

1 Like

Can you fix your post syntax please

1 Like

Fixed it, thank you :smiley:, I still need help tho please

How do you populate your PlayersMap ?

1 Like

Like this:

OnPlayerSpawned(MaybeAgent:?agent):void=
    Print("PlayerSpawnedIn")
    if(Agent:= MaybeAgent?):
        if(PlayerExists := PlayersMap[Agent]):
        #Found Player
        else: 
            CP:CustomPlayer = CustomPlayer{MyAgentObj := Agent}
            option:
                set PlayersMap[Agent] = CP

Is this what you need? My Custom Player class would be this if you need it:

CustomPlayer := class():
    MyAgentObj:agent

    var Class<private>:int = 1

    SetClass(C:int):void=
        set Class = C

    GetClass():int=
        return Class

Thank you for your help :smiley:

The OnPlayerSpawned method is probably called after the UI is being added

1 Like

I thought of this so I added a time of 3 seconds before my interface is created:

ChooseAbilities()<suspends>:void=
    Sleep(3.0)
    Players:=GetPlayspace().GetPlayers()
    for(Player:Players):
        if(IsAgent:agent = agent[Player]):
            var NewGUI : Interface = Interface{}
            NewGUI.AddUI(IsAgent)

However it doesn´t work that way, how could I fix it instead?

Found a Workaround by passing the Custom Player to the Interface Class :slight_smile:

1 Like