Handheld item change event||doc135524

September 20, 2022

Format

 HAND_ITEM_CHANGED
  • The class to which the function belongs:Entity

Description

This event is triggered when the entity object holding the item changes.


Precautions

  • When switching item, both new item and old item may be empty, so pay attention to the empty

Context

The context content of the event

name type Description
obj1 Entity Entity instance
item Item new item instance
oldItem Item old item instance


Code Samples

On the server side of the entity templatemount script, register this type of entity when switching handheld itemCallback, if the new item is 'sword', thenbroadcast tipsEntity holding a sword

Trigger.RegisterHandler(this:cfg(), "HAND_ITEM_CHANGED", function(context)
      local entity = context.obj1                                             
      local newItem = context.item
      local oldItem = context.oldItem
      if newItem:full_name()  == "myplugin/sword" then                        --Determine if the new item is a sword
           World.CurWorld.SystemNotice(1, entity.name..' Take the sword', 40) --Broadcast
      end     
end)