If object has attribute

I'm trying to make an object, specifically a sealed medical container. The player can use any item they have in their inventory that has the attribute 'isSharp' that I added to the game attributes.

That way I can define a knife, a shard of glass, or a pair of scissors as 'sharp' using that attribute.

So what I TRIED to do was make the medical container a container, then run a script on "open" that checks if object has attribute 'isSharp' but the object box is blank. How do I tell Quest I don't care what the object is, as long as the user has it and is using it to try and open the case, AND the object isSharp.


Are you running your checking script in the open command, or in the object's container tab?


I think you'd have the container's "openscript" something like:

sharpthing = PickOneObject (FilterByAttribute (ScopeInventory(), "isSharp", true))
if (sharpthing = null) {
  msg ("You need something sharp to open it with.")
}
else {
  msg ("You cut it open with " + GetDisplayName(sharpthing) + ".")
  this.isopen = true
  this.openscript = null
}

(I included the line this.openscript = null because I assume that once you've cut the seal on the container, you can close it and open it again without needing something sharp; so it disables the script once you've successfully opened the container)


I added your code to the "After opening the object" Section of the container tab.

It opens and says "You need something sharp to open it with."

I made the 'knife' and added the attribute isSharp (from my game attribute) but it's not recognizing it.


add/create Object (to where ever you want it) -> Name: knife

left side's "tree of stuff" -> 'knife' Object -> 'Attributes' Tab -> Attributes (the box at the bottom) -> add -> (see below)

(Object: knife)
Attribute Name: isSharp
Attribute Type: boolean
Attribute Value: true

// VS:

add/create Object (to where ever you want it) -> Name: hammer

left side's "tree of stuff" -> 'hammer' Object -> 'Attributes' Tab -> Attributes (the box at the bottom) -> add -> (see below)

(Object: hammer)
Attribute Name: isSharp
Attribute Type: boolean
Attribute Value: false

<object name="knife">
  <attr name="isSharp" type="boolean">true</attr>
</object>

// vs:

<object name="hammer">
  <attr name="isSharp" type="boolean">false</attr>
</object>

// -----------------

// and essentially what mrangel's code is doing:

if (OBJECT.isSharp = true) {
  // (cut) open the container object
} else if (OBJECT.isSharp = false) {
  msg ("You need a sharp Object  to cut open the container object, silly")
}

// the 'knife' Object is sharp: knife.isSharp = true
// whereas, the 'hammer' Object is blunt: hammer.isSharp = false

now, mrangel's code should work, assuming you got it all set up correctly and in proper placement and all that... lol


you may want to try to understand Attribute usage better... as this is really advanced/confusing stuff if you're new to coding.....

I got some links/guides to help you get started:

http://textadventures.co.uk/forum/general/topic/ljjm32av4e2t9ot49k478g/help#710be61e-eae1-4af1-8363-520cc718ba1c

(see the 'Attributes and if script' link specifically, but you may want to look at the 'quest code structure and coding basics' link first)


I added your code to the "After opening the object" Section of the container tab.

It opens and says "You need something sharp to open it with."

It should be in the "Script to run when opening object" section.
If this doesn't appear (the editor confuses me, hiding some attributes sometimes), you can either:

  • add this.isopen = false next to the "You need something sharp" message (this will close the box again after displaying its contents, so not ideal)

  • or you could give the container a script attribute named openscript and put the script there.

I made the 'knife' and added the attribute isSharp (from my game attribute) but it's not recognizing it.

Not sure what you mean by "(from my game attribute)".

I assumed that isSharp is a boolean attribute with the value true. Is that right?
If not, you'll need to show us what the value of that attribute is.


Well the "before you open or close" only appears when you choose 'Openable/Closable' on the container type. Other options don't show it. I entered in the code and it's still not working.

if (OBJECT.isSharp = true) {
msg ("You use the #OBJECT# to cut open the container ")
}
else if (OBJECT.isSharp = false) {
msg ("You need something sharp in order to cut this open!")
}

"(from my game attribute)" when I added verbs they appeared under GAME, so I thought all attributes have to be applied to the game before I can use them on an object, I've figured out this isn't necessary.

I got this rigged up, the knife has .isSharp added as an attribute, and I can use the knife. I've tried 'use knife on container' (container is a legal alias for the medical container, I added it under 'Other Names') I've tried 'cut open container with knife' and all kinds of things.

Open throws an error (because there's no OBJECT to check an attribute for I assume) Close just states it's already closed.


OK... so you want it to do nothing if the player types "open container", so they have to enter "use knife on container"?

My previous code was designed so that if the player is holding a sharp object, they will use that to open the container when they type "open container".

For the "Use knife on container" command, you'll need to do it a bit differently. In the container's "use/give" tab, where it says "Use (other object) on this:", choose "Handle objects individually" from the drop down. There's space to add any specific objects that have special effects, but you don't need to use that. In the default script (under "Use any other object on this:") you can put:

if (this.isopen) {
  msg ("The container is already open.")
}
else if (object.isSharp) {
  msg ("You use the " + GetDisplayAlias(object) + " to cut open the container ")
  this.open = true
  this.openscript = null
  OpenObject (this)
}
else {
  msg ("You need something sharp in order to cut this open!")
}

Not exactly what you asked for but you could request a menu, listing all sharp objects as an option to cut the obstacle. Then just click the item you want to use. Maybe it doesn't care which Item is used to cut a seal at all ^^

cutWith = FilterByAttribute(ScopeInventory(), "isSharp", true)
ShowMenu("Cut the seal with which Item?", cutWith, true) {

Log in to post a reply.

Support

Forums