Running a command/verb via a script

Hi all,
This is either a very simple or very difficult request, I'm not sure which. Essentially I want to be able to do the following:

If the player types 'use hat' it will present a question: Do you mean you want to wear the hat? Yes/No and if you click yes it calls the verb: wear hat. Making the question is straight forward but I don't know how to call a verb. I can simply set hat.worn=True but it doesn't run the text 'You put on the hat'. I can add this manually but it seems like clumsy programming. I've tried the do, eval and invoke commands but not getting anywhere...

Thanks in advance!

Quest's built-in wearables system provides a function to make it easier to do this:

_DoWear (hat)

(you don't need to UpdateArmour unless you're using the armour feature of wearables)
Several of the built-in commands have functions like this to make it easier to do them from code, like TryOpenClose (door, true) or DoDrop (object, false). But not all of them do. And I figure that while you asked about "Running a command/verb via a script", I should also give you an answer that you can use for any command:

There's a few ways to do it; useful in different situations.

Method 1 will nearly always work, but if there's more than one hat around it will give the player a menu asking which one they meant. Just as if they'd typed that command. Methods 2-4 don't have this problem, but which one you use will be different for different commands. (For example, in my current game, method 2 will work for "eat", method 3 will work for "wear", and method 4 will work for "stab". It depends how the command was originally coded)

  1. To send a command as if the player typed it, you can do:
HandleSingleCommand ("wear hat")
  1. If you know the name of the command, you can put the hat into a dictionary and pass that to the command:
params = NewDictionary()
dictionary add (params, "object", hat)
do (lookat, "script", params)

(this runs the 'script' attribute of the command 'lookat', putting the hat into that script's 'object' parameter. I'm using "lookat" in this example, because this method doesn't work for the default implementation of "wear")

  1. Note that for some commands you might have to make a list of objects first, even if the list only has 1 item on it, so you'd do:
objects_to_wear = NewObjectList()
list add (objects_to_wear, hat)
dictionary add (params, "object", objects_to_wear)
do (wear, "script", params)

(This is the method that will work for "wear" or "take", I believe. It's for commands that can understand multiple objects)

  1. If the command you want to execute is a verb, you can run it directly:
do (hat, "wear")

(This will work if you see the verb on the "Verbs" tab when looking at the object)

Thank you so much for such a detailed reply. I wish I'd known some of this when I started programming a few months ago, my code would be a lot neater! Method 1 works perfectly, method 4 is the one I tried before but I just got the error message:

Error running script: Object reference not set to an instance of an object.

Not sure why. Anyway, thanks again! I'll try the other methods down the line if required.

Maybe worth mentioning that method 2 is the one you want if you're doing a command that has more than one parameter. For example, if you want to programatically call the command put #object1# in #object2#, you would use:

params = NewDictionary()
dictionary add (params, "object1", bunny)
dictionary add (params, "object2", box)
do (put, "script", params)

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