Skip to content
BagModule

Extension / BagModule

BagModule Class

Bag instance

The bag system is like a special bag you use when playing games, which can help you manage and store various items and asset. The MW editor has packaged a backpack system that can be used directly.

Your character collect many valuable items in the game, such as weapons, equipment, medicines, materials, etc. These items need a place to store, and the bag system is a virtual bag that can hold these items.

It is a special tool in the game to help you organize, store and manage various items and asset you collect in the game, making your game experience more convenient and orderly.

Example usage: Create a script named BagExample, place it in the object bar, open the script, modify the original content to the following, save and run the game

ts
@Component
export default class BagExample extends Script {
   protected onStart(): void {
      BagModule. registrant Item (1, "37692", "Golden Shovel", ItemQuality.Legend,10, {a:1,b:2});
      BagModule. registrant Item (2, "37690", "little speaker", ItemQuality.Legend,10, {a:1,b:2});
      BagModule. registrant Item (3, "37697", "Gold Coins", ItemQuality.Legend,10, {a:1,b:2});
      BagModule. registrant Item (4, "37695", "Pumpkin", ItemQuality.Common,20, {a:1,b:2});
      if(SystemUtil.isClient()){
          BagModule.addItemClickListener(this.onItemClick,this);
          BagModule.addItem(1,1);
          BagModule.addItem(2,5);
          BagModule.addItem(3,10);
          BagModule.addItem(4,30);
          BagModule.open();
      }
   }
   private onItemClick(cfg:ItemConfig){
      Console. log ("click", cfg);
   }
}
@Component
export default class BagExample extends Script {
   protected onStart(): void {
      BagModule. registrant Item (1, "37692", "Golden Shovel", ItemQuality.Legend,10, {a:1,b:2});
      BagModule. registrant Item (2, "37690", "little speaker", ItemQuality.Legend,10, {a:1,b:2});
      BagModule. registrant Item (3, "37697", "Gold Coins", ItemQuality.Legend,10, {a:1,b:2});
      BagModule. registrant Item (4, "37695", "Pumpkin", ItemQuality.Common,20, {a:1,b:2});
      if(SystemUtil.isClient()){
          BagModule.addItemClickListener(this.onItemClick,this);
          BagModule.addItem(1,1);
          BagModule.addItem(2,5);
          BagModule.addItem(3,10);
          BagModule.addItem(4,30);
          BagModule.open();
      }
   }
   private onItemClick(cfg:ItemConfig){
      Console. log ("click", cfg);
   }
}

Table of contents

Accessors

capacity(): number other
Backpack capacity
dragEnabled(): boolean other
Can I drag and drop
labelVisible(): boolean other
Is the item name visible
searchEnabled(): boolean other
Display search box or not

Methods

addItem(itemId: number, count?: number, player?: Player): Promise<number> other
add item
addItemClickListener(clickCallback: (cfg: ItemConfig) => void, thisObject: any): typeof BagModule other
Add item click callback
close(player?: Player): typeof BagModule other
Close the bag interface
getItemCount(itemId: number, player?: Player): number other
Get the number of specified props of bag
open(player?: Player): typeof BagModule other
Open the backpack interface
registerItem(itemId: number, icon: string, name: string, quality?: ItemQuality, stackCount?: number, customData?: any): typeof BagModule other
Backpack registration item information, items need to be registered on the client side
removeItem(itemId: number, count?: number, player?: Player): typeof BagModule other
Delete Item
removeItemClickListener(clickCallback: (cfg: ItemConfig) => void, thisObject: any): typeof BagModule other
Remove props and click callback
skin(bagSkin: (...args: any[]) => IBagSkin, itemSkin: (...args: any[]) => IBagItemSkin, itemBgSkin: (...args: any[]) => UIScript, deleteDialogSkin: (...args: any[]) => IItemDeleteSkin): typeof BagModule other
Set bag skin
sort(): typeof BagModule other
Organize bag

Accessors

capacity

Static get capacity(): number other

Static set capacity(capacity): void other

Backpack capacity

Returns

numberBackpack capacity

Backpack capacity

Parameters

capacity numberUsage: Backpack capacity
range: [1128]

dragEnabled

Static get dragEnabled(): boolean other

Static set dragEnabled(isDragable): void other

Can I drag and drop

Returns

booleanCan drag and drop return

Can I drag and drop

Parameters

isDragable booleanUsage: Can I drag and drop

labelVisible

Static get labelVisible(): boolean other

Static set labelVisible(isLabelVisible): void other

Is the item name visible

Returns

booleanReturn whether the item name is visible

Is the item name visible

Parameters

isLabelVisibleboolean

searchEnabled

Static get searchEnabled(): boolean other

Static set searchEnabled(isSearchEnabled): void other

Display search box or not

Returns

booleanReturn whether to display the search box

Display search box or not

Parameters

isSearchEnabled booleanUsage: Display search box or not

Methods

addItem

Static addItem(itemId, count?, player?): Promise<number> other

add item

Parameters

itemId numberUsage: Prop ID
range: No restrictions type: Integer
count? numberUsage: Number of props default: 1
Range: No limit type: Integer
player? PlayerUsage: Props Owner default: Must be passed to the server

Returns

Promise<number>Return the number of items added, and if the addition fails, return 0

Usage example: Create a BagExample script and drag it into the object manager. Add the following code and run the project

ts
@Component
export default class BagExample extends Script {
   protected onStart(): void {
      BagModule. registrant Item (1, "37692", "Golden Shovel", ItemQuality.Legend,10, {a:1,b:2});
      if (SystemUtil.isServer()) return;
      BagModule.addItem(1).then((count:number)=>{
         Console.log ("Number of items added", count);
      });
      BagModule.open();
   }
@Component
export default class BagExample extends Script {
   protected onStart(): void {
      BagModule. registrant Item (1, "37692", "Golden Shovel", ItemQuality.Legend,10, {a:1,b:2});
      if (SystemUtil.isServer()) return;
      BagModule.addItem(1).then((count:number)=>{
         Console.log ("Number of items added", count);
      });
      BagModule.open();
   }

addItemClickListener

Static addItemClickListener(clickCallback, thisObject): typeof BagModule other

Add item click callback

Parameters

clickCallback (cfg: ItemConfig) => voidUsage: Click on callback
thisObject anyUsage: call context

Returns

typeof BagModuleReturn to bag instance

close

Static close(player?): typeof BagModule other

Close the bag interface

Parameters

player? PlayerUsage: player needs to be passed in when the server call default: player needs to be passed in when the server calls

Returns

typeof BagModuleReturn to bag instance

getItemCount

Static getItemCount(itemId, player?): number other

Get the number of specified props of bag

Parameters

itemId numberUsage: Prop ID
range: Determine type based on prop ID: integer
player? PlayerUsage: server input, open the specified player's backpack default: server must input

Returns

numberReturn to bag instance

open

Static open(player?): typeof BagModule other

Open the backpack interface

Parameters

player? PlayerUsage: owner of the bag default: filled in by the server, not by the client

Returns

typeof BagModuleReturn to bag instance

registerItem

Static registerItem(itemId, icon, name, quality?, stackCount?, customData?): typeof BagModule other

Backpack registration item information, items need to be registered on the client side

Parameters

itemId numberUsage: Prop ID
range: Determine type based on prop ID: integer
icon stringUsage: props image
range: unlimited
name stringUsage: Name
Range: No restrictions
quality? ItemQualityUsage: prop quality - default ordinary default: ItemQuality Common
stackCount? numberUsage: stacking quantity default: 1
range: no limit type: integer
customData? anyUsage: additional data default: null

Returns

typeof BagModuleReturn to bag instance

Usage example: create a BagExample script and drag it into the object manager. Add the following code So you have registered 4 items, and then add the corresponding ID in the backpack through BagModule. addItem to see these 4 items

ts
@Component
export default class BagExample extends Script {

    protected onStart(): void {
        BagModule. registrant Item (1, "37692", "Golden Shovel", ItemQuality.Legend,10, {a:1,b:2});
        BagModule. registrant Item (2, "37690", "little speaker", ItemQuality.Legend,10, {a:1,b:2});
        BagModule. registrant Item (3, "37697", "Gold Coins", ItemQuality.Legend,10, {a:1,b:2});
        BagModule. registrant Item (4, "37695", "Pumpkin", ItemQuality.Common,20, {a:1,b:2});
    }
}
@Component
export default class BagExample extends Script {

    protected onStart(): void {
        BagModule. registrant Item (1, "37692", "Golden Shovel", ItemQuality.Legend,10, {a:1,b:2});
        BagModule. registrant Item (2, "37690", "little speaker", ItemQuality.Legend,10, {a:1,b:2});
        BagModule. registrant Item (3, "37697", "Gold Coins", ItemQuality.Legend,10, {a:1,b:2});
        BagModule. registrant Item (4, "37695", "Pumpkin", ItemQuality.Common,20, {a:1,b:2});
    }
}

removeItem

Static removeItem(itemId, count?, player?): typeof BagModule other

Delete Item

Parameters

itemId numberUsage: Prop ID
range: Determine type based on prop ID: integer
count? numberUsage: Number of props default: 1
Range: No limit type: Integer
player? PlayerUsage: Props owner, server must pass in default: server must pass in

Returns

typeof BagModuleReturn to bag instance

removeItemClickListener

Static removeItemClickListener(clickCallback, thisObject): typeof BagModule other

Remove props and click callback

Parameters

clickCallback (cfg: ItemConfig) => voidUsage: Click on callback
thisObject anyUsage: call context

Returns

typeof BagModuleReturn to bag instance

skin

Static skin(bagSkin, itemSkin, itemBgSkin, deleteDialogSkin): typeof BagModule other

Set bag skin

Parameters

bagSkin (...args: any[]) => IBagSkinUsage: bag UI skin, null is the default skin
itemSkin (...args: any[]) => IBagItemSkinUsage: Backpack Item Skin, null is the default skin
itemBgSkin (...args: any[]) => UIScriptUsage: Backpack Item Background Skin, null is the default skin
deleteDialogSkin (...args: any[]) => IItemDeleteSkinUsage: bag item delete pop-up skin, null is the default skin

Returns

typeof BagModuleReturn to bag instance

Usage example:

  1. create a UI named BagUI in the project, and drag the following widget closeBtn: mw.StaleButton; content: mw.Canvas; filterBox: mw.InputBox; filterCanvas: mw.Canvas;
  2. Export the UI, copy the exported UI file BagUI_Generate to the JavaScript folder, rename it BagSkin, and modify extends to BagUI Create a script named BagExample, place it in the object bar, open the script, modify the original content as follows, save and run the game
  3. The same applies to other UI skins
ts
@Component
export default class BagExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isServer()) return;
        BagModule.skin(BagSkin, null, null, null);
        BagModule.open();
    }
}
@Component
export default class BagExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isServer()) return;
        BagModule.skin(BagSkin, null, null, null);
        BagModule.open();
    }
}

sort

Static sort(): typeof BagModule other

Organize bag

Returns

typeof BagModuleReturn to bag instance