Need help with "Checking for an Attribute" (Tutorial 7. Custom commands)

Hi,

I'm new here and I have a question regarding the tutorial. I am stuck in the section "checking for an attribute" (step 7: Custom commands) and could not find an answer in the forum. After running the game I always get the following error when I would like to weigh my objects:

Error running script: Error compiling expression 'HasAttribute(, "weight")': SyntaxError: Unexpected token ","; expected one of ")"Line: 1, Column: 14

This is what "code view" shows me:

(command pattern -> weigh #object#)

if (HasAttribute(, "weight")) {
msg ("It weighs " + object.weight + " grams.")
}
else {
msg ("You can't weigh that.")
}

(game > scripts:)

set (eggs, "weight", 250)

I don't see what I did wrong here (I guess it has something to do with the missing object in (,_"weight")) and I'm sorry to bother you with such a simple question.. I thought I did exactly as the tutorial told me or maybe I'm just blind or stupid or both. :(

Many thanks in advance for your help!


K.V.

Hello!

if (HasAttribute(, "weight")) { should be if (HasAttribute(object, "weight")) {


(filler for getting my edited post, updated/posted)


just to further add commentary to KV's post:

(this is a pseudo/partial example in-code, albiet using my own re-naming/re-labeling, lol)

<game name="example_game">
  <attr name="pov" type="object">player</attr>
  <attr name="start" type="script">
    set (eggs, "weight", 250) // or you can do this instead: eggs.weight = 250
  </attr>
</game>

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

<object name="player">
  <attr name="parent" type="object">room</attr>
</object>

<object name="eggs">
  <attr name="parent" type="object">room</attr>
  <attr name="weight" type="int">0</attr>
</object>

<command name="example_weigh_command">
  <pattern>weigh #object_parameter#</pattern>
  <script>
    if (HasInt (object_parameter, "weight")) {
      msg ("It weighs " + object_parameter.weight + " grams")
    } else {
      msg ("You can't weigh that")
    }
  </script>
</command>

the Command's and its 'pattern' String Attribute's parsing functionality looks for the/your typed-in input pattern of: weigh NAME_OF_OBJECT

the hashes/pound symbols in '#objectXXX' or '#textXXX' in the 'pattern' String Attribute of a Command, are command-symbols for quest to store that input location into a 'objectXXX' or 'textXXX' Parameter VARIABLE, which the Command's scripting (its 'script' Script Attribute) will use.

so, for example:

I type in: weigh eggs

for the '#objectXXX#' pattern, quest looks through ONLY all of the Objects WITHIN THE ROOM YOU'RE IN, to see if there is such an 'eggs' Object

if it exists (again it only checks for that object within room you're currently in. It does NOT check the entire game, for that object) (which in my example, indeed it does exist within the room you're currently in), then quest stores it into the 'objectXXX' Parameter VARIABLE:

objectXXX = eggs // (notice that the Parameter VARIABLE does NOT have the hash/pound symbols in it. The hash/pound symbols are only for the Command's 'pattern' String Attribute, as they're special command-symbols for quest's parsing functionality)

so, now that Parameter VARIABLE is/can-be used in the Command's scripting, as seen:

    if (HasInt (object_parameter, "weight")) {
      msg ("It weighs " + object_parameter.weight + " grams")
    } else {
      msg ("You can't weigh that")
    }

you must have either:

(my 'XXX' represents whatever you want to name/label it as, in my example above, as can be seen, I like for my own personal naming/labeling system/convention, to be descriptive: object_parameter, because it is a Parameter VARIABLE, lol. So, my generalized 'objectXXX' represents the/my actual name/label choice of 'object_parameter')

Pattern: #object#
Scripting: object

or

// my generalized format (not an actual format... well you could actually use it... lol):
Pattern: #objectXXX#
Scripting: objectXXX

for examples:

Pattern: #object_parameter#
Scripting: object_parameter

// an example of using multiple Parameters:
Pattern: weigh #object1#, #object2#, and #object3#
Scripting: object1
Scripting: object2
Scripting: object3

// an example of using multiple Parameters:
Pattern: weigh #object_100#, #object_200#, and #object_300#
Scripting: object_100
Scripting: object_200
Scripting: object_300

Pattern: #objectABC#
Scripting: objectABC

or

Pattern: #text#
Scripting: text

or

// my generalized format (not an actual format... well you could actually use it... lol):
Pattern: #textXXX#
Scripting: textXXX

for examples:

Pattern: #text_parameter#
Scripting: text_parameter

// an example of using multiple Parameters:
Pattern: weigh #text1#, #text2#, and #text3#
Scripting: text1
Scripting: text2
Scripting: textt3

// an example of using multiple Parameters:
Pattern: weigh #text_100#, #text_200#, and #text_300#
Scripting: text_100
Scripting: text_200
Scripting: text_300

Pattern: #textABC#
Scripting: textABC


in other words:

the Pattern for the Parameters must be:

start with a '#', then followed directly (no space) by either 'object' or 'text', optionally more characters/symbols (or not), and lastly ends with a '#'

(my use of the 'XXX' represents the 'optionally more characters/symbols or not', lol)

the Parameter VARIABLE itself (used in the scripting), is the same as above, but without the '#' symbols in it.


the (generalized/dynamic) 'HasAttribute' Function has quest parse the type of Attribute automatically for you, but if you know what the Attribute is (or what it has to be), you can use the specific Functions instead:

http://docs.textadventures.co.uk/quest/functions/index_allfunctions.html (scroll down to the 'h' section/category)

HasString (XXX)
HasInt (XXX)
HasDouble (XXX)
HasObject (XXX)
HasBoolean (XXX)
HasScript (XXX)


Thank you so much for your help (and the detailed explanation @hegemonkhan. I had to re-read it a few times to be honest, but now it makes more sense)! I probably tried everything except this. Sometimes it is as simple as that... so obvious that I can't believe that I did not figure it out myself.

Thanks again. :)


learning coding/scripting (and then programming and beyond) is not easy, when I was learning for my first time too (5 years ago with quest, which is an excellent source for learning to code if you're completely new to coding/programming), it took me many attempts to understand as well. Unfortunately now, I respond with posts that assume people understand coding already, as it takes a lot of time and effort to have to help/explain/guide someone who's new to coding through it all, especially having to do so with the GUI/Editor, as that takes more explanation and steps/writing/typing/posting than it does to write (and copy and paste) code into a post. When I have the time, I can give GUI/Editor help, but that's not often, especially now with school and school work.

so, ignore my posts if they don't help, and/or (if you want) try to understand them / learn, through multiple attempts and/or asking for help with them. I'm happy to help and explain you through my posts and the code and/or how to do the code stuff through/within the GUI/Editor, but it's a matter of when I can find or have the time, to do so, so you might have to wait a bit, though hopefully, others will help you before I finally get the time to do so, hehe. But if not, then I'll help you when that free/open time comes for me to be able to do so.


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

Support

Forums