Referencing an attribute using 'this' inside a menu

Hi again. I'm feeling a little like a broken record, but I'm a little confused about how 'this' and 'do' work when used inside a menu.

This is the code I currently have:

commands = NewStringList()
list add (this.commands, "Command_01")
list add (this.commands, "Command_01")
ShowMenu ("Commands:", this.commands, true) {
  if (result = "Command_01") {
    do (this, "Script_01")
  }
  if (result = "Command_02") {
    do (this, "Script_02")
  }
}

But 'this' isn't recognised when it's used inside the menu. Is there anyway I can use 'do' to run a script attribute on an object without referencing it by name?


the 'this' special keyword/keycommand GETS_AND_RETURNS (if it can, otherwise: ERROR) the parent (containing/container) Object of the (contained) scripting:

<object name="tree">
  <attr name="look" type="script">
    msg (this.name)
    // otherwise, you'd normally do this:
    // msg (tree.name)
  </attr>
</object>

// invoke (tree.look), it'll output: tree

-----------------------------

<object name="flower">
  <attr name="look" type="script">
    // otherwise, you'd normally do this:
    // msg (flower.name)
  </attr>
</object>

// invoke (flower.look), it'll output: flower

---------

<type name="example_type">
  <attr name="look" type="script">
    msg (this.name)
  </attr>
</type>

<object name="tree">
  <inherit name="example_type" />
</object>
// invoke (tree.look), it'll output: tree

<object name="flower">
  <inherit name="example_type" />
</object>
// invoke (flower.look), it'll output: flower

I'm not that familiar with Commands in regards to this stuff (having local commands: commands inside of an Object/Room Object)...

you might not be able to do this:

this.commands / NAME_OF_OBJECT.commands

if you can... then you need to change your:

commands = NewStringList()

to this:

this.commands = NewStringList()

and I'm assuming that this code block is indeed inside of Objects (as that's why you'd want to be using the 'this' keyword/keycommand), for example (not the most practical design/method, but it's just an example):

(and, you got a few other mistakes to fix up, see below for corrections)

(going by this: http://docs.textadventures.co.uk/quest/elements/ --- see the very bottom: any other XML element will set an attribute of that name on the parent object/type/exit/command, maybe the below might work)

<object name="example_1_object">
  <attr name="Command_01" type="command">
    <pattern>XXX</pattern>
    <script>
      // XXX
    </script>
  </attr>
  <attr name="Command_02" type="command">
    <pattern>XXX</pattern>
    <script>
      // XXX
    </script>
  </attr>
  <attr name="example_script" type="script">
    this.commands = NewStringList()
    list add (this.commands, "Command_01")
    list add (this.commands, "Command_02")
    ShowMenu ("Commands:", this.commands, true) {
      if (result = "Command_01") {
        do (this, "Commands_01", "script")
      }
      if (result = "Command_02") {
        do (this, "Commands_02", "script")
      }
    }
  </attr>
</object>

<object name="example_2_object">
  <attr name="Command_01" type="command">
    <pattern>XXX</pattern>
    <script>
      // XXX
    </script>
  </attr>
  <attr name="Command_02" type="command">
    <pattern>XXX</pattern>
    <script>
      // XXX
    </script>
  </attr>
  <attr name="example_script" type="script">
    this.commands = NewStringList()
    list add (this.commands, "Command_01")
    list add (this.commands, "Command_02")
    ShowMenu ("Commands:", this.commands, true) {
      if (result = "Command_01") {
        do (this, "Commands_01", "script")
      }
      if (result = "Command_02") {
        do (this, "Commands_02", "script")
      }
    }
  </attr>
</object>

this is way beyond my knowledge... unless you need to use Commands... you should just use a Function or Script Attributes (+ Delegates if need Parameters and/or return type)...

just found this too:

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

looks like some of the 'object' Parameters might actually mean OBJECT (of quest's engine's/itself's programming, its OOP/OOD structure --- which in quest, is its 'Elements', such as 'Commands', 'Turnscripts', 'Verbs', 'Exits', 'Objects', 'Functions', 'Object Types / Types', etc etc etc), so maybe it might be better for the 'do' syntax to put your Commands into an Objectlist Attribute and use the 'ObjectListItem', which may make it more simple (for me hopefully lol) to get the syntax working with 'do'....


Thanks hegemon! I don't quite understand your explanation yet, but I'm trying to work through it. :)

EDIT: I thought I'd found a way to circumvent using 'this' inside a menu, but it seems I haven't. New thread here: http://textadventures.co.uk/forum/quest/topic/ajju5ga33eojfmi0qk9xnw/looping-a-script-but-pausing-for-user-input


As you say, this has no meaning inside ShowMenu. The solution is to assign the thing to an attribute of game before calling ShowMenu, and use that inside the script.


I... Don't quite understand how to do that yet. But I'll keep working on it.

EDIT: OH! That was simpler that I thought it would be. Thanks!

Here's an example of how my menu works, in case needs it for future reference

game.menuitem = this
this.commands = NewStringList()
list add (this.commands, "Attack")
list add (this.commands, "Defend")
list add (this.commands, "Do Nothing")
ShowMenu ("Commands:", this.commands, true) {
  if (result = "Attack") {
    do (game.menuitem, "attack_script")
  }
  if (result = "Defend") {
    do (game.menuitem, "defend_script")
  }
  if (result = "Do Nothing") {
    do (game.menuitem, "nothing_script")
  }
  on ready {
    do (game.menuitem, "combat_script")
  }
}

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

Support

Forums