Calling a custom attribute with an expression that uses a function return as the object name

So, I'm kind of new at this language, but I have some programming experience. I'm confused by the way the language treats attributes. There seems to be a difference in the way you can call custom attributes versus built-in attributes.

For example, let's say I want to output the pattern of the shirt the character is wearing. (Not my actual use case, but it highlights the problem I'm trying to figure out in a simple way.)

I create a wearable item "shirt" and give it a custom attribute "pattern" with the value "polka dots". I set it to the wear slot "upper," then I put that shirt on my character.
Next, I make a button in the character's room that runs this function when pressed:

  <function name="test_script">
    OutputTextNoBr ("You are wearing a shirt with the pattern: ")
    msg (GetOuter("upper").pattern)
  </function>

This doesn't work. I get the error message:

You are wearing a shirt with the pattern: Error running script: Error compiling expression 'GetOuter("upper").pattern': Unknown object or variable 'pattern'

Okay, I thought, this language just doesn't know how to use the .attribute syntax on the result of a function in an expression. But then I tried something. I changed the function to do this instead:

  <function name="test_script">
    OutputTextNoBr ("You are wearing a shirt with the name: ")
    msg (GetOuter("upper").name)
  </function>

Lo and behold, it works! I get the output:

You are wearing a shirt with the name: shirt

It also seems to work with .type, .parent, or any other built-in attribute. Why are custom attributes different in this way? Is there any way to do what I'm trying to do with a custom attribute?

Thank you!


There are some weird optimisations in syntax handling.
You come across them more often when they completely break some expressions.

It's probably safest just to assume that the left hand side of the dot has to be an object or variable; with the exception of certain attributes that are recognised as magic words.

So in those cases, you probably want:

  <function name="test_script">
    OutputTextNoBr ("You are wearing a shirt with the pattern: ")
    msg (GetString (GetOuter("upper"), "pattern"))
  </function>

or

  <function name="test_script">
    OutputTextNoBr ("You are wearing a shirt with the pattern: ")
    garment = GetOuter("upper")
    msg (garment.pattern)
  </function>

or

  <function name="test_script">
    garment = GetOuter("upper")
    msg ("You are wearing a shirt with the pattern: " + garment.pattern)
  </function>

Log in to post a reply.

Support

Forums