Default Behavior vs. Run Script for opening a container

Using Desktop. With most settings, I can customize what happens. With Take, for example, I can choose Default Behavior or Run Script.

With a container, I can make a script run AFTER opening the object, but for what happens WHEN opening, it seems I can only have one message -- the default or a custom one, but not one that's dependent on the situation.

Here's an example: Let's say I have a magic box. Mostly, the box's behavior is entirely predictable -- the player can open and close it, and see the objects inside. Under normal circumstances, I want the game to use the default message.

But when the player is wearing the Hat of Wisdom, I want the box to open to reveal a new item, and I want the message to correspond. I can create a script that checks if wisdomhat.worn = true, and if so, add the new item and display extra text. But because the default message is automatic, it plays like this:

You open the box.
As you reach out to open the box, your Hat of Wisdom radiates warmth, and you know that something special is about to happen. You open the lid to find a beautiful gem!
It contains a gem.

To be clear, I want the first and third lines under some circumstances, and I want only the second line under other circumstances.

The last line is a similar problem to the first line -- I can "List children when object is looked at or opened" all the time or none of the time. But I can't have the option on for most circumstances, then suppress it for my special circumstance.

In Code View, I tried putting script between
<openmsg></openmsg>
but the game displays it as text, rather than RUNNING the code. I can put a space there:
<openmsg> </openmsg>
so that the game THINKS it's displaying a message, and that message is just empty. Although for the player it results in a line break that really shouldn't be there. Then in the "After opening the object" field, I can run my script, with a check for my special circumstance followed by an Else: "You open the box." That's an acceptable solution, except for the extra line break. Then, I suppose I can turn off "List children when opened" and add to my "After opening" script something involving GetDirectChildren, but I haven't worked out what that code would be, and it seems like there should be a much easier solution than this! Any ideas?


I'm looking at the code, and openscript should run instead of displaying the default message, while onopen runs after the object becomes open.

This is a slight weirdness in the editor. You need to change the object's type (at the top of the container tab) to "Openable/Closeable". This makes the openscript and closescript appear so you can edit them. Once you've entered a script there, you can change it back to the type of container it is. Those scripts won't appear in the editor, but if you've changed them the object will still have them.

In this case you'd want something like:

if (GetBoolean (hatofwisdom, "worn")) {
  msg ("As you reach out to open the box, your Hat of Wisdom radiates warmth, and you know that something special is about to happen. You open the lid to find a beautiful gem!")
  gem.parent = this
  this.isopen = true
}
else {
  OpenObject (this)
}

(OpenObject is the command that displays the default open message and lists the contents)

If you don't like playing around changing an object's type, you can add a script attribute named openscript on the Attributes tab. I don't know why it doesn't show up in the editor by default.


If there really wasn't a script to do this, or if it doesn't work for some reason, the workaround would be to use the text processor. You could set the open message to something like:

{either hatofwisdom.worn:As you reach out to open the box, your Hat of Wisdom radiates warmth, and you know that something special is about to happen.:You open the box.}

That way you only see one of the two messages. either selects two pieces of text based on a condition (the same kind of think you could put in an if statemment). You'd then have to use the onopen script to make the gem visible as you have. If you want to hide the list of objects, you could use code like this:

this.listobjects = false

to uncheck that box. And then on the "when the box is closed" script, you put

this.listobjects = true

to enable it again for next time the box is opened.


Another alternative would be to make the open message something like:

{=SomeFunctionName()}

This allows you to run a function to display the message. You should make sure the function's type is "string", and make it return the message you want to show.


"This is a slight weirdness in the editor. " No kidding! But this solves it. What I needed was there, I just never would have thought to look for it under a different object type. Thanks too for OpenObject -- I hadn't known that one yet.


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

Support

Forums