Force examine object when taking object

Hi,

How do I force the examine object script when the player takes a particular item. I guess it's 'call function' but how/what?

Thanks.


you can go into the built-in 'take' Command handling of code and add in the scripting for it, but I don't like altering the built-in coding of things. This would be a global effect on all Objects (that use take), as you altered 'take' itself.

instead, I'd use the special 'changed' Script Attribute, but, you'd have to do it for every Object unfortunately:

<object name="room">
</object>

<object name="player">
</object>

<object name="example_object">
  <attr name="parent" type="object">room</attr>
  <attr name="look" type="script"> // it can also be a String Attribute too
    msg ("example_look_examine__text")
  </attr>
  <attr name="changedparent" type="script">
    if (this.parent = game.pov) {
      do (this, "look")
    }
  </attr>
</object>

I'm not sure what is the script option in the GUI/Editor, for the special 'changed' Script Attribute, you'll have to figure that out.

And the 'look' is a String/Script Attribute, so it's NOT a Function and thus you're NOT doing a function call. Instead, you ARE doing a String/Script Attribute call:

do (NAME_OF_OBJECT, "look") // HK Edit: this only works for when it's a Script Attribute (according to the doc anyways), I had wrongly previously said that 'do' works for both String and Script Attributes, hence my edit here.
// or for when 'look' is set as a String Attribute, you can do this too:
msg (NAME_OF_OBJECT.look)
// or for when 'look' is set as a Script Attribute, you can do this too:
invoke (NAME_OF_OBJECT.look)


K.V.

image

image


ah... after taking Object... that works... never knew that existed, lol. Much better than digging into the built-in code, trying to find the handling of the 'take' Command, hehe :D


is there a global 'after taking Object' in one of the 'game' Game Object's Tabs ???


thanks both, much appreciated!


K.V.

If you use do (object, "look") and the object does not have a description set up, it will throw an error:


You are in a room.
You can see a phone and a box.
You can go in.

> take box
You pick it up.
Error running script: Object reference not set to an instance of an object.


So:

Just add an if (HasAttribute(object, "look")) { like so:

if (HasAttribute(box, "look")) {
  msg (box.look)
  do (box, "look")
}
else {
  msg ("No 'look' set up.")
}

image


you can add further handling too:

http://docs.textadventures.co.uk/quest/functions/typeof.html

http://docs.textadventures.co.uk/quest/scripts/do.html (oops, in some post of mine I said/thought that 'do' handles both String and Script types, but it's just Script types, like 'invoke', though 'do' allows for concatenation, so it's more powerful/useful)

http://docs.textadventures.co.uk/quest/scripts/invoke.html

if (HasAttribute (box, "look")) {
  if (TypeOf (box, "look") = "string") {
    msg (box.look)
  } else if (TypeOf (box, "look") = "script") {
    invoke (box.look) // or: do (box, "look")
  }
} else {
  msg ("No 'look' set up.")
}

The only problem I've found with the 'invoke' script is that, if your function/verb uses the special variable 'this', it causes an error message to occur.
so for the above example you can use
do (this, "look")
but not
invoke (this.look)


ah, thanks for pointing that out, I was not aware that 'this' only works with 'do' but not with 'invoke'.


what makes 'do' more useful/powerful is that you can concatenate with it, for example:

<object name="joe">
  <attr name="fight_script_attribute" type="script">
  </attr>
  <attr name="defend_script_attribute" type="script">
  </attr>
</object>

<object name="jim">
  <attr name="fight_script_attribute" type="script">
  </attr>
  <attr name="defend_script_attribute" type="script">
  </attr>
</object>

<object name="john">
  <attr name="fight_script_attribute" type="script">
  </attr>
  <attr name="defend_script_attribute" type="script">
  </attr>
</object>

<object name="jeff">
  <attr name="fight_script_attribute" type="script">
  </attr>
  <attr name="defend_script_attribute" type="script">
  </attr>
</object>

<function name="example_function">

  objectlist_variable = NewObjectList ()
  list add (objectlist_variable, joe)
  list add (objectlist_variable, jim)
  list add (objectlist_variable, john)
  list add (objectlist_variable, jeff)

  stringlist_variable = NewStringList ()
  list add (stringlist_variable, "fight")
  list add (stringlist_variable, "defend")

  foreach (object_variable, objectlist_variable)  {
    string_variable = StringListItem (stringlist_variable, GetRandomInt (0, ListCount (stringlist_variable) - 1))
    do (object_variable, string_variable + "_script_attribute") // concatenation, which you can't do with 'invoke'
    // I think you may be able to do concatenation with the OBJECT_NAME part of it too... but I've not tried/tested it... may not be able to... or may be able to... no idea... need to try/test it, lol
  }

</function>

K.V.
  foreach (object_variable, objectlist_variable)  {
    string_variable = StringListItem (stringlist_variable, GetRandomInt (0, ListCount (stringlist_variable) - 1))
    do (object_variable, string_variable + "_script_attribute") // concatenation, which you can't do with 'invoke'
    // I think you may be able to do concatenation with the OBJECT_NAME part of it too... but I've not tried/tested it... may not be able to... or may be able to... no idea... need to try/test it, lol
  }

Holy crap, HK!

Is this to 'do' a random attribute???


no, 'do' is a Function:

http://docs.textadventures.co.uk/quest/scripts/
http://docs.textadventures.co.uk/quest/scripts/do.html

'do' (and 'invoke') are 'script attribute calls', similiar to 'function calls'

'do' and 'invoke' enable you to do/run/execute/activate/use an Object's Script Attributes

for example (an infinite loop, so don't do it):

<game name="example_game">
  <attr name="start" type="script">
    msg ("hi")
    do (game, "start") // or: invoke (game.start) // or: do (this, "start") // but thanks to someone else pointing out, you can NOT do this: invoke (this.start)
  </attr>
</game>

K.V.
 foreach (o, ScopeVisibleForRoom(game.pov.parent))  {
    x = StringListItem (o, GetRandomInt (0, ListCount (stringlist_variable) - 1))
    do (o, "x")
  }

Yeah... I was thinking something like this, but I was just temporarily insane.


for your example, it'd actually be with OUT the double quotes, as the 'x' is a VARIABLE:

foreach (o, ScopeVisibleForRoom(game.pov.parent))  {
  x = StringListItem (o, GetRandomInt (0, ListCount (stringlist_variable) - 1))
  do (o, x)
}

K.V.

with OUT the double quotes

Yep!

Error running script:

(I see that at least 100 times a day!)

Thanks, HK!


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

Support

Forums