[IGN-1660]PSA - Assigning values to a property in a script may drive you nuts

Hello folks,

This got me pulling the hair out of my head. I spent the last hour yelling at my screen while doing all sorts of tests and experiments.
I think I got it now, but... try it yourself.
Here's the json for a perspective view:

{
  "custom": {},
  "params": {
    "category_id": "",
    "family_id": "",
    "item_id": ""
  },
  "propConfig": {
    "custom.isFresh": {
      "access": "PRIVATE"
    },
    "params.category_id": {
      "paramDirection": "input",
      "persistent": true
    },
    "params.family_id": {
      "paramDirection": "input",
      "persistent": true
    },
    "params.item_id": {
      "paramDirection": "input",
      "persistent": true
    }
  },
  "props": {},
  "root": {
    "children": [
      {
        "meta": {
          "name": "Label"
        },
        "position": {
          "basis": "32px"
        },
        "props": {
          "text": "open your console, type something in the text field, then click the button."
        },
        "type": "ia.display.label"
      },
      {
        "meta": {
          "name": "TextField"
        },
        "position": {
          "basis": "32px"
        },
        "props": {
          "placeholder": "type something here",
          "text": "qwd"
        },
        "type": "ia.input.text-field"
      },
      {
        "custom": {
          "data": {
            "text": "qwd"
          }
        },
        "events": {
          "component": {
            "onActionPerformed": {
              "config": {
                "script": "\tdata \u003d self.custom.data\n\t\n\ttext \u003d self.getSibling(\"TextField\").props.text\n\tdata[\u0027text\u0027] \u003d text\n\t\n\tsystem.perspective.print(\u0027-\u0027*20)\n\tsystem.perspective.print(\"text: {}\".format(text))\n\tsystem.perspective.print(\"data[\u0027text\u0027]: {}\".format(data[\u0027text\u0027]))"
              },
              "scope": "G",
              "type": "script"
            }
          }
        },
        "meta": {
          "name": "Button"
        },
        "position": {
          "basis": "34px"
        },
        "type": "ia.input.button"
      },
      {
        "meta": {
          "name": "Label_0"
        },
        "position": {
          "basis": "32px"
        },
        "props": {
          "text": "now check the code on the button..."
        },
        "type": "ia.display.label"
      }
    ],
    "meta": {
      "name": "root"
    },
    "props": {
      "direction": "column",
      "style": {
        "gap": "10px",
        "padding": "15px"
      }
    },
    "type": "ia.container.flex"
  }
}

Here's a quick summary:
There's a button. There's a data custom property.
The button has code on onActionPerformed as follow:

data = self.custom.data

text = self.getSibling("TextField").props.text
data['text'] = text

system.perspective.print('-'*20)
system.perspective.print("text: {}".format(text))
system.perspective.print("data['text']: {}".format(data['text']))

The text field is only there to provide a way to change the value
Simple, right ? wrong:

I'm guessing the printing happens before the property actually gets its value and since data is just a reference to that property, the old value is the one that gets printed.
It would make sense, but damn did it trip me up !

If it's not that, then I'll take the whatever explanation you might have.

Anyway, if you ever find yourself in this situation - keep calm, declare another variable and use this one instead.

IIUC, writes to Perspective properties are serialized by a queue somewhere. Not sure of the details, but it explains the behavior you are seeing.

im afread it will get more complicated... look at this

regrabbing the self.custom (dataRe) gives the coorrect value while data still is wrong

1 Like

Among other things, basically, don't do this. You're capturing the 'property tree' wrapper housed at self.custom.data in your script. Mutations are primarily forwarded to the actual backing store (because that's what matters, and what syncs to the frontend) and there's a longstanding bug where the local instance you have doesn't reflect those updates correctly. It's mostly been a nuisance bug because the typical pattern (referring to component props fully qualified all the time) avoids it.

Man we run into this at least once or twice a week and it's super frustrating.

It's good to know we can "work around" this by using a fully qualified path, but sometimes those fully qualified paths look long and ugly.

1 Like

Yeah, I've bumped the internal ticket a bit in priority.

4 Likes

Not only that, but now if you move anything around, you have to remember to update the path in multiple places within your script. Ctrl-F can help with that I guess but is not my favorite way

1 Like

Yea okay that makes sense, and explains the weirdness exposed in @victordcq 's post

What's the term for "more than" super frustrating.

2 Likes

infuriating? ha

I will admit I get angry when I'm like "why the hell isn't this working"

4 Likes

Imagine my face when I was doing

a = b
return a == b

and it was returning False

5 Likes