the 'this' within Script Attributes, is not handled by 'invoke', but is handled by 'do'

I was pulling my hear out as 'this' was no longer working in an Object's Script Attributes, but finally I tracked it down, to it not working through the use of 'invoke', but works fine with 'do', in activating the Object's Script Attributes

maybe if Pixie or whoever knows the internal coding of quest, if you can check the 'invoke' coding, as maybe it was forgotten or accidentally messed up to no longer work with 'this' within the Script Attributes

or, is 'invoke' not suppose to work with 'this' within the Script Attributes it is activating?

'do' uses an Object Parameter (and a Script Parameter), while 'invoke' does not (it just uses a Script Parameter), is this why 'this' doesn't work with 'invoke', but does work with 'do ???

('this' also works with Delegates too, just not with 'invoke')


Yep, that's the difference between 'do' and 'invoke'.

invoke invokes a script.
do does a script attribute of this.

That is exactly how it's supposed to work.


Let's say you have a script attribute named "testing".

To invoke that:

invoke (this.testing)

It messed me up before, because the syntax is different when using do:

do (this, "testing")

@ RH:

I meant using 'this' within the Script Attribute when: using 'invoke' to call the Script Attribute (doesn't work) vs using 'do' to call the Script Attribute (does work):

invoke (test_object.test_script) // causes an error: 'this' is not defined, not recognized as Object or variable

do (test_object, "test_script") // NO error, displays correctly: 7

// using the Verb Functionality (hyperlinks and/or right pane object-buttons): NO error, displays correctly: 7

// game.start, via the automatic running/calling of it, works fine too

// (need to test 'invoking' the 'game.start', to confirm it causes an error, lol)

// rundelete (blah_object, "blah_script") // need to test this a bit more thoroughly (if it always works with 'this' or if it depends upon the passed Parameters of the Delegate)

-------

<delegate name="blah_delegate" />

<game name="NAME_OF_GAME">

  <attr name="firstpublished" type="string">2019</attr>

  <attr name="start" type="script">

    msg (this.firstpublished)

    invoke (game.start) // need to test, if causes error (should cause error, ignore that this would be an infinite/endless loop, as is, lol)

    do (game, "start") // need to test, if causes error (should work, ignore that this would be an infinite/endless loop, as is, lol)

    rundelete (blah_object, "blah_script") // need to test/examine a bit more with the Delegates, to see if using 'this' always works or not (if it has to do with the Parameters of the Delegate or not)

  </attr>

</game>

<object name="test_object">

  <attr name="count" type="int">7</attr>

  <attr name="test_script" type="script">
    msg (this.count)
  </attr>

  <attr name="blah_script" type="blah_delegate">
    msg (this.count)
  </attr>

</object>

<verb>
  <property>test_script</property>
  <pattern>test_script</pattern>
</verb>

@ mrangel:

this entire time, until now, I've never had a problem with using 'this' within a Script Attribute, I never realized that I must have never used 'this' and 'invoke' together, as this is my first time ever being met with 'this' not working, it took quite a bit of time for me to track down why 'this' was no longer working for me, laughs

Also, I just realized now... in my troubleshooting search for why 'this' wasn't working for me, I was looking at some other code of mine, and it was working fine with using 'this' within a Script Attribute, but I was using the Verb Functionality (using the hyperlinks/right-pane object-buttons) to call the Script Attributes, and had no problem with using 'this' within the Script Attribute, it was only occurring when I was calling them using 'invoke', if only I had known/realized it... sighs.. argh

(its hard to track down the cause of a problem, when you don't know what is cause of the problem, lol)

(now, in hindsight, I'm realizing a lot of things that I was clueless about as I was trying to figure out why 'this' was no longer working for me, haha, like the Verb Functionality just now)


invoke (test_object.test_script)

When the interpreter comes to this, it first evaluates the expression test_object.test_script, which returns a script.

This script is then passed to the invoke function.

Invoke only knows about the script; it doesn't know anything about the expression that returned the script. It doesn't know if you got the script from a script attribute, a dictonary, a variable, a function that returns a script, or anywhere else.


I'm confused and trying to understand.

Here is my small example game:

<!--Saved by Quest 5.8.6809.15141-->
<asl version="580">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="Invoke this">
    <gameid>9110e147-2ff2-4e45-a69c-387b3fe96d73</gameid>
    <version>1.0</version>
    <firstpublished>2019</firstpublished>
  </game>
  <object name="room">
    <inherit name="editor_room" />
    <isroom />
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
    </object>
    <object name="maguffin">
      <inherit name="editor_object" />
      <attr name="script_att" type="script">
        msg ("This is the script_att!")
      </attr>
      <test type="script">
        invoke (this.script_att)
      </test>
    </object>
  </object>
  <verb>
    <property>testing</property>
    <pattern>testing</pattern>
    <defaultexpression>"You can't testing " + object.article + "."</defaultexpression>
  </verb>
  <verb>
    <property>test</property>
    <pattern>test</pattern>
    <defaultexpression>"You can't test " + object.article + "."</defaultexpression>
  </verb>
</asl>

When I use the maguffin object's test verb (which runs invoke (this.script_att)), it works.

What am I doing differently?


Oh, never mind. I think I see what you guys are talking about.

If my maguffin object had a testing script attribute like this:

msg(this.alias)

...then invoke would throw an error, but do would work correctly.


Although if you really want to use "this" with invoke, you can.

do (someobject, "someattribute") is functionally the same as invoke (GetScript (someobject, "someattribute"), QuickParams ("this", someobject))

Here's a thought on why that might be useful:

Silly code off the top of my head

This would create a function which takes the same parameters as do, but can be used on an attribute that is a list or dictionary of scripts (in which case it runs all of them). Not sure if there's any use to it, but it shows that it might be useful to manually put "this" into the parameters array passed to invoke.

<function name="FlexibleDo" parameters="object, attribute, params">
  if (not IsDefined ("params")) {
    params = NewDictionary()
  }
  else if (params = null) {
    params = NewDictionary()
  }
  if (DictionaryContains (params, "this")) {
    dictionary remove (params, "this")
  }
  dictionary add (params, "this", object)
  scripts = NewList()
  switch (TypeOf (object, attribute)) {
    case ("script") {
      list add (scripts, GetAttribute (object, attribute))
    }
    case ("list") {
      scripts = GetAttribute (object, attribute)
    }
    case ("dictionary", "scriptdictionary") {
      dict = GetAttribute (object, attribute)
      foreach (key, dict) {
        list add (scripts, DictionaryItem (dict, key))
      }
    }
    default {
      error (object.name + "." + attribute + " isn't a script collection.")
    }
  }
  foreach (script, scripts) {
    if (TypeOf (script) = "script") {
      invoke (script, params)
    }
  }
</function>

(it would also be fairly simple to modify this function so that it displays a text or runs a script, in the way verb attributes are handled)


For anyone who isn't grasping the concept (like I wasn't), I've updated my maguffin object to demonstrate.

Here's the code:

    <object name="maguffin">
      <inherit name="editor_object" />
      <attr name="msg_att">This is script_att_two!</attr>
      <attr name="script_att" type="script">
        msg ("This is script_att!")
      </attr>
      <attr name="script_att_two" type="script">
        msg (this.msg_att)
      </attr>
      <test type="script">
        msg ("Using 'invoke'...")
        msg ("Invoking script_att...")
        invoke (this.script_att)
        // *** ABOVE LINE DOES NOT THROW AN ERROR BECAUSE this NOT USED IN script_att
        msg ("Invoking script_att_two...")
        invoke (this.script_att_two)
        // *** ABOVE LINE THROWS ERROR BECAUSE this IS USED IN script_att_two
        //
        // *** THE REST OF THIS CODE WORKS CORRECTLY
        msg ("Using 'do'...")
        msg ("Doing script_att...")
        do (this, "script_att")
        msg ("Doing script_att_two...")
        do (this, "script_att_two")
      </test>
    </object>

Here's a screenshot of gameplay:

Invoke_this-SCREENSHOT


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

Support

Forums