Forum Discussion

flarb's avatar
flarb
MHCP Partner
10 months ago
Solved

Is there a way to auto-grab items?

I want to make a player automatically grab an object when they bump into it instead of having to press the hold/grab button. Is there a way to do this? I don't see any method that would allow me to place an object in an avatar's hand.

  • I made a few assumptions. The object being a sword and that it is being force held on player enter. The main piece of code you are looking for is force hold line but this should work.

    // these are the required imports from horizon/core
    import { Component, PropTypes, CodeBlockEvents, GrabbableEntity, Handedness } from 'horizon/core';
    
    class ForceHoldObject extends Component<typeof ForceHoldObject > {
         static propsDefinition = {
              sword: { type: PropTypes.Entity },
         };
    
         // first we must connect to the player enter world event. This will give us a reference to the player when they enter the world.
         preStart() {
              this.connectCodeBlockEvent(this.entity, CodeBlockEvents.OnPlayerEnterWorld, data => this.OnPlayerEnterWorld(data));
         }
    
         // once the player enters the world we can force hold the sword.
         // however, we must first ensure that the sword is set.
         // we do this by checking with an if statement.
         OnPlayerEnterWorld(player: Player) {
              if (this.props.sword) { // if this returns true we can force hold the sword
                   this.props.sword.as(GrabbableEntity).forceHold(player, Handedness.Right, false)
              }
              else { // else we may want to know that it is not set so print a warning to the console.
                   console.warn("sword is missing or null!");
              }
         }
    }
    Component.register(ForceHoldObject);

     

4 Replies

  • Codeblocks has a "Force Hold" block which you can activate on collision.  TS likely has an equivalent

  • I made a few assumptions. The object being a sword and that it is being force held on player enter. The main piece of code you are looking for is force hold line but this should work.

    // these are the required imports from horizon/core
    import { Component, PropTypes, CodeBlockEvents, GrabbableEntity, Handedness } from 'horizon/core';
    
    class ForceHoldObject extends Component<typeof ForceHoldObject > {
         static propsDefinition = {
              sword: { type: PropTypes.Entity },
         };
    
         // first we must connect to the player enter world event. This will give us a reference to the player when they enter the world.
         preStart() {
              this.connectCodeBlockEvent(this.entity, CodeBlockEvents.OnPlayerEnterWorld, data => this.OnPlayerEnterWorld(data));
         }
    
         // once the player enters the world we can force hold the sword.
         // however, we must first ensure that the sword is set.
         // we do this by checking with an if statement.
         OnPlayerEnterWorld(player: Player) {
              if (this.props.sword) { // if this returns true we can force hold the sword
                   this.props.sword.as(GrabbableEntity).forceHold(player, Handedness.Right, false)
              }
              else { // else we may want to know that it is not set so print a warning to the console.
                   console.warn("sword is missing or null!");
              }
         }
    }
    Component.register(ForceHoldObject);

     

  • flarb's avatar
    flarb
    MHCP Partner

    Hey thanks! Sort of related--is there a way to know if the player is holding an object? I don't want the forcehold to happen if the player is already holding something.

    • gausroth's avatar
      gausroth
      MHCP Mentor

      You will have to add a boolean to the held items script and set to true if held and false if not. If it is a local script you will need to create a manager script for this.