Item Command Arguments

Commands may specify some arguments of the &item type. This is an item reference, and allows item IDs to be passed to commands directly on the command line. The value of the argument is not an item itself but actually a ValueReference Interface which may contain a reference to an item.

Using Commands

If you want to use a command that has an item argument, you will need to set the value of the argument from an item you hold.

Setting an item argument

Given the Attributes Interface from the command (or any attributes interface with a &item value), you first get the attribute as a value reference, then set the value to your item.

1
2
3
4
        CLxUser_[[Item Interface|Item]]          item;
        CLxUser_[[Attributes Interface|Attributes]]      attr;
        CLxUser_[[ValueReference Interface|ValueReference]]      ref;
        unsigned                 idxArg;
1
2
        attr.ObjectRW (idxArg, ref);
        ref.SetObject (item);

Executing a command

This complete example shows executing a command with an item argument. First we create the item.draw command, get the attributes interface, then set the mode and item arguments, and finally execute it.

1
2
3
4
5
        CLxUser_[[Item Interface|Item]]          item;
        CLxUser_[[CommandService Interface|CommandService]]      svcCmd;
        CLxUser_[[Command Interface|Command]]            cmd;
        CLxUser_[[Attributes Interface|Attributes]]      attr;
        CLxUser_[[ValueReference Interface|ValueReference]]      ref;
1
2
        if (svcCmd.NewCommand (cmd, "item.draw")) {
                attr.set (cmd);
1
                attr.Set (attr.FindIndex ("mode"), 1);   // 'add' mode
1
2
                attr.ObjectRW (attr.FindIndex ("item"), ref);
                ref.SetObject (item);
1
2
                cmd.Execute (LXfCMD_EXEC_DEFAULT);
        }

Authoring Commands

You can also create a command utilizing a &item argument.

Declaring

If you’re using the CLxBasicCommmand helper class, then defining your argument is done in the constructor for the command.

1
2
3
4
        CMyCommand::CMyCommand ()
        {
                dyna_Add ("item", "&item");
        }

Reading

Reading the item passed to a command, the index of the command’s &item argument is specified by ‘’idxArg’’.

1
2
3
4
        CLxUser_[[Item Interface|Item]]                  item;
        CLxUser_[[Attributes Interface|Attributes]]              attr;
        CLxUser_ValueReference           ref;
        unsigned                 idxArg;
1
2
3
        if (dyna_Object (idxArg, ref) && ref.Get (item)) {
                // do stuff with item
        }