Forum Discussion

Tiitouaan's avatar
Tiitouaan
Member
18 days ago
Solved

Why is the start() method not triggered ?

Hello,

My PlayerManager is attaching a HUD to the player's head when he joins the world :

private handleOnPlayerEnterWorld(player: hz.Player): void {
    // Handle double join messages
    if (!this.gamePlayers.get(player)) {
      const pData = this.gamePlayers.addNewPlayer(new PlayerData(player, this.props.initialHealthForPlayer, this.props.coolDownInMs));
      console.log(`PlayerManager: New player joined world: ${player.name} with id ${player.id}`);
      // Spawn du HUD personnel
      if (this.props.hudPrefab) {
        console.log("PlayerManager: Spawning HUD for player " + player.name);
        this.world.spawnAsset(this.props.hudPrefab, hz.Vec3.zero, hz.Quaternion.zero).then(entities => {
        const hud = entities[0];
        // The HUD is an AttachableEntity, so we can attach it to the player's head
        hud.as(hz.AttachableEntity).attachToPlayer(player, hz.AttachablePlayerAnchor.Head); 
      });
  }
    }
    else {
      console.warn("PlayerManager: Player already joined world");
    }
  }

Here is the start() method from the HUD script

start() {
    console.log("BigBox_UI_ToastHud: Starting Toast HUD");
    // Check if this is running on the Server or Client
    const localPlayer = this.world.getLocalPlayer();

    // Only attach listeners if this is running on the local player
    if (localPlayer) {
      this.connectNetworkBroadcastEvent(BigBox_ToastEvents.textToast, this.onLocalTextToast.bind(this));
    }
  }

When a player joins the world, the HUD is correctely created in the world but the start() method isn't triggered (I presume since the message isn't displayed in the console and it doesn't work at all). I precise that the HUD Script is setted in local mode.

Have you any idea why ? Thank a lot for your help !

  • Yeah it's good ! The FBS's window says that there may be problem with assets for no visible reason and we just have to recreate it, and that's what I did and it's working now 👍

    Thank you for your help I wouldn't have seen this by myself

7 Replies

  • What is the HUD? Is it a Noesis UI, Custom UI or something else? Are you getting any errors or warnings in the console when it spawns in?

  • It is Custom UI and no, I did not get any errors or warnings when it spawns in

  • I think it's because your world is not FBS. Go to Scripts > Script Settings > Script editing.

    This is the code I used to test it and start ran for me.

    import { Component, CodeBlockEvents, Player, PropTypes, Vec3, Quaternion, AttachableEntity, AttachablePlayerAnchor } from 'horizon/core';
    
    class HUDSpawnTest extends Component<typeof HUDSpawnTest> {
        static propsDefinition = {
            hudPrefab: { type: PropTypes.Asset },
    };
    
        start() {
            this.connectCodeBlockEvent(this.entity, CodeBlockEvents.OnPlayerEnterWorld, (player: Player) => this.handleOnPlayerEnterWorld(player));
    
      }
    
        private handleOnPlayerEnterWorld(player: Player): void {
            if (this.props.hudPrefab) {
                console.log("PlayerManager: Spawning HUD for player " + player.name);
                this.world.spawnAsset(this.props.hudPrefab, Vec3.zero, Quaternion.zero).then(entities => {
                    const hud = entities[0];
                    // The HUD is an AttachableEntity, so we can attach it to the player's head
                    hud.as(AttachableEntity).attachToPlayer(player, AttachablePlayerAnchor.Head);
                });
            }
        }
    }
    Component.register(HUDSpawnTest);
    import { Text, UIComponent, UINode, View } from 'horizon/ui';
    
    class HUDTest extends UIComponent<typeof HUDTest> {
      protected panelHeight: number = 300;
      protected panelWidth: number = 500;
    
        static propsDefinition = {};
    
        start() {
            console.log("BigBox_UI_ToastHud: Starting Toast HUD");
            // Check if this is running on the Server or Client
            const localPlayer = this.world.getLocalPlayer();
    
            console.log("BigBox_UI_ToastHud: Local Player is ", localPlayer.name.get());
        }
    
      initializeUI(): UINode {
        // Return a UINode to specify the contents of your UI.
        // For more details and examples go to:
        // https://developers.meta.com/horizon-worlds/learn/documentation/typescript/api-references-and-examples/custom-ui
    
        return View({
          children: [
            Text({
              text: "New UI Panel",
              style: {
                fontSize: 48,
                textAlign: 'center',
                textAlignVertical: 'center',
                height: this.panelHeight,
                width: this.panelWidth,
              }
            })
          ],
          style: {
            backgroundColor: 'black',
            height: this.panelHeight,
            width: this.panelWidth,
          }
        });
      }
    }
    UIComponent.register(HUDTest);
    

     

  • Wow thank you for your involvement it is really nice of you ! I try your code but can you tell me what is FBS world please?

  • Well my world is FBS, I hoped it was this but it isn’t the problem here :/. My HUD script come from a public asset of a toast notification system, can it be the problem?

  • Yeah it's good ! The FBS's window says that there may be problem with assets for no visible reason and we just have to recreate it, and that's what I did and it's working now 👍

    Thank you for your help I wouldn't have seen this by myself