Delete <object>

So I want to be able to delete an object in the game as a command. Ideally from any room and not just the room I'm in, though I understand if it won't work like that. So for example I could write Delete Soldier21 and that soldier would just disappear from the game even if I'm in a whole different room. Is that possible? If so, how?


If you want the player to enter the name of the object, msake a command with the pattern delete #text# and the script destroy (text).

If you want the player to enter the alias like they do for other commands, the pattern would be delete #object#, the scope is world, and the script would be destroy (object.name).


It's either Destroy #object# and Destroy (object), or Destroy #text# and Destroy (object).


No, Destroy #text# would overlap existing code.
It's Destroy #object# and Destroy (object).

Pretty sure you're wrong there.

delete #text# as a command pattern allows the player to enter arbitrary text. estroy (text) will destroy the named object, or give an error if it doesn't exist. This works because the parameter to destroy is a string containing an object name.

If you're using destroy #object# the object parameter will be matched against alias, using the parser's normal disambiguation process; which is why you need to use object.name when calling destroy. In this case, you set the scope to world so that it considers all objects regardless of location.


Oh, so that's what that does. Nevermind. That is a bit strange, isn't it?


Like this?


I've replaced < with ( so it shows up.

(command>
(pattern>Delete #text#
(scope type="string">
(script>
destroy (text)
(/script>
(/command>


<command name="delete">
  <pattern>Delete #text#</pattern>
  <scope type="string">world</scope>
  <script>
    destroy (text)
  </script>
</command>

That looks right, I think. Does it work?

I've replaced < with ( so it shows up.

You can put 3 backticks (```) on a line before and after your code so the forum doesn't mangle it. The backtick is the key in the top left on most keyboards, or you can copypaste them from the example to the right of the reply box.


Yes, but only if I'm in the room with it. I'd like to be able to do it from anywhere in the place.


Yes, but only if I'm in the room with it. I'd like to be able to do it from anywhere in the place.

#text# doesn't know that the text you're entering is an object name, so it doesn't check where it is. You should be able to do this with any object you know the name of.


It doesn't seem to be deleting them in the other rooms though which is odd.


EDIT

That code works for me. (I just tested it.)

Note that it is case-sensitive. So, "Frob" and "frob" are two different things. (If you got that wrong, it would throw an error.)

The only other thing I can think of is: are you using clones. The exact name needs to be used here. (Again, it would throw an error if you got this wrong.)


It doesn't seem to be deleting them in the other rooms though which is odd.

Are you sure you're entering the name correctly? If it doesn't give an error, I think that means you might be giving the name of the wrong object.

Is the object you're trying to delete a clone?

I can't give much more help without seeing an example of what you're using it for.

@KV

Just guessing, didn't test anything, but should that be this?:

I did mention the difference between those two. If you're using it for debugging, I think #text# might be better because it allows you to specify the name of a specific object, and can also be used for exits and commands. If it's used in-game, the version that deletes by alias might be better.


Ah, I see KV edited his post :)

The only other thing I can think of is: are you using clones. The exact name needs to be used here. (Again, it would throw an error if you got this wrong.)

That's what I thought of. Because you might end up deleting the original, and there wouldn't be an error message until the game tried to clone it again.


I updated it a little, just to catch the error:

CODE

  <command name="delete_cmd">
    <pattern>delete #text#</pattern>
    <scope>world</scope>
    <script>
      if (not GetObject(text) = null){
        destroy (text)
        if (GetObject(text) = null) {
          msg ("Done.")
        }
        else ("Something went wrong.")
      }
      else {
        msg("The object (" + text + ") doesn't exist!")
      }
    </script>
  </command>

TRANSCRIPT

You are in a room.
You can see a Fred.
You can go north.

> north

You are in a room2.
You can see a Barney.
You can go south.

> south

You are in a room.
You can see a Fred.
You can go north.

> delete fred
The object (fred) doesn't exist!

> delete Fred
Done.

> l
You are in a room.
You can go north.

> n

You are in a room2.
You can see a Barney.
You can go south.

> s

You are in a room.
You can go north.

> delete Barney
Done.

> n

You are in a room2.
You can go south.

clones

delete #object# with a world scope would most probably present the disambiguation menu, too.

Which one?

  1. the frob
  2. the frob
  3. the frob

I did mention the difference between those two.

Oh. Ha! Now I see that.

I gotta stop using my phone to surf the forum. It seems that I always overlook something. :o)


PLEASE DELETE THIS POST; THE PREVIOUS POST WAS DUPLICATED; THANKS.


A more sophisticated version, that will behave sanely for clones:

<command name="delete">
  <pattern>delete #text#</pattern>
  <script>
    object = GetObject (text)
    if (object = null) {
      msg ("There is no such object")
      // You could add code here to find similar names
    }
    else {
      clones = FilterByAttribute (AllObjects(), "prototype", object)
      if (not object.prototype = object) {
        list add (clones, object)
      }
      if (ListCount (clones) = 1) {
        msg ("Deleting " + GetDisplayAlias(object) + " from " + GetDisplayAlias (object.parent) + ".")
        destroy (text)
      }
      else {
        options = NewStringDictionary()
        foreach my (obj, clones) {
          label = GetDiaplayAlias (obj) + "(" + obj.name + "; "
          if (obj = object) {
            label = label + "original"
          }
          else {
            label = label + "clone"
          }
          if (obj.parent = game.pov) {
            label = label + ", in inventory"
          }
          else if (HasObject (obj, "parent")) {
            label = label + ", in " + GetDisplayAlias (obj.parent)
          }
          dictionary add (options, obj.name, label + ")")
        }
        ShowMenu ("Did you mean…", options, true) {
          msg ("Deleting " + result + ".")
          destroy (result)
        }
      }
    }
  </script>
</command>

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums